@colinlu50/openclaw-lark-stream 260323.3.0 → 260327.2.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.
@@ -1,259 +1,297 @@
1
- #!/usr/bin/env node
2
-
3
- import { execSync } from "node:child_process";
4
- import { createInterface } from "node:readline";
5
- import { existsSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
6
- import { join } from "node:path";
7
-
8
- const SELF_PACKAGE = "@colinlu50/openclaw-lark-stream";
9
- const STATE_DIR = process.env.OPENCLAW_STATE_DIR || join(process.env.HOME || process.env.USERPROFILE || "", ".openclaw");
10
- const EXTENSIONS_DIR = join(STATE_DIR, "extensions");
11
- const CONFIG_FILE = join(STATE_DIR, "openclaw.json");
12
- const OFFICIAL_DIR = join(EXTENSIONS_DIR, "openclaw-lark");
13
- const SELF_DIR = join(EXTENSIONS_DIR, "openclaw-lark-stream");
14
-
15
- const args = process.argv.slice(2);
16
- const subcommand = args[0];
17
-
18
- // ── install / update ──
19
- if (subcommand === "install" || subcommand === "update") {
20
- await runInstall();
21
- process.exit(0);
22
- }
23
-
24
- // ── All other commands: show help ──
25
- console.log(`Usage: npx ${SELF_PACKAGE} install`);
26
- console.log(` npx ${SELF_PACKAGE} update`);
27
- process.exit(0);
28
-
29
- // ---------------------------------------------------------------------------
30
- // Install flow
31
- // ---------------------------------------------------------------------------
32
-
33
- async function runInstall() {
34
- // 1. Version check
35
- checkOpenClawVersion();
36
-
37
- // 2. Clean stale state
38
- cleanPluginState();
39
-
40
- // 3. Install our plugin
41
- console.log(`\nInstalling ${SELF_PACKAGE}...`);
42
- try {
43
- execSync(`openclaw plugins install ${SELF_PACKAGE}`, { stdio: "inherit" });
44
- } catch (error) {
45
- console.error(`\n❌ Failed to install ${SELF_PACKAGE}.`);
46
- console.error(error.message || error);
47
- console.error(`\nYou can retry with: openclaw plugins install ${SELF_PACKAGE}`);
48
- process.exit(error.status ?? 1);
49
- }
50
- console.log(`\n✅ Plugin installed successfully.`);
51
-
52
- // 4. Ensure plugins.allow includes our plugin ID
53
- ensurePluginAllowed("openclaw-lark-stream");
54
-
55
- // 5. Bot configuration (interactive)
56
- await configureBotIfNeeded();
57
-
58
- // 6. Restart gateway
59
- console.log("\nRestarting gateway...");
60
- try {
61
- execSync("openclaw gateway restart", { stdio: "inherit" });
62
- } catch {
63
- console.log("Gateway restart failed. You can manually run: openclaw gateway restart");
64
- }
65
-
66
- console.log("\n🎉 All done!");
67
- }
68
-
69
- // ---------------------------------------------------------------------------
70
- // Version check
71
- // ---------------------------------------------------------------------------
72
-
73
- function checkOpenClawVersion() {
74
- try {
75
- const ver = execSync("openclaw -v", { encoding: "utf8" }).trim();
76
- console.log(`OpenClaw version: ${ver}`);
77
- if (!ver.includes("2026.3.13")) {
78
- console.warn(`\n⚠️ This plugin is tested with OpenClaw 2026.3.13.`);
79
- console.warn(` Current version: ${ver}`);
80
- console.warn(` To install the compatible version: npm install -g openclaw@2026.3.13\n`);
81
- }
82
- } catch {
83
- console.error("❌ OpenClaw not found. Install it first: npm install -g openclaw@2026.3.13");
84
- process.exit(1);
85
- }
86
- }
87
-
88
- // ---------------------------------------------------------------------------
89
- // Bot configuration
90
- // ---------------------------------------------------------------------------
91
-
92
- async function configureBotIfNeeded() {
93
- const cfg = readConfig();
94
- const existing = cfg.channels?.feishu;
95
-
96
- if (existing?.appId) {
97
- console.log(`\nFound existing bot config (App ID: ${existing.appId}).`);
98
- const reuse = await ask("Use existing bot config? (Y/n): ");
99
- if (reuse.toLowerCase() !== "n") {
100
- // Keep bot credentials but ensure streaming is enabled
101
- ensureStreamingConfig(cfg);
102
- return;
103
- }
104
- }
105
-
106
- console.log("\n── Feishu Bot Setup ──");
107
- console.log("You need a Feishu bot app. Create one at: https://open.feishu.cn/app\n");
108
-
109
- const appId = await ask("App ID: ");
110
- const appSecret = await ask("App Secret: ");
111
-
112
- if (!appId || !appSecret) {
113
- console.log("Skipped. You can configure manually in ~/.openclaw/openclaw.json");
114
- return;
115
- }
116
-
117
- // Ask for domain
118
- const domainChoice = await ask("Domain - feishu or lark? (feishu): ");
119
- const domain = domainChoice === "lark" ? "lark" : "feishu";
120
-
121
- // Write config
122
- if (!cfg.channels) cfg.channels = {};
123
- cfg.channels.feishu = {
124
- ...(cfg.channels.feishu || {}),
125
- enabled: true,
126
- appId,
127
- appSecret,
128
- connectionMode: "websocket",
129
- domain,
130
- streaming: true,
131
- defaultAccount: "main",
132
- replyMode: {
133
- direct: "streaming",
134
- group: "streaming",
135
- default: "streaming",
136
- },
137
- accounts: {
138
- ...(cfg.channels?.feishu?.accounts || {}),
139
- main: { appId, appSecret },
140
- },
141
- dmPolicy: cfg.channels?.feishu?.dmPolicy || "pairing",
142
- groupPolicy: cfg.channels?.feishu?.groupPolicy || "open",
143
- };
144
-
145
- writeFileSync(CONFIG_FILE, JSON.stringify(cfg, null, 2) + "\n", "utf8");
146
- console.log(`\n✅ Bot configured (App ID: ${appId}).`);
147
- }
148
-
149
- // ---------------------------------------------------------------------------
150
- // Helpers
151
- // ---------------------------------------------------------------------------
152
-
153
- /**
154
- * Ensure streaming is enabled on an existing config.
155
- */
156
- function ensureStreamingConfig(cfg) {
157
- if (!cfg.channels?.feishu) return;
158
- let changed = false;
159
- if (!cfg.channels.feishu.streaming) {
160
- cfg.channels.feishu.streaming = true;
161
- changed = true;
162
- }
163
- const rm = cfg.channels.feishu.replyMode || {};
164
- if (rm.direct !== "streaming" || rm.group !== "streaming" || rm.default !== "streaming") {
165
- cfg.channels.feishu.replyMode = { direct: "streaming", group: "streaming", default: "streaming" };
166
- changed = true;
167
- }
168
- if (changed) {
169
- writeFileSync(CONFIG_FILE, JSON.stringify(cfg, null, 2) + "\n", "utf8");
170
- console.log("Enabled streaming mode for all reply modes.");
171
- }
172
- }
173
-
174
- function ask(prompt) {
175
- const rl = createInterface({ input: process.stdin, output: process.stdout });
176
- return new Promise((resolve) => {
177
- rl.question(prompt, (answer) => {
178
- rl.close();
179
- resolve(answer.trim());
180
- });
181
- });
182
- }
183
-
184
- function readConfig() {
185
- if (!existsSync(CONFIG_FILE)) return {};
186
- try {
187
- return JSON.parse(readFileSync(CONFIG_FILE, "utf8"));
188
- } catch {
189
- return {};
190
- }
191
- }
192
-
193
- /**
194
- * Remove all plugin directories, staging leftovers, and stale config
195
- * references so that openclaw has a clean state for the next install.
196
- */
197
- /**
198
- * Ensure the plugin ID is in plugins.allow so openclaw doesn't warn.
199
- */
200
- function ensurePluginAllowed(pluginId) {
201
- const cfg = readConfig();
202
- if (!cfg.plugins) cfg.plugins = {};
203
- if (!Array.isArray(cfg.plugins.allow)) cfg.plugins.allow = [];
204
- if (!cfg.plugins.allow.includes(pluginId)) {
205
- cfg.plugins.allow.push(pluginId);
206
- writeFileSync(CONFIG_FILE, JSON.stringify(cfg, null, 2) + "\n", "utf8");
207
- console.log(`Added "${pluginId}" to plugins.allow.`);
208
- }
209
- }
210
-
211
- function cleanPluginState() {
212
- for (const dir of [OFFICIAL_DIR, SELF_DIR]) {
213
- if (existsSync(dir)) {
214
- console.log(`Removing ${dir}...`);
215
- rmSync(dir, { recursive: true, force: true });
216
- }
217
- }
218
- // Remove leftover staging directories (.openclaw-install-stage-*)
219
- if (existsSync(EXTENSIONS_DIR)) {
220
- try {
221
- for (const entry of readdirSync(EXTENSIONS_DIR)) {
222
- if (entry.startsWith(".openclaw-install-stage-")) {
223
- const p = join(EXTENSIONS_DIR, entry);
224
- console.log(`Removing staging dir ${p}...`);
225
- rmSync(p, { recursive: true, force: true });
226
- }
227
- }
228
- } catch { /* ignore */ }
229
- }
230
- cleanConfigReferences("openclaw-lark");
231
- cleanConfigReferences("openclaw-lark-stream");
232
- }
233
-
234
- function cleanConfigReferences(pluginId) {
235
- if (!existsSync(CONFIG_FILE)) return;
236
- try {
237
- const cfg = JSON.parse(readFileSync(CONFIG_FILE, "utf8"));
238
- let changed = false;
239
- if (cfg.plugins?.entries?.[pluginId]) {
240
- delete cfg.plugins.entries[pluginId];
241
- changed = true;
242
- }
243
- if (cfg.plugins?.installs?.[pluginId]) {
244
- delete cfg.plugins.installs[pluginId];
245
- changed = true;
246
- }
247
- if (Array.isArray(cfg.plugins?.allow)) {
248
- const idx = cfg.plugins.allow.indexOf(pluginId);
249
- if (idx !== -1) {
250
- cfg.plugins.allow.splice(idx, 1);
251
- changed = true;
252
- }
253
- }
254
- if (changed) {
255
- writeFileSync(CONFIG_FILE, JSON.stringify(cfg, null, 2) + "\n", "utf8");
256
- console.log(`Cleaned "${pluginId}" references from config.`);
257
- }
258
- } catch { /* ignore */ }
259
- }
1
+ #!/usr/bin/env node
2
+
3
+ import { execSync } from "node:child_process";
4
+ import { createInterface } from "node:readline";
5
+ import { existsSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
6
+ import { join } from "node:path";
7
+
8
+ const SELF_PACKAGE = "@colinlu50/openclaw-lark-stream";
9
+ // Version-gated npm specs:
10
+ // - OpenClaw >= 2026.3.22 → latest build (new SDK)
11
+ // - OpenClaw < 2026.3.22 → pinned old build (legacy SDK)
12
+ const PACKAGE_NEW = "@colinlu50/openclaw-lark-stream"; // >= 2026.3.22
13
+ const PACKAGE_OLD = "@colinlu50/openclaw-lark-stream@260323.3.0"; // < 2026.3.22
14
+ const OPENCLAW_BREAKPOINT = { year: 2026, month: 3, day: 22 };
15
+
16
+ const STATE_DIR = process.env.OPENCLAW_STATE_DIR || join(process.env.HOME || process.env.USERPROFILE || "", ".openclaw");
17
+ const EXTENSIONS_DIR = join(STATE_DIR, "extensions");
18
+ const CONFIG_FILE = join(STATE_DIR, "openclaw.json");
19
+ const OFFICIAL_DIR = join(EXTENSIONS_DIR, "openclaw-lark");
20
+ const SELF_DIR = join(EXTENSIONS_DIR, "openclaw-lark-stream");
21
+
22
+ const args = process.argv.slice(2);
23
+ const subcommand = args[0];
24
+
25
+ // ── install / update ──
26
+ if (subcommand === "install" || subcommand === "update") {
27
+ await runInstall();
28
+ process.exit(0);
29
+ }
30
+
31
+ // ── All other commands: show help ──
32
+ console.log(`Usage: npx ${SELF_PACKAGE} install`);
33
+ console.log(` npx ${SELF_PACKAGE} update`);
34
+ process.exit(0);
35
+
36
+ // ---------------------------------------------------------------------------
37
+ // Install flow
38
+ // ---------------------------------------------------------------------------
39
+
40
+ async function runInstall() {
41
+ // 1. Version check — determines which package version to install
42
+ const npmSpec = resolveNpmSpec();
43
+
44
+ // 2. Clean stale state
45
+ cleanPluginState();
46
+
47
+ // 3. Install the appropriate plugin version
48
+ console.log(`\nInstalling ${npmSpec}...`);
49
+ try {
50
+ execSync(`openclaw plugins install ${npmSpec}`, { stdio: "inherit" });
51
+ } catch (error) {
52
+ console.error(`\n❌ Failed to install ${npmSpec}.`);
53
+ console.error(error.message || error);
54
+ console.error(`\nYou can retry with: openclaw plugins install ${npmSpec}`);
55
+ process.exit(error.status ?? 1);
56
+ }
57
+ console.log(`\n✅ Plugin installed successfully.`);
58
+
59
+ // 4. Ensure plugins.allow includes our plugin ID
60
+ ensurePluginAllowed("openclaw-lark-stream");
61
+
62
+ // 5. Bot configuration (interactive)
63
+ await configureBotIfNeeded();
64
+
65
+ // 6. Restart gateway
66
+ console.log("\nRestarting gateway...");
67
+ try {
68
+ execSync("openclaw gateway restart", { stdio: "inherit" });
69
+ } catch {
70
+ console.log("Gateway restart failed. You can manually run: openclaw gateway restart");
71
+ }
72
+
73
+ console.log("\n🎉 All done!");
74
+ }
75
+
76
+ // ---------------------------------------------------------------------------
77
+ // Version detection — returns the correct npm spec to install
78
+ // ---------------------------------------------------------------------------
79
+
80
+ /**
81
+ * Detect the installed OpenClaw version and return the correct npm spec:
82
+ * - >= 2026.3.22 → latest package (new SDK)
83
+ * - < 2026.3.22 pinned legacy package
84
+ */
85
+ function resolveNpmSpec() {
86
+ let verString;
87
+ try {
88
+ verString = execSync("openclaw -v", { encoding: "utf8" }).trim();
89
+ console.log(`OpenClaw version: ${verString}`);
90
+ } catch {
91
+ console.error("❌ OpenClaw not found. Install it first: npm install -g openclaw");
92
+ process.exit(1);
93
+ }
94
+
95
+ const ver = parseOpenClawVersion(verString);
96
+ if (!ver) {
97
+ console.warn(`⚠️ Could not parse OpenClaw version "${verString}", installing latest.`);
98
+ return PACKAGE_NEW;
99
+ }
100
+
101
+ const isNew = isVersionAtLeast(ver, OPENCLAW_BREAKPOINT);
102
+ if (isNew) {
103
+ console.log(`✅ OpenClaw >= 2026.3.22 detected — installing latest plugin version.`);
104
+ return PACKAGE_NEW;
105
+ } else {
106
+ console.log(`ℹ️ OpenClaw < 2026.3.22 detected — installing legacy plugin version.`);
107
+ console.log(` (To use the latest plugin, upgrade OpenClaw: npm install -g openclaw)`);
108
+ return PACKAGE_OLD;
109
+ }
110
+ }
111
+
112
+ /** Parse "YYYY.M.D" or "OpenClaw YYYY.M.D ..." into { year, month, day }. */
113
+ function parseOpenClawVersion(str) {
114
+ const m = /(\d{4})\.(\d+)\.(\d+)/.exec(str);
115
+ if (!m) return null;
116
+ return { year: parseInt(m[1], 10), month: parseInt(m[2], 10), day: parseInt(m[3], 10) };
117
+ }
118
+
119
+ /** True if ver >= { year, month, day }. */
120
+ function isVersionAtLeast(ver, min) {
121
+ if (ver.year !== min.year) return ver.year > min.year;
122
+ if (ver.month !== min.month) return ver.month > min.month;
123
+ return ver.day >= min.day;
124
+ }
125
+
126
+ // ---------------------------------------------------------------------------
127
+ // Bot configuration
128
+ // ---------------------------------------------------------------------------
129
+
130
+ async function configureBotIfNeeded() {
131
+ const cfg = readConfig();
132
+ const existing = cfg.channels?.feishu;
133
+
134
+ if (existing?.appId) {
135
+ console.log(`\nFound existing bot config (App ID: ${existing.appId}).`);
136
+ const reuse = await ask("Use existing bot config? (Y/n): ");
137
+ if (reuse.toLowerCase() !== "n") {
138
+ // Keep bot credentials but ensure streaming is enabled
139
+ ensureStreamingConfig(cfg);
140
+ return;
141
+ }
142
+ }
143
+
144
+ console.log("\n── Feishu Bot Setup ──");
145
+ console.log("You need a Feishu bot app. Create one at: https://open.feishu.cn/app\n");
146
+
147
+ const appId = await ask("App ID: ");
148
+ const appSecret = await ask("App Secret: ");
149
+
150
+ if (!appId || !appSecret) {
151
+ console.log("Skipped. You can configure manually in ~/.openclaw/openclaw.json");
152
+ return;
153
+ }
154
+
155
+ // Ask for domain
156
+ const domainChoice = await ask("Domain - feishu or lark? (feishu): ");
157
+ const domain = domainChoice === "lark" ? "lark" : "feishu";
158
+
159
+ // Write config
160
+ if (!cfg.channels) cfg.channels = {};
161
+ cfg.channels.feishu = {
162
+ ...(cfg.channels.feishu || {}),
163
+ enabled: true,
164
+ appId,
165
+ appSecret,
166
+ connectionMode: "websocket",
167
+ domain,
168
+ streaming: true,
169
+ defaultAccount: "main",
170
+ replyMode: {
171
+ direct: "streaming",
172
+ group: "streaming",
173
+ default: "streaming",
174
+ },
175
+ accounts: {
176
+ ...(cfg.channels?.feishu?.accounts || {}),
177
+ main: { appId, appSecret },
178
+ },
179
+ dmPolicy: cfg.channels?.feishu?.dmPolicy || "pairing",
180
+ groupPolicy: cfg.channels?.feishu?.groupPolicy || "open",
181
+ };
182
+
183
+ writeFileSync(CONFIG_FILE, JSON.stringify(cfg, null, 2) + "\n", "utf8");
184
+ console.log(`\n✅ Bot configured (App ID: ${appId}).`);
185
+ }
186
+
187
+ // ---------------------------------------------------------------------------
188
+ // Helpers
189
+ // ---------------------------------------------------------------------------
190
+
191
+ /**
192
+ * Ensure streaming is enabled on an existing config.
193
+ */
194
+ function ensureStreamingConfig(cfg) {
195
+ if (!cfg.channels?.feishu) return;
196
+ let changed = false;
197
+ if (!cfg.channels.feishu.streaming) {
198
+ cfg.channels.feishu.streaming = true;
199
+ changed = true;
200
+ }
201
+ const rm = cfg.channels.feishu.replyMode || {};
202
+ if (rm.direct !== "streaming" || rm.group !== "streaming" || rm.default !== "streaming") {
203
+ cfg.channels.feishu.replyMode = { direct: "streaming", group: "streaming", default: "streaming" };
204
+ changed = true;
205
+ }
206
+ if (changed) {
207
+ writeFileSync(CONFIG_FILE, JSON.stringify(cfg, null, 2) + "\n", "utf8");
208
+ console.log("Enabled streaming mode for all reply modes.");
209
+ }
210
+ }
211
+
212
+ function ask(prompt) {
213
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
214
+ return new Promise((resolve) => {
215
+ rl.question(prompt, (answer) => {
216
+ rl.close();
217
+ resolve(answer.trim());
218
+ });
219
+ });
220
+ }
221
+
222
+ function readConfig() {
223
+ if (!existsSync(CONFIG_FILE)) return {};
224
+ try {
225
+ return JSON.parse(readFileSync(CONFIG_FILE, "utf8"));
226
+ } catch {
227
+ return {};
228
+ }
229
+ }
230
+
231
+ /**
232
+ * Remove all plugin directories, staging leftovers, and stale config
233
+ * references so that openclaw has a clean state for the next install.
234
+ */
235
+ /**
236
+ * Ensure the plugin ID is in plugins.allow so openclaw doesn't warn.
237
+ */
238
+ function ensurePluginAllowed(pluginId) {
239
+ const cfg = readConfig();
240
+ if (!cfg.plugins) cfg.plugins = {};
241
+ if (!Array.isArray(cfg.plugins.allow)) cfg.plugins.allow = [];
242
+ if (!cfg.plugins.allow.includes(pluginId)) {
243
+ cfg.plugins.allow.push(pluginId);
244
+ writeFileSync(CONFIG_FILE, JSON.stringify(cfg, null, 2) + "\n", "utf8");
245
+ console.log(`Added "${pluginId}" to plugins.allow.`);
246
+ }
247
+ }
248
+
249
+ function cleanPluginState() {
250
+ for (const dir of [OFFICIAL_DIR, SELF_DIR]) {
251
+ if (existsSync(dir)) {
252
+ console.log(`Removing ${dir}...`);
253
+ rmSync(dir, { recursive: true, force: true });
254
+ }
255
+ }
256
+ // Remove leftover staging directories (.openclaw-install-stage-*)
257
+ if (existsSync(EXTENSIONS_DIR)) {
258
+ try {
259
+ for (const entry of readdirSync(EXTENSIONS_DIR)) {
260
+ if (entry.startsWith(".openclaw-install-stage-")) {
261
+ const p = join(EXTENSIONS_DIR, entry);
262
+ console.log(`Removing staging dir ${p}...`);
263
+ rmSync(p, { recursive: true, force: true });
264
+ }
265
+ }
266
+ } catch { /* ignore */ }
267
+ }
268
+ cleanConfigReferences("openclaw-lark");
269
+ cleanConfigReferences("openclaw-lark-stream");
270
+ }
271
+
272
+ function cleanConfigReferences(pluginId) {
273
+ if (!existsSync(CONFIG_FILE)) return;
274
+ try {
275
+ const cfg = JSON.parse(readFileSync(CONFIG_FILE, "utf8"));
276
+ let changed = false;
277
+ if (cfg.plugins?.entries?.[pluginId]) {
278
+ delete cfg.plugins.entries[pluginId];
279
+ changed = true;
280
+ }
281
+ if (cfg.plugins?.installs?.[pluginId]) {
282
+ delete cfg.plugins.installs[pluginId];
283
+ changed = true;
284
+ }
285
+ if (Array.isArray(cfg.plugins?.allow)) {
286
+ const idx = cfg.plugins.allow.indexOf(pluginId);
287
+ if (idx !== -1) {
288
+ cfg.plugins.allow.splice(idx, 1);
289
+ changed = true;
290
+ }
291
+ }
292
+ if (changed) {
293
+ writeFileSync(CONFIG_FILE, JSON.stringify(cfg, null, 2) + "\n", "utf8");
294
+ console.log(`Cleaned "${pluginId}" references from config.`);
295
+ }
296
+ } catch { /* ignore */ }
297
+ }