grok-telegram-bot 2.6.0 → 2.7.0
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/.env.example +13 -0
- package/CHANGELOG.md +35 -0
- package/README.md +15 -2
- package/docs/INSTALL.md +2 -0
- package/package.json +1 -1
- package/scripts/setup.mjs +20 -3
- package/src/app/instance.ts +223 -0
- package/src/app/types.ts +7 -0
- package/src/bot/ask-user-service.ts +226 -0
- package/src/bot/bot.ts +32 -0
- package/src/bot/commands.ts +22 -2
- package/src/bot/handlers/grok-slash.ts +336 -0
- package/src/bot/handlers/system.ts +63 -1
- package/src/bot/plan-exit-service.ts +169 -0
- package/src/bot/registry.ts +11 -2
- package/src/bot/session-runtime.ts +2 -0
- package/src/cli.ts +43 -7
- package/src/config.ts +35 -25
- package/src/grok/client.ts +29 -5
- package/src/grok/plan-approval.ts +8 -0
- package/src/index.ts +4 -0
- package/src/service/linux.ts +21 -15
- package/src/service/macos.ts +20 -15
- package/src/service/platform.ts +12 -3
- package/src/service/types.ts +6 -0
- package/src/service/windows.ts +31 -22
package/src/service/platform.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { execFileSync, spawn } from "node:child_process";
|
|
6
6
|
import { join } from "node:path";
|
|
7
|
+
import { parseInstanceFlags, serviceIdentity } from "../app/instance.js";
|
|
7
8
|
import { PROJECT_ROOT, INSTANCE_DIR } from "../config.js";
|
|
8
9
|
|
|
9
10
|
export type Platform = "windows" | "linux" | "macos" | "unknown";
|
|
@@ -31,9 +32,14 @@ export function buildLaunchSpec(): LaunchSpec {
|
|
|
31
32
|
// the bot where its .env/logs/data live. Only appended for a global install
|
|
32
33
|
// (instance dir differs) so in-place checkouts keep identical launch args.
|
|
33
34
|
if (INSTANCE_DIR !== PROJECT_ROOT) args.push("--instance", INSTANCE_DIR);
|
|
35
|
+
const flags = parseInstanceFlags(process.argv);
|
|
36
|
+
const ident = serviceIdentity(INSTANCE_DIR, flags.name || process.env.GROK_TG_NAME);
|
|
34
37
|
return {
|
|
35
|
-
id:
|
|
36
|
-
displayName:
|
|
38
|
+
id: ident.id,
|
|
39
|
+
displayName: ident.displayName,
|
|
40
|
+
windowsTaskName: ident.windowsTaskName,
|
|
41
|
+
macosLabel: ident.macosLabel,
|
|
42
|
+
slug: ident.slug,
|
|
37
43
|
nodePath: process.execPath,
|
|
38
44
|
args,
|
|
39
45
|
cwd: PROJECT_ROOT,
|
|
@@ -41,7 +47,10 @@ export function buildLaunchSpec(): LaunchSpec {
|
|
|
41
47
|
// relaunches on exit — so its auto-updater exits cleanly instead of
|
|
42
48
|
// re-exec'ing (which would double-run). Windows applies no env, so its
|
|
43
49
|
// Scheduled Task (no auto-restart) takes the re-exec path instead.
|
|
44
|
-
env: {
|
|
50
|
+
env: {
|
|
51
|
+
GROK_TG_SUPERVISED: "1",
|
|
52
|
+
...(ident.slug ? { GROK_TG_NAME: ident.slug } : {}),
|
|
53
|
+
},
|
|
45
54
|
logsDir,
|
|
46
55
|
logFile: join(logsDir, "grok-telegram-bot.log"),
|
|
47
56
|
};
|
package/src/service/types.ts
CHANGED
|
@@ -7,6 +7,12 @@ export interface LaunchSpec {
|
|
|
7
7
|
id: string;
|
|
8
8
|
/** Human-readable name. */
|
|
9
9
|
displayName: string;
|
|
10
|
+
/** Windows Scheduled Task name (unique per named instance). */
|
|
11
|
+
windowsTaskName?: string;
|
|
12
|
+
/** launchd label (unique per named instance). */
|
|
13
|
+
macosLabel?: string;
|
|
14
|
+
/** Named instance slug when using `--name`. */
|
|
15
|
+
slug?: string;
|
|
10
16
|
/** Absolute path to the node binary that should run the bot. */
|
|
11
17
|
nodePath: string;
|
|
12
18
|
/** Arguments after the node binary (tsx loader + entry file). */
|
package/src/service/windows.ts
CHANGED
|
@@ -15,6 +15,14 @@ const TASK = "GrokTelegramBot";
|
|
|
15
15
|
/** Launcher dropped in the per-user Startup folder when no admin is available. */
|
|
16
16
|
const STARTUP_VBS = "GrokTelegramBot.vbs";
|
|
17
17
|
|
|
18
|
+
function taskName(spec?: { windowsTaskName?: string }): string {
|
|
19
|
+
return spec?.windowsTaskName || TASK;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function startupVbsFile(spec?: { slug?: string }): string {
|
|
23
|
+
return spec?.slug ? `GrokTelegramBot-${spec.slug}.vbs` : STARTUP_VBS;
|
|
24
|
+
}
|
|
25
|
+
|
|
18
26
|
/** The per-user Startup folder (runs at logon for the current user, no admin).
|
|
19
27
|
* Undefined only if APPDATA is unset (e.g. running with no roaming profile). */
|
|
20
28
|
function startupDir(): string | undefined {
|
|
@@ -22,9 +30,9 @@ function startupDir(): string | undefined {
|
|
|
22
30
|
return appData ? join(appData, "Microsoft", "Windows", "Start Menu", "Programs", "Startup") : undefined;
|
|
23
31
|
}
|
|
24
32
|
|
|
25
|
-
function startupVbsPath(): string | undefined {
|
|
33
|
+
function startupVbsPath(spec?: { slug?: string }): string | undefined {
|
|
26
34
|
const dir = startupDir();
|
|
27
|
-
return dir ? join(dir,
|
|
35
|
+
return dir ? join(dir, startupVbsFile(spec)) : undefined;
|
|
28
36
|
}
|
|
29
37
|
|
|
30
38
|
/** Remove a leftover Startup-folder launcher (e.g. from an earlier non-elevated
|
|
@@ -40,8 +48,8 @@ function vbsPath(spec: LaunchSpec): string {
|
|
|
40
48
|
}
|
|
41
49
|
|
|
42
50
|
/** True when our hidden Scheduled Task is registered. */
|
|
43
|
-
function taskInstalled(): boolean {
|
|
44
|
-
return runSafe("schtasks", ["/Query", "/TN",
|
|
51
|
+
function taskInstalled(spec?: { windowsTaskName?: string }): boolean {
|
|
52
|
+
return runSafe("schtasks", ["/Query", "/TN", taskName(spec)]).ok;
|
|
45
53
|
}
|
|
46
54
|
|
|
47
55
|
/** True when a bot process matching this spec is currently running. Launch
|
|
@@ -63,35 +71,36 @@ export const windowsController: ServiceController = {
|
|
|
63
71
|
// Preferred: a hidden ONLOGON Scheduled Task. Registering a *logon-triggered*
|
|
64
72
|
// task is a privileged operation, so /Create succeeds only from an elevated
|
|
65
73
|
// (admin) terminal. From a normal terminal it returns "Access is denied".
|
|
66
|
-
|
|
74
|
+
const tn = taskName(spec);
|
|
75
|
+
runSafe("schtasks", ["/Delete", "/F", "/TN", tn]); // replace if present
|
|
67
76
|
const res = runSafe("schtasks", [
|
|
68
77
|
"/Create",
|
|
69
78
|
"/F",
|
|
70
79
|
"/SC",
|
|
71
80
|
"ONLOGON",
|
|
72
81
|
"/TN",
|
|
73
|
-
|
|
82
|
+
tn,
|
|
74
83
|
"/TR",
|
|
75
84
|
`wscript.exe "${vbs}"`,
|
|
76
85
|
]);
|
|
77
86
|
if (res.ok) {
|
|
78
87
|
removeStartupLauncher(); // avoid a leftover launcher double-starting the bot
|
|
79
|
-
if (!isRunning(spec)) runSafe("schtasks", ["/Run", "/TN",
|
|
80
|
-
return ok(`Installed scheduled task "${
|
|
88
|
+
if (!isRunning(spec)) runSafe("schtasks", ["/Run", "/TN", tn]);
|
|
89
|
+
return ok(`Installed scheduled task "${tn}" (starts at logon) and launched it.`);
|
|
81
90
|
}
|
|
82
91
|
|
|
83
92
|
// A task may still exist that we just couldn't overwrite (e.g. created by an
|
|
84
93
|
// earlier elevated install). Reuse it rather than ALSO adding a Startup
|
|
85
94
|
// launcher, which would double-launch the bot at logon (409 Conflict).
|
|
86
|
-
if (taskInstalled()) {
|
|
95
|
+
if (taskInstalled(spec)) {
|
|
87
96
|
removeStartupLauncher();
|
|
88
|
-
if (!isRunning(spec)) runSafe("schtasks", ["/Run", "/TN",
|
|
89
|
-
return ok(`Scheduled task "${
|
|
97
|
+
if (!isRunning(spec)) runSafe("schtasks", ["/Run", "/TN", tn]);
|
|
98
|
+
return ok(`Scheduled task "${tn}" already exists; launched it. (Re-run elevated to recreate it.)`);
|
|
90
99
|
}
|
|
91
100
|
|
|
92
101
|
// Fallback (no admin — the common case): drop the launcher in the per-user
|
|
93
102
|
// Startup folder. It runs hidden at every logon with no elevation.
|
|
94
|
-
const startupVbs = startupVbsPath();
|
|
103
|
+
const startupVbs = startupVbsPath(spec);
|
|
95
104
|
const dir = startupDir();
|
|
96
105
|
if (!startupVbs || !dir) {
|
|
97
106
|
return fail(
|
|
@@ -118,21 +127,21 @@ export const windowsController: ServiceController = {
|
|
|
118
127
|
|
|
119
128
|
async uninstall(spec) {
|
|
120
129
|
await this.stop(spec);
|
|
121
|
-
runSafe("schtasks", ["/Delete", "/F", "/TN",
|
|
130
|
+
runSafe("schtasks", ["/Delete", "/F", "/TN", taskName(spec)]); // best-effort (may not exist)
|
|
122
131
|
rmSync(vbsPath(spec), { force: true });
|
|
123
|
-
const startupVbs = startupVbsPath();
|
|
132
|
+
const startupVbs = startupVbsPath(spec);
|
|
124
133
|
if (startupVbs) rmSync(startupVbs, { force: true });
|
|
125
|
-
return ok(`Removed "${
|
|
134
|
+
return ok(`Removed "${taskName(spec)}" (scheduled task and/or Startup launcher).`);
|
|
126
135
|
},
|
|
127
136
|
|
|
128
137
|
async start(spec) {
|
|
129
138
|
if (isRunning(spec)) return ok("Already running.");
|
|
130
|
-
if (taskInstalled()) {
|
|
139
|
+
if (taskInstalled(spec)) {
|
|
131
140
|
// schtasks /Run returns once the task is queued (does not wait for the bot).
|
|
132
|
-
const res = runSafe("schtasks", ["/Run", "/TN",
|
|
141
|
+
const res = runSafe("schtasks", ["/Run", "/TN", taskName(spec)]);
|
|
133
142
|
return res.ok ? ok("Started.") : fail(res.out);
|
|
134
143
|
}
|
|
135
|
-
const startupVbs = startupVbsPath();
|
|
144
|
+
const startupVbs = startupVbsPath(spec);
|
|
136
145
|
if (startupVbs && existsSync(startupVbs)) {
|
|
137
146
|
// Forever-restart VBS — must be detached or this CLI never returns.
|
|
138
147
|
const launched = launchDetached("wscript.exe", [startupVbs]);
|
|
@@ -148,20 +157,20 @@ export const windowsController: ServiceController = {
|
|
|
148
157
|
},
|
|
149
158
|
|
|
150
159
|
async stop(spec) {
|
|
151
|
-
runSafe("schtasks", ["/End", "/TN",
|
|
160
|
+
runSafe("schtasks", ["/End", "/TN", taskName(spec)]); // best-effort if task-based
|
|
152
161
|
const res = runSafe("powershell", ["-NoProfile", "-Command", killScript(entryOf(spec))]);
|
|
153
162
|
return ok(`Stopped. ${res.out.trim()}`);
|
|
154
163
|
},
|
|
155
164
|
|
|
156
165
|
async status(spec) {
|
|
157
|
-
const installedTask = taskInstalled();
|
|
158
|
-
const startupVbs = startupVbsPath();
|
|
166
|
+
const installedTask = taskInstalled(spec);
|
|
167
|
+
const startupVbs = startupVbsPath(spec);
|
|
159
168
|
const installedStartup = !!startupVbs && existsSync(startupVbs);
|
|
160
169
|
const installed = installedTask || installedStartup;
|
|
161
170
|
const running = isRunning(spec);
|
|
162
171
|
const how = installedTask ? "scheduled task" : installedStartup ? "Startup folder" : "—";
|
|
163
172
|
const detail = installedTask
|
|
164
|
-
? `\n${runSafe("schtasks", ["/Query", "/TN",
|
|
173
|
+
? `\n${runSafe("schtasks", ["/Query", "/TN", taskName(spec), "/FO", "LIST"]).out.trim()}`
|
|
165
174
|
: installedStartup
|
|
166
175
|
? `\nLauncher: ${startupVbs}`
|
|
167
176
|
: "";
|