@uniquedivine/tg-sai 0.0.3 → 0.0.5

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/health.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  import { constants } from "node:fs"
2
2
  import { access, mkdir, readdir, readFile, stat } from "node:fs/promises"
3
3
  import { join } from "node:path"
4
-
5
- import { configDir, configPath, readConfig } from "./profiles"
4
+ import { configDir, configPath, profileLabel, readConfig } from "./profiles"
6
5
  import type {
7
6
  ProfileStoreEnv,
8
7
  TelegramCredentials,
@@ -110,45 +109,43 @@ function directoryFix(path: string): string[] {
110
109
  ]
111
110
  }
112
111
 
113
- function missingProfileFix(profileName?: string): string[] {
114
- const addCommand = profileName
115
- ? `tg-sai profile add --name ${profileName} --src <number>`
116
- : "tg-sai profile add --name <name> --src <number>"
112
+ function missingProfileFix(): string[] {
117
113
  return [
118
114
  "Find a Telegram credential source and save it as a tg-sai profile.",
119
115
  "tg-sai profile find",
120
- addCommand,
116
+ "tg-sai profile add",
117
+ "If apiId/apiHash are missing, create a Telegram API app at https://my.telegram.org/apps",
118
+ "Then run: tg-sai profile add --qr",
121
119
  ]
122
120
  }
123
121
 
124
- function incompleteProfileFix(profileName: string, fields: string[]): string[] {
122
+ function incompleteProfileFix(selector: string, fields: string[]): string[] {
125
123
  return [
126
124
  `Fill missing profile field(s): ${fields.join(",")}`,
127
125
  "Check the redacted active profile:",
128
- `tg-sai profile show ${profileName}`,
129
- "Then refresh it from a complete credential source:",
126
+ `tg-sai profile show ${selector}`,
127
+ "Then refresh it from detected values or generate a session:",
130
128
  "tg-sai profile find",
131
- `tg-sai profile add --name ${profileName} --src <number>`,
129
+ "tg-sai profile add",
130
+ "tg-sai profile add --qr",
132
131
  ]
133
132
  }
134
133
 
135
- function connectionFix(profileName: string): string[] {
134
+ function connectionFix(selector: string): string[] {
136
135
  return [
137
136
  "Check the active profile API credentials and Telegram session string.",
138
- `tg-sai profile show ${profileName}`,
137
+ `tg-sai profile show ${selector}`,
139
138
  "If apiId, apiHash, or sessionString are stale, refresh from a known-good source:",
140
139
  "tg-sai profile find",
141
- `tg-sai profile add --name ${profileName} --src <number>`,
140
+ "tg-sai profile add",
141
+ "tg-sai profile add --qr",
142
142
  ]
143
143
  }
144
144
 
145
- function passwordFix(profileName?: string): string[] {
146
- const saveCommand = profileName
147
- ? `tg-sai profile add --name ${profileName} --password '<password>'`
148
- : "tg-sai profile add --name <name> --password '<password>'"
145
+ function passwordFix(): string[] {
149
146
  return [
150
147
  "Save the Telegram 2FA password in the active tg-sai profile:",
151
- saveCommand,
148
+ "tg-sai profile add --password '<password>'",
152
149
  "You can also override it for one command with TELEGRAM_PASSWORD.",
153
150
  ]
154
151
  }
@@ -250,7 +247,7 @@ async function checkTelegramConnection(params: {
250
247
  collector,
251
248
  "telegram_connection",
252
249
  "telegram_connection: skipped because active profile is incomplete",
253
- incompleteProfileFix(profile.name, missingFields),
250
+ incompleteProfileFix("0", missingFields),
254
251
  )
255
252
  return
256
253
  }
@@ -273,7 +270,7 @@ async function checkTelegramConnection(params: {
273
270
  "Use a Telegram user-account session for tg-sai admin operations.",
274
271
  "Switch to a user profile:",
275
272
  "tg-sai profile list",
276
- "tg-sai profile use <user-profile-name>",
273
+ "tg-sai profile use <index-or-handle-or-user-id>",
277
274
  ],
278
275
  )
279
276
  }
@@ -282,7 +279,7 @@ async function checkTelegramConnection(params: {
282
279
  collector,
283
280
  "telegram_connection",
284
281
  `telegram_connection: failed ${errorMessage(caught)}`,
285
- connectionFix(profile.name),
282
+ connectionFix("0"),
286
283
  )
287
284
  } finally {
288
285
  if (client) {
@@ -297,14 +294,14 @@ function checkPassword(
297
294
  profile: TgSaiProfile | null,
298
295
  ): void {
299
296
  if (env.TELEGRAM_PASSWORD || profile?.password) {
300
- info(collector, "password_status: present")
297
+ good(collector, "password_status: present")
301
298
  return
302
299
  }
303
300
  warning(
304
301
  collector,
305
302
  "password_status",
306
303
  "password_status: missing",
307
- passwordFix(profile?.name),
304
+ passwordFix(),
308
305
  )
309
306
  info(
310
307
  collector,
@@ -394,13 +391,13 @@ export async function runHealthCheck(params: {
394
391
  }
395
392
 
396
393
  const config = await readConfig(env)
397
- const activeProfileName = config.activeProfile
398
- const activeProfile = activeProfileName
399
- ? (config.profiles[activeProfileName] ?? null)
394
+ const activeProfile = config.profiles[0] ?? null
395
+ const activeProfileLabel = activeProfile
396
+ ? profileLabel(activeProfile, 0)
400
397
  : null
401
398
  const missingFields = activeProfile ? missingProfileFields(activeProfile) : []
402
399
 
403
- if (!activeProfileName) {
400
+ if (!activeProfile) {
404
401
  error(
405
402
  collector,
406
403
  "active_profile",
@@ -413,33 +410,20 @@ export async function runHealthCheck(params: {
413
410
  "profile_status: missing",
414
411
  missingProfileFix(),
415
412
  )
416
- } else if (!activeProfile) {
417
- error(
418
- collector,
419
- "active_profile",
420
- `active_profile: ${activeProfileName}`,
421
- missingProfileFix(activeProfileName),
422
- )
423
- error(
424
- collector,
425
- "profile_status",
426
- "profile_status: missing",
427
- missingProfileFix(activeProfileName),
428
- )
429
413
  } else {
430
- info(collector, `active_profile: ${activeProfile.name}`)
414
+ info(collector, `active_profile: ${activeProfileLabel}`)
431
415
  if (missingFields.length > 0) {
432
416
  error(
433
417
  collector,
434
418
  "profile_status",
435
419
  "profile_status: incomplete",
436
- incompleteProfileFix(activeProfile.name, missingFields),
420
+ incompleteProfileFix("0", missingFields),
437
421
  )
438
422
  error(
439
423
  collector,
440
424
  "missing_fields",
441
425
  `missing_fields: ${missingFields.join(",")}`,
442
- incompleteProfileFix(activeProfile.name, missingFields),
426
+ incompleteProfileFix("0", missingFields),
443
427
  )
444
428
  } else {
445
429
  good(collector, "profile_status: complete")
@@ -457,14 +441,11 @@ export async function runHealthCheck(params: {
457
441
  await checkLogs(collector, logsPath)
458
442
  }
459
443
 
460
- if (!activeProfileName) {
444
+ if (!activeProfile) {
461
445
  info(collector, "next: tg-sai profile find")
462
- info(collector, "next: tg-sai profile add --name <name> --src <number>")
463
- } else if (!activeProfile || missingFields.length > 0) {
464
- info(
465
- collector,
466
- `next: tg-sai profile add --name ${activeProfileName} --src <number>`,
467
- )
446
+ info(collector, "next: tg-sai profile add --qr")
447
+ } else if (missingFields.length > 0) {
448
+ info(collector, "next: tg-sai profile add --qr")
468
449
  }
469
450
 
470
451
  renderSuggestedFixes(collector)