macroclaw 0.3.0 → 0.4.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/src/setup.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { execSync } from "node:child_process";
1
2
  import { Bot } from "grammy";
2
3
  import { createLogger } from "./logger";
3
4
  import { maskValue, type Settings, settingsSchema } from "./settings";
@@ -7,6 +8,7 @@ const log = createLogger("setup");
7
8
  export interface SetupIO {
8
9
  ask: (question: string) => Promise<string>;
9
10
  write: (msg: string) => void;
11
+ close?: () => void;
10
12
  }
11
13
 
12
14
  async function startSetupBot(token: string): Promise<Bot> {
@@ -20,19 +22,42 @@ async function startSetupBot(token: string): Promise<Bot> {
20
22
 
21
23
  await bot.init();
22
24
  await bot.api.setMyCommands([{ command: "chatid", description: "Get your chat ID" }]);
23
- log.info({ username: bot.botInfo.username }, "Setup bot started");
24
-
25
25
  bot.start();
26
26
  return bot;
27
27
  }
28
28
 
29
- export async function runSetupWizard(io: SetupIO): Promise<Settings> {
29
+ export interface ServiceInstaller {
30
+ install: (oauthToken?: string) => string;
31
+ }
32
+
33
+ export interface SetupDefaults {
34
+ botToken?: string;
35
+ chatId?: string;
36
+ model?: string;
37
+ workspace?: string;
38
+ openaiApiKey?: string;
39
+ }
40
+
41
+ export function resolveClaudePath(exec: (cmd: string) => string = (cmd) => execSync(cmd, { encoding: "utf-8" })): string {
42
+ try {
43
+ return exec("which claude").trim();
44
+ } catch {
45
+ throw new Error("Claude Code CLI not found. Install it first: https://docs.anthropic.com/en/docs/claude-code");
46
+ }
47
+ }
48
+
49
+ export async function runSetupWizard(io: SetupIO, opts?: { defaults?: SetupDefaults; serviceInstaller?: ServiceInstaller; onSettingsReady?: (settings: Settings) => void; resolveClaude?: () => string; platform?: string }): Promise<Settings> {
30
50
  const { ask, write } = io;
51
+ const prev = opts?.defaults ?? {};
52
+
53
+ // Fail fast if claude CLI is not installed
54
+ const resolve = opts?.resolveClaude ?? resolveClaudePath;
55
+ resolve();
31
56
 
32
57
  write("\n=== Macroclaw Setup ===\n\n");
33
58
 
34
59
  // Bot token
35
- const defaultToken = process.env.TELEGRAM_BOT_TOKEN || "";
60
+ const defaultToken = prev.botToken || process.env.TELEGRAM_BOT_TOKEN || "";
36
61
  const tokenPrompt = defaultToken ? `Bot token [${maskValue("botToken", defaultToken)}]: ` : "Bot token: ";
37
62
  let botToken = await ask(tokenPrompt) || defaultToken;
38
63
 
@@ -54,7 +79,7 @@ export async function runSetupWizard(io: SetupIO): Promise<Settings> {
54
79
  }
55
80
 
56
81
  // Chat ID
57
- const defaultChatId = process.env.AUTHORIZED_CHAT_ID || "";
82
+ const defaultChatId = prev.chatId || process.env.AUTHORIZED_CHAT_ID || "";
58
83
  const chatIdPrompt = defaultChatId ? `Chat ID [${defaultChatId}]: ` : "Chat ID: ";
59
84
  let chatId = await ask(chatIdPrompt) || defaultChatId;
60
85
  while (!chatId) {
@@ -67,15 +92,15 @@ export async function runSetupWizard(io: SetupIO): Promise<Settings> {
67
92
  }
68
93
 
69
94
  // Model
70
- const defaultModel = process.env.MODEL || "sonnet";
95
+ const defaultModel = prev.model || process.env.MODEL || "sonnet";
71
96
  const model = await ask(`Model [${defaultModel}]: `) || defaultModel;
72
97
 
73
98
  // Workspace
74
- const defaultWorkspace = process.env.WORKSPACE || "~/.macroclaw-workspace";
99
+ const defaultWorkspace = prev.workspace || process.env.WORKSPACE || "~/.macroclaw-workspace";
75
100
  const workspace = await ask(`Workspace [${defaultWorkspace}]: `) || defaultWorkspace;
76
101
 
77
102
  // OpenAI API key
78
- const defaultOpenai = process.env.OPENAI_API_KEY || "";
103
+ const defaultOpenai = prev.openaiApiKey || process.env.OPENAI_API_KEY || "";
79
104
  const openaiPrompt = defaultOpenai ? `OpenAI API key [${maskValue("openaiApiKey", defaultOpenai)}] (optional): ` : "OpenAI API key (optional): ";
80
105
  const openaiApiKey = await ask(openaiPrompt) || defaultOpenai || undefined;
81
106
 
@@ -88,7 +113,37 @@ export async function runSetupWizard(io: SetupIO): Promise<Settings> {
88
113
  logLevel: "debug",
89
114
  });
90
115
 
116
+ // Persist settings before service install prompt so ServiceManager can find them
117
+ opts?.onSettingsReady?.(settings);
118
+
91
119
  write("\nSetup complete!\n\n");
92
120
 
121
+ // Optional service installation
122
+ const installAnswer = await ask("Install as a system service? [Y/n]: ");
123
+ let oauthToken: string | undefined;
124
+ if (installAnswer.toLowerCase() !== "n" && installAnswer.toLowerCase() !== "no") {
125
+ if ((opts?.platform ?? process.platform) === "darwin") {
126
+ write("\nmacOS requires a long-lived OAuth token for the service.\n");
127
+ write("Run `claude setup-token` in another terminal, then paste the token here.\n\n");
128
+ oauthToken = await ask("OAuth token: ");
129
+ if (!oauthToken) {
130
+ write("No token provided. Skipping service installation.\n");
131
+ io.close?.();
132
+ return settings;
133
+ }
134
+ }
135
+ }
136
+ // Release terminal control before sudo may prompt for a password
137
+ io.close?.();
138
+ if (installAnswer.toLowerCase() !== "n" && installAnswer.toLowerCase() !== "no") {
139
+ try {
140
+ const svc = opts?.serviceInstaller ?? new (await import("./service")).ServiceManager();
141
+ const logCmd = svc.install(oauthToken);
142
+ write(`Service installed and started. Check logs:\n ${logCmd}\n`);
143
+ } catch (err) {
144
+ write(`Service installation failed: ${(err as Error).message}\n`);
145
+ }
146
+ }
147
+
93
148
  return settings;
94
149
  }