-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathclaude-remote.js
More file actions
executable file
·1045 lines (893 loc) · 40.9 KB
/
Copy pathclaude-remote.js
File metadata and controls
executable file
·1045 lines (893 loc) · 40.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env node
/**
* Claude-Code-Remote - Claude Code Smart Notification System
* Main entry point for the CLI tool
*/
// Load environment variables from Claude-Code-Remote directory
const path = require('path');
const envPath = path.join(__dirname, '.env');
require('dotenv').config({ path: envPath });
const Logger = require('./src/core/logger');
const Notifier = require('./src/core/notifier');
const ConfigManager = require('./src/core/config');
class ClaudeCodeRemoteCLI {
constructor() {
this.logger = new Logger('CLI');
this.config = new ConfigManager();
this.notifier = new Notifier(this.config);
}
async init() {
// Load configuration
this.config.load();
// Initialize channels
await this.notifier.initializeChannels();
}
async run() {
const args = process.argv.slice(2);
const command = args[0];
try {
await this.init();
switch (command) {
case 'notify':
await this.handleNotify(args.slice(1));
break;
case 'test':
await this.handleTest(args.slice(1));
break;
case 'status':
await this.handleStatus(args.slice(1));
break;
case 'config':
await this.handleConfig(args.slice(1));
break;
case 'install':
await this.handleInstall(args.slice(1));
break;
case 'relay':
await this.handleRelay(args.slice(1));
break;
case 'edit-config':
await this.handleEditConfig(args.slice(1));
break;
case 'setup-email':
await this.handleSetupEmail(args.slice(1));
break;
case 'daemon':
await this.handleDaemon(args.slice(1));
break;
case 'commands':
await this.handleCommands(args.slice(1));
break;
case 'test-paste':
await this.handleTestPaste(args.slice(1));
break;
case 'test-simple':
await this.handleTestSimple(args.slice(1));
break;
case 'test-claude':
await this.handleTestClaude(args.slice(1));
break;
case 'setup-permissions':
await this.handleSetupPermissions(args.slice(1));
break;
case 'diagnose':
await this.handleDiagnose(args.slice(1));
break;
case '--help':
case '-h':
case undefined:
this.showHelp();
break;
default:
console.error(`Unknown command: ${command}`);
this.showHelp();
process.exit(1);
}
} catch (error) {
this.logger.error('CLI error:', error.message);
process.exit(1);
}
}
async handleNotify(args) {
const typeIndex = args.findIndex(arg => arg === '--type');
if (typeIndex === -1 || typeIndex + 1 >= args.length) {
console.error('Usage: claude-remote notify --type <completed|waiting>');
process.exit(1);
}
const type = args[typeIndex + 1];
if (!['completed', 'waiting'].includes(type)) {
console.error('Invalid type. Use: completed or waiting');
process.exit(1);
}
// Automatically capture current tmux session conversation content
const metadata = await this.captureCurrentConversation();
// Handle subagent notifications
if (type === 'waiting') {
const Config = require('./src/core/config');
const config = new Config();
config.load();
const enableSubagentNotifications = config.get('enableSubagentNotifications', false);
if (!enableSubagentNotifications) {
// Instead of skipping, track the subagent activity
const SubagentTracker = require('./src/utils/subagent-tracker');
const tracker = new SubagentTracker();
// Use tmux session as the tracking key
const trackingKey = metadata.tmuxSession || 'default';
// Capture more detailed information about the subagent activity
const activityDetails = {
userQuestion: metadata.userQuestion || 'No question captured',
claudeResponse: metadata.claudeResponse || 'No response captured',
timestamp: new Date().toISOString(),
tmuxSession: metadata.tmuxSession
};
// Don't truncate the response too aggressively
if (activityDetails.claudeResponse && activityDetails.claudeResponse.length > 1000) {
activityDetails.claudeResponse = activityDetails.claudeResponse.substring(0, 1000) + '...[see full output in tmux]';
}
tracker.addActivity(trackingKey, {
type: 'SubagentStop',
description: metadata.userQuestion || 'Subagent task',
details: activityDetails
});
this.logger.info(`Subagent activity tracked for tmux session: ${trackingKey}`);
process.exit(0);
}
}
// For completed notifications, include subagent activities and execution trace
if (type === 'completed') {
const Config = require('./src/core/config');
const config = new Config();
config.load();
const showSubagentActivitiesInEmail = config.get('showSubagentActivitiesInEmail', false);
if (showSubagentActivitiesInEmail) {
const SubagentTracker = require('./src/utils/subagent-tracker');
const tracker = new SubagentTracker();
const trackingKey = metadata.tmuxSession || 'default';
// Get and format subagent activities
const subagentSummary = tracker.formatActivitiesForEmail(trackingKey);
if (subagentSummary) {
metadata.subagentActivities = subagentSummary;
}
// Clear activities after including them in the notification
tracker.clearActivities(trackingKey);
} else {
// Always clear activities even if not showing them
const SubagentTracker = require('./src/utils/subagent-tracker');
const tracker = new SubagentTracker();
const trackingKey = metadata.tmuxSession || 'default';
tracker.clearActivities(trackingKey);
}
}
const result = await this.notifier.notify(type, metadata);
if (result.success) {
this.logger.info(`${type} notification sent successfully`);
process.exit(0);
} else {
this.logger.error(`Failed to send ${type} notification`);
process.exit(1);
}
}
async captureCurrentConversation() {
try {
const { execSync } = require('child_process');
const TmuxMonitor = require('./src/utils/tmux-monitor');
// Get current tmux session name
let currentSession = null;
try {
currentSession = execSync('tmux display-message -p "#S"', {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore']
}).trim();
} catch (e) {
// Not running in tmux, return empty metadata
return {};
}
if (!currentSession) {
return {};
}
// Use TmuxMonitor to capture conversation
const tmuxMonitor = new TmuxMonitor();
const conversation = tmuxMonitor.getRecentConversation(currentSession);
const fullTrace = tmuxMonitor.getFullExecutionTrace(currentSession);
return {
userQuestion: conversation.userQuestion,
claudeResponse: conversation.claudeResponse,
tmuxSession: currentSession,
fullExecutionTrace: fullTrace
};
} catch (error) {
this.logger.debug('Failed to capture conversation:', error.message);
return {};
}
}
async handleTest(args) {
console.log('Testing notification channels...\n');
const results = await this.notifier.test();
for (const [channel, result] of Object.entries(results)) {
const status = result.success ? '✅ PASS' : '❌ FAIL';
console.log(`${channel}: ${status}`);
if (result.error) {
console.log(` Error: ${result.error}`);
}
}
const passCount = Object.values(results).filter(r => r.success).length;
const totalCount = Object.keys(results).length;
console.log(`\nTest completed: ${passCount}/${totalCount} channels passed`);
if (passCount === 0) {
process.exit(1);
}
}
async handleStatus(args) {
const status = this.notifier.getStatus();
console.log('Claude-Code-Remote Status\n');
console.log('Configuration:');
console.log(` Enabled: ${status.enabled ? 'Yes' : 'No'}`);
console.log(` Language: ${status.config.language}`);
console.log(` Sounds: ${status.config.sound.completed} / ${status.config.sound.waiting}`);
console.log('\nChannels:');
// Display all available channels, including disabled ones
const allChannels = this.config._channels || {};
const activeChannels = status.channels || {};
// Merge all channel information
const channelNames = new Set([
...Object.keys(allChannels),
...Object.keys(activeChannels)
]);
for (const name of channelNames) {
const channelConfig = allChannels[name] || {};
const channelStatus = activeChannels[name];
let enabled, configured, relay;
if (channelStatus) {
// Active channel, use actual status
enabled = channelStatus.enabled ? '✅' : '❌';
configured = channelStatus.configured ? '✅' : '❌';
relay = channelStatus.supportsRelay ? '✅' : '❌';
} else {
// Inactive channel, use configuration status
enabled = channelConfig.enabled ? '✅' : '❌';
configured = this._isChannelConfigured(name, channelConfig) ? '✅' : '❌';
relay = this._supportsRelay(name) ? '✅' : '❌';
}
console.log(` ${name}:`);
console.log(` Enabled: ${enabled}`);
console.log(` Configured: ${configured}`);
console.log(` Supports Relay: ${relay}`);
}
}
_isChannelConfigured(name, config) {
switch (name) {
case 'desktop':
return true; // Desktop notifications don't need special configuration
case 'email':
return config.config &&
config.config.smtp &&
config.config.smtp.host &&
config.config.smtp.auth &&
config.config.smtp.auth.user &&
config.config.to;
default:
return false;
}
}
_supportsRelay(name) {
switch (name) {
case 'email':
return true;
case 'desktop':
default:
return false;
}
}
async handleConfig(args) {
// Launch the configuration tool
const ConfigTool = require('./src/tools/config-manager');
const configTool = new ConfigTool(this.config);
await configTool.run(args);
}
async handleInstall(args) {
// Launch the installer
const Installer = require('./src/tools/installer');
const installer = new Installer(this.config);
await installer.run(args);
}
async handleRelay(args) {
const subcommand = args[0];
switch (subcommand) {
case 'start':
await this.startRelay(args.slice(1));
break;
case 'stop':
await this.stopRelay(args.slice(1));
break;
case 'status':
await this.relayStatus(args.slice(1));
break;
case 'cleanup':
await this.cleanupRelay(args.slice(1));
break;
default:
console.error('Usage: claude-remote relay <start|stop|status|cleanup>');
console.log('');
console.log('Commands:');
console.log(' start Start email command relay service');
console.log(' stop Stop email command relay service');
console.log(' status View relay service status');
console.log(' cleanup Clean up completed command history');
process.exit(1);
}
}
async startRelay(args) {
try {
const CommandRelayService = require('./src/relay/command-relay');
const emailConfig = this.config.getChannel('email');
if (!emailConfig || !emailConfig.enabled) {
console.error('❌ Email channel not configured or disabled');
console.log('Please run first: claude-remote config');
process.exit(1);
}
console.log('🚀 Starting email command relay service...');
const relayService = new CommandRelayService(emailConfig.config);
// Listen for events
relayService.on('started', () => {
console.log('✅ Command relay service started');
console.log('📧 Listening for email replies...');
console.log('💡 You can now remotely execute Claude Code commands by replying to emails');
console.log('');
console.log('Press Ctrl+C to stop the service');
});
relayService.on('commandQueued', (command) => {
console.log(`📨 Received new command: ${command.command.substring(0, 50)}...`);
});
relayService.on('commandExecuted', (command) => {
console.log(`✅ Command executed successfully: ${command.id}`);
});
relayService.on('commandFailed', (command, error) => {
console.log(`❌ Command execution failed: ${command.id} - ${error.message}`);
});
// Handle graceful shutdown
process.on('SIGINT', async () => {
console.log('\n🛑 Stopping command relay service...');
await relayService.stop();
console.log('✅ Service stopped');
process.exit(0);
});
// Start service
await relayService.start();
// Keep process running
process.stdin.resume();
} catch (error) {
console.error('❌ Failed to start relay service:', error.message);
process.exit(1);
}
}
async stopRelay(args) {
console.log('💡 Command relay service usually stopped with Ctrl+C');
console.log('If the service is still running, please find the corresponding process and terminate it manually');
}
async relayStatus(args) {
try {
const fs = require('fs');
const path = require('path');
const stateFile = path.join(__dirname, 'src/data/relay-state.json');
console.log('📊 Command relay service status\n');
// Check email configuration
const emailConfig = this.config.getChannel('email');
if (!emailConfig || !emailConfig.enabled) {
console.log('❌ Email channel not configured');
return;
}
console.log('✅ Email configuration enabled');
console.log(`📧 SMTP: ${emailConfig.config.smtp.host}:${emailConfig.config.smtp.port}`);
console.log(`📥 IMAP: ${emailConfig.config.imap.host}:${emailConfig.config.imap.port}`);
console.log(`📬 Recipient: ${emailConfig.config.to}`);
// Check relay status
if (fs.existsSync(stateFile)) {
const state = JSON.parse(fs.readFileSync(stateFile, 'utf8'));
console.log(`\n📋 Command queue: ${state.commandQueue?.length || 0} commands`);
if (state.commandQueue && state.commandQueue.length > 0) {
console.log('\nRecent commands:');
state.commandQueue.slice(-5).forEach(cmd => {
const status = cmd.status === 'completed' ? '✅' :
cmd.status === 'failed' ? '❌' :
cmd.status === 'executing' ? '⏳' : '⏸️';
console.log(` ${status} ${cmd.id}: ${cmd.command.substring(0, 50)}...`);
});
}
} else {
console.log('\n📋 No command history found');
}
} catch (error) {
console.error('❌ Failed to get status:', error.message);
}
}
async cleanupRelay(args) {
try {
const fs = require('fs');
const path = require('path');
const stateFile = path.join(__dirname, 'src/data/relay-state.json');
if (!fs.existsSync(stateFile)) {
console.log('📋 No cleanup needed, no command history found');
return;
}
const state = JSON.parse(fs.readFileSync(stateFile, 'utf8'));
const beforeCount = state.commandQueue?.length || 0;
// Clean up completed commands (keep those within 24 hours)
const cutoff = new Date(Date.now() - 24 * 60 * 60 * 1000);
state.commandQueue = (state.commandQueue || []).filter(cmd =>
cmd.status !== 'completed' ||
new Date(cmd.completedAt || cmd.queuedAt) > cutoff
);
const afterCount = state.commandQueue.length;
const removedCount = beforeCount - afterCount;
fs.writeFileSync(stateFile, JSON.stringify(state, null, 2));
console.log(`🧹 Cleanup completed: removed ${removedCount} completed commands`);
console.log(`📋 ${afterCount} commands remaining in queue`);
} catch (error) {
console.error('❌ Cleanup failed:', error.message);
}
}
async handleEditConfig(args) {
const { spawn } = require('child_process');
const path = require('path');
const configType = args[0];
if (!configType) {
console.log('Available configuration files:');
console.log(' user - User personal configuration (config/user.json)');
console.log(' channels - Notification channel configuration (config/channels.json)');
console.log(' default - Default configuration template (config/default.json)');
console.log('');
console.log('Usage: claude-remote edit-config <configuration-type>');
console.log('Example: claude-remote edit-config channels');
return;
}
const configFiles = {
'user': path.join(__dirname, 'config/user.json'),
'channels': path.join(__dirname, 'config/channels.json'),
'default': path.join(__dirname, 'config/default.json')
};
const configFile = configFiles[configType];
if (!configFile) {
console.error('❌ Invalid configuration type:', configType);
console.log('Available types: user, channels, default');
return;
}
// Check if file exists
const fs = require('fs');
if (!fs.existsSync(configFile)) {
console.error('❌ Configuration file does not exist:', configFile);
return;
}
console.log(`📝 Opening configuration file: ${configFile}`);
console.log('💡 Save and close the editor after editing to take effect');
console.log('');
// Determine the editor to use
const editor = process.env.EDITOR || process.env.VISUAL || this._getDefaultEditor();
try {
const editorProcess = spawn(editor, [configFile], {
stdio: 'inherit'
});
editorProcess.on('close', (code) => {
if (code === 0) {
console.log('✅ Configuration file saved');
console.log('💡 Run "claude-remote status" to view updated configuration');
} else {
console.log('❌ Editor exited abnormally');
}
});
editorProcess.on('error', (error) => {
console.error('❌ Unable to start editor:', error.message);
console.log('');
console.log('💡 You can manually edit the configuration file:');
console.log(` ${configFile}`);
});
} catch (error) {
console.error('❌ Failed to start editor:', error.message);
console.log('');
console.log('💡 You can manually edit the configuration file:');
console.log(` ${configFile}`);
}
}
_getDefaultEditor() {
// Determine default editor based on platform
if (process.platform === 'win32') {
return 'notepad';
} else if (process.platform === 'darwin') {
return 'nano'; // Use nano on macOS as most users have it
} else {
return 'nano'; // Linux default to nano
}
}
async handleSetupEmail(args) {
const readline = require('readline');
const fs = require('fs');
const path = require('path');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const question = (prompt) => {
return new Promise((resolve) => {
rl.question(prompt, resolve);
});
};
try {
console.log('🚀 Claude-Code-Remote Email Quick Setup Wizard\n');
// Select email provider
console.log('Please select your email provider:');
console.log('1. Gmail');
console.log('2. QQ Email');
console.log('3. 163 Email');
console.log('4. Outlook/Hotmail');
console.log('5. Custom');
const providerChoice = await question('\nPlease select (1-5): ');
let smtpHost, smtpPort, imapHost, imapPort, secure;
switch (providerChoice) {
case '1':
smtpHost = 'smtp.gmail.com';
smtpPort = 587;
imapHost = 'imap.gmail.com';
imapPort = 993;
secure = false;
console.log('\n📧 Gmail Configuration');
console.log('💡 Need to enable two-factor authentication and generate app password first');
break;
case '2':
smtpHost = 'smtp.qq.com';
smtpPort = 587;
imapHost = 'imap.qq.com';
imapPort = 993;
secure = false;
console.log('\n📧 QQ Email Configuration');
break;
case '3':
smtpHost = 'smtp.163.com';
smtpPort = 587;
imapHost = 'imap.163.com';
imapPort = 993;
secure = false;
console.log('\n📧 163 Email Configuration');
break;
case '4':
smtpHost = 'smtp.live.com';
smtpPort = 587;
imapHost = 'imap-mail.outlook.com';
imapPort = 993;
secure = false;
console.log('\n📧 Outlook Configuration');
break;
case '5':
console.log('\n📧 Custom Configuration');
smtpHost = await question('SMTP Host: ');
smtpPort = parseInt(await question('SMTP Port (default 587): ') || '587');
imapHost = await question('IMAP Host: ');
imapPort = parseInt(await question('IMAP Port (default 993): ') || '993');
const secureInput = await question('Use SSL/TLS? (y/n): ');
secure = secureInput.toLowerCase() === 'y';
break;
default:
console.log('❌ Invalid selection');
rl.close();
return;
}
// Get email account information
console.log('\n📝 Please enter email account information:');
const email = await question('Email address: ');
const password = await question('Password/App password: ');
// Build configuration
const emailConfig = {
type: "email",
enabled: true,
config: {
smtp: {
host: smtpHost,
port: smtpPort,
secure: secure,
auth: {
user: email,
pass: password
}
},
imap: {
host: imapHost,
port: imapPort,
secure: true,
auth: {
user: email,
pass: password
}
},
from: `Claude-Code-Remote <${email}>`,
to: email,
template: {
checkInterval: 30
}
}
};
// Read existing configuration
const channelsFile = path.join(__dirname, 'config/channels.json');
let channels = {};
if (fs.existsSync(channelsFile)) {
channels = JSON.parse(fs.readFileSync(channelsFile, 'utf8'));
}
// Update email configuration
channels.email = emailConfig;
// Save configuration
fs.writeFileSync(channelsFile, JSON.stringify(channels, null, 2));
console.log('\n✅ Email configuration saved!');
console.log('\n🧪 You can now test email functionality:');
console.log(' claude-remote test');
console.log('\n🚀 Start command relay service:');
console.log(' claude-remote relay start');
// Ask if user wants to test immediately
const testNow = await question('\nTest email sending now? (y/n): ');
if (testNow.toLowerCase() === 'y') {
rl.close();
// Reload configuration and test
await this.init();
await this.handleTest([]);
} else {
rl.close();
}
} catch (error) {
console.error('❌ Configuration failed:', error.message);
rl.close();
}
}
async handleDaemon(args) {
const ClaudeCodeRemoteDaemon = require('./src/daemon/taskping-daemon');
const daemon = new ClaudeCodeRemoteDaemon();
const command = args[0];
switch (command) {
case 'start':
await daemon.start();
break;
case 'stop':
await daemon.stop();
break;
case 'restart':
await daemon.restart();
break;
case 'status':
daemon.showStatus();
break;
default:
console.log('Usage: claude-remote daemon <start|stop|restart|status>');
console.log('');
console.log('Commands:');
console.log(' start Start background daemon process');
console.log(' stop Stop background daemon process');
console.log(' restart Restart background daemon process');
console.log(' status View daemon process status');
break;
}
}
async handleCommands(args) {
const ClaudeCommandBridge = require('./src/relay/claude-command-bridge');
const bridge = new ClaudeCommandBridge();
const command = args[0];
switch (command) {
case 'list':
const pending = bridge.getPendingCommands();
console.log(`📋 Pending commands: ${pending.length}\n`);
if (pending.length > 0) {
pending.forEach((cmd, index) => {
console.log(`${index + 1}. ${cmd.id}`);
console.log(` Command: ${cmd.command}`);
console.log(` Time: ${cmd.timestamp}`);
console.log(` Session: ${cmd.sessionId}`);
console.log('');
});
}
break;
case 'status':
const status = bridge.getStatus();
console.log('📊 Command bridge status\n');
console.log(`Pending commands: ${status.pendingCommands}`);
console.log(`Processed commands: ${status.processedCommands}`);
console.log(`Commands directory: ${status.commandsDir}`);
console.log(`Response directory: ${status.responseDir}`);
if (status.recentCommands.length > 0) {
console.log('\nRecent commands:');
status.recentCommands.forEach(cmd => {
console.log(` • ${cmd.command} (${cmd.timestamp})`);
});
}
break;
case 'cleanup':
bridge.cleanup();
console.log('🧹 Old command files cleaned up');
break;
case 'clear':
const pending2 = bridge.getPendingCommands();
for (const cmd of pending2) {
bridge.markCommandProcessed(cmd.id, 'cancelled', 'Manually cancelled');
}
console.log(`🗑️ Cleared ${pending2.length} pending commands`);
break;
default:
console.log('Usage: claude-remote commands <list|status|cleanup|clear>');
console.log('');
console.log('Commands:');
console.log(' list Show pending email commands');
console.log(' status Show command bridge status');
console.log(' cleanup Clean up old command files');
console.log(' clear Clear all pending commands');
break;
}
}
async handleTestPaste(args) {
const ClipboardAutomation = require('./src/automation/clipboard-automation');
const automation = new ClipboardAutomation();
const testCommand = args.join(' ') || 'echo "Testing email reply auto-paste functionality"';
console.log('🧪 Testing auto-paste functionality');
console.log(`📝 Test command: ${testCommand}`);
console.log('\n⚠️ Please ensure Claude Code or Terminal window is open and active');
console.log('⏳ Command will be sent automatically in 3 seconds...\n');
// Countdown
for (let i = 3; i > 0; i--) {
process.stdout.write(`${i}... `);
await new Promise(resolve => setTimeout(resolve, 1000));
}
console.log('\n');
try {
const success = await automation.sendCommand(testCommand);
if (success) {
console.log('✅ Command has been auto-pasted!');
console.log('💡 If you don\'t see the effect, please check app permissions and window status');
} else {
console.log('❌ Auto-paste failed');
console.log('💡 Please ensure automation permissions are granted and target app is open');
}
} catch (error) {
console.error('❌ Test failed:', error.message);
}
}
async handleSetupPermissions(args) {
const PermissionSetup = require('./setup-permissions');
const setup = new PermissionSetup();
await setup.checkAndSetup();
}
async handleTestSimple(args) {
const SimpleAutomation = require('./src/automation/simple-automation');
const automation = new SimpleAutomation();
const testCommand = args.join(' ') || 'echo "Testing simple automation functionality"';
console.log('🧪 Testing simple automation functionality');
console.log(`📝 Test command: ${testCommand}`);
console.log('\nThis test will:');
console.log('1. 📋 Copy command to clipboard');
console.log('2. 📄 Save command to file');
console.log('3. 🔔 Send notification (including dialog box)');
console.log('4. 🤖 Attempt auto-paste (if permissions granted)');
console.log('\n⏳ Starting test...\n');
try {
const success = await automation.sendCommand(testCommand, 'test-session');
if (success) {
console.log('✅ Test successful!');
console.log('\n📋 Next steps:');
console.log('1. Check if you received notification');
console.log('2. Check if command was copied to clipboard');
console.log('3. If you see dialog box, you can choose to open command file');
console.log('4. Manually paste to Claude Code (if auto-paste didn\'t work)');
const status = automation.getStatus();
console.log(`\n📄 Command file: ${status.commandFile}`);
if (status.commandFileExists) {
console.log('💡 You can run "open -t ' + status.commandFile + '" to view command file');
}
} else {
console.log('❌ Test failed');
}
} catch (error) {
console.error('❌ Error occurred during test:', error.message);
}
}
async handleTestClaude(args) {
const ClaudeAutomation = require('./src/automation/claude-automation');
const automation = new ClaudeAutomation();
const testCommand = args.join(' ') || 'echo "This is an automated test command from email reply"';
console.log('🤖 Testing Claude Code specialized automation');
console.log(`📝 Test command: ${testCommand}`);
console.log('\n⚠️ Please ensure:');
console.log(' 1. Claude Code application is open');
console.log(' 2. Or Terminal/iTerm2 etc. terminal applications are open');
console.log(' 3. Necessary accessibility permissions have been granted');
console.log('\n⏳ Full automation test will start in 5 seconds...\n');
// Countdown
for (let i = 5; i > 0; i--) {
process.stdout.write(`${i}... `);
await new Promise(resolve => setTimeout(resolve, 1000));
}
console.log('\n🚀 Starting automation...\n');
try {
// Check permissions
const hasPermission = await automation.requestPermissions();
if (!hasPermission) {
console.log('⚠️ Permission check failed, but will still attempt execution...');
}
// Execute full automation
const success = await automation.sendCommand(testCommand, 'test-session');
if (success) {
console.log('✅ Full automation test successful!');
console.log('💡 Command should have been automatically input to Claude Code and started execution');
console.log('🔍 Please check Claude Code window to see if command was received');
} else {
console.log('❌ Automation test failed');
console.log('💡 Possible reasons:');
console.log(' • Claude Code or terminal application not found');
console.log(' • Insufficient permissions');
console.log(' • Application not responding');
console.log('\n🔧 Suggestions:');
console.log(' 1. Run "claude-remote setup-permissions" to check permissions');
console.log(' 2. Ensure Claude Code is running in foreground');
console.log(' 3. Try manually clicking input box in Claude Code first');
}
} catch (error) {
console.error('❌ Error occurred during test:', error.message);
}
}
async handleDiagnose(args) {
const AutomationDiagnostic = require('./diagnose-automation');
const diagnostic = new AutomationDiagnostic();
await diagnostic.runDiagnostic();
}
showHelp() {
console.log(`
Claude-Code-Remote - Claude Code Smart Notification System
Usage: claude-remote <command> [options]
Commands:
notify --type <type> Send a notification (completed|waiting)
test Test all notification channels
status Show system status
config Launch configuration manager
setup-email Quick email setup wizard
edit-config <type> Edit configuration files directly
install Install and configure Claude Code hooks
relay <subcommand> Manage email command relay service
daemon <subcommand> Manage background daemon service
commands <subcommand> Manage email commands and bridge
test-paste [command] Test automatic paste functionality
test-simple [command] Test simple automation (recommended)
test-claude [command] Test Claude Code full automation
setup-permissions Setup macOS permissions for automation
diagnose Diagnose automation issues
Options:
-h, --help Show this help message
Relay Subcommands: