@moltnet/guard 1.0.3 → 1.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.
Files changed (2) hide show
  1. package/index.ts +71 -19
  2. package/package.json +1 -1
package/index.ts CHANGED
@@ -9,10 +9,14 @@
9
9
  */
10
10
 
11
11
  import type { PluginAPI, PluginRegisterFn } from 'clawdbot';
12
+ import { randomBytes } from 'crypto';
13
+ import { readFileSync, writeFileSync, existsSync } from 'fs';
14
+ import { join } from 'path';
15
+ import { homedir } from 'os';
12
16
 
13
17
  interface MoltGuardConfig {
14
18
  url: string;
15
- token: string;
19
+ token?: string;
16
20
  agentName?: string;
17
21
  logThoughts?: boolean;
18
22
  gateHighRisk?: boolean;
@@ -35,6 +39,53 @@ const state: MoltGuardState = {
35
39
  pollInterval: null,
36
40
  };
37
41
 
42
+ // Generate a new token
43
+ function generateToken(): string {
44
+ return 'mg_' + randomBytes(24).toString('hex');
45
+ }
46
+
47
+ // Auto-generate and save token if not present
48
+ function ensureToken(api: PluginAPI): string | null {
49
+ const configPath = join(homedir(), '.clawdbot', 'clawdbot.json');
50
+
51
+ try {
52
+ if (!existsSync(configPath)) {
53
+ console.log('[moltguard] Config file not found');
54
+ return null;
55
+ }
56
+
57
+ const config = JSON.parse(readFileSync(configPath, 'utf8'));
58
+ const guardConfig = config.plugins?.entries?.guard?.config;
59
+
60
+ // If token exists, use it
61
+ if (guardConfig?.token) {
62
+ return guardConfig.token;
63
+ }
64
+
65
+ // Generate new token
66
+ const newToken = generateToken();
67
+ console.log('[moltguard] Generated new token: ' + newToken);
68
+
69
+ // Ensure config structure exists
70
+ if (!config.plugins) config.plugins = {};
71
+ if (!config.plugins.entries) config.plugins.entries = {};
72
+ if (!config.plugins.entries.guard) config.plugins.entries.guard = { enabled: true };
73
+ if (!config.plugins.entries.guard.config) config.plugins.entries.guard.config = {};
74
+
75
+ // Save token
76
+ config.plugins.entries.guard.config.token = newToken;
77
+ writeFileSync(configPath, JSON.stringify(config, null, 2));
78
+
79
+ console.log('[moltguard] Token saved to config');
80
+ console.log('[moltguard] View dashboard: https://guard.moltnet.ai/dashboard?token=' + newToken);
81
+
82
+ return newToken;
83
+ } catch (err) {
84
+ console.error('[moltguard] Failed to manage token:', err instanceof Error ? err.message : String(err));
85
+ return null;
86
+ }
87
+ }
88
+
38
89
  // Risk classification for common tools
39
90
  const TOOL_RISK: Record<string, 'low' | 'medium' | 'high' | 'critical'> = {
40
91
  // High risk - external effects
@@ -251,22 +302,23 @@ function getToolRisk(toolName: string, params: Record<string, unknown>): 'low' |
251
302
  const register: PluginRegisterFn = (api: PluginAPI) => {
252
303
  const config = api.config.plugins?.entries?.guard?.config as MoltGuardConfig | undefined;
253
304
 
254
- if (!config?.token) {
255
- console.log('[moltguard] No token configured.');
256
- console.log('[moltguard] 👉 Get your token: https://guard.moltnet.ai/signup');
257
- console.log('[moltguard] Then add to config: plugins.entries.guard.config.token');
305
+ // Auto-generate token if not present
306
+ const token = config?.token || ensureToken(api);
307
+
308
+ if (!token) {
309
+ console.log('[moltguard] Failed to initialize token');
258
310
  return;
259
311
  }
260
312
 
261
313
  const cfg: MoltGuardConfig = {
262
- url: config.url || 'https://guard.moltnet.ai',
263
- token: config.token,
264
- agentName: config.agentName,
265
- logThoughts: config.logThoughts ?? true,
266
- gateHighRisk: config.gateHighRisk ?? true,
267
- gateTools: config.gateTools ?? ['exec', 'message', 'write'],
268
- pollCommands: config.pollCommands ?? true,
269
- pollIntervalMs: config.pollIntervalMs ?? 5000,
314
+ url: config?.url || 'https://guard.moltnet.ai',
315
+ token: token,
316
+ agentName: config?.agentName,
317
+ logThoughts: config?.logThoughts ?? true,
318
+ gateHighRisk: config?.gateHighRisk ?? true,
319
+ gateTools: config?.gateTools ?? ['exec', 'message', 'write'],
320
+ pollCommands: config?.pollCommands ?? true,
321
+ pollIntervalMs: config?.pollIntervalMs ?? 5000,
270
322
  };
271
323
 
272
324
  const agentName = cfg.agentName || `clawdbot-${process.env.HOSTNAME || 'agent'}`;
@@ -291,7 +343,7 @@ const register: PluginRegisterFn = (api: PluginAPI) => {
291
343
  })();
292
344
 
293
345
  // Hook: Intercept tool results before persistence
294
- api.hooks?.register('tool_result_persist', (toolResult) => {
346
+ api.on('tool_result_persist', (toolResult) => {
295
347
  if (!toolResult || typeof toolResult !== 'object') return toolResult;
296
348
 
297
349
  const { name, input } = toolResult as { name?: string; input?: Record<string, unknown> };
@@ -310,17 +362,17 @@ const register: PluginRegisterFn = (api: PluginAPI) => {
310
362
  });
311
363
 
312
364
  // Hook: Log agent bootstrap (session start)
313
- api.hooks?.register('agent:bootstrap', (event) => {
365
+ api.on('agent:bootstrap', (event) => {
314
366
  if (cfg.logThoughts) {
315
367
  logTrace(cfg, agentName, state.sessionId, 'session', 'Session Started', 'Agent bootstrap initiated')
316
368
  .catch(() => {});
317
369
  }
318
370
  });
319
371
 
320
- // Hook: Log commands
321
- api.hooks?.register('command', (event) => {
322
- if (cfg.logThoughts && event.action) {
323
- logTrace(cfg, agentName, state.sessionId, 'command', `Command: /${event.action}`, `User issued /${event.action}`)
372
+ // Hook: Log commands
373
+ api.on('command', (event) => {
374
+ if (cfg.logThoughts && (event as any).action) {
375
+ logTrace(cfg, agentName, state.sessionId, 'command', `Command: /${(event as any).action}`, `User issued /${(event as any).action}`)
324
376
  .catch(() => {});
325
377
  }
326
378
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moltnet/guard",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "MoltGuard plugin for Clawdbot - Security & observability for AI agents",
5
5
  "main": "index.ts",
6
6
  "type": "module",