-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli.ts
More file actions
1181 lines (1032 loc) · 36 KB
/
Copy pathcli.ts
File metadata and controls
1181 lines (1032 loc) · 36 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 Sync CLI
*
* Commands:
* login - Configure Convex URL and API Key
* logout - Clear stored credentials
* status - Show connection status
* config - Show current configuration
*/
import { Command } from "commander";
import * as readline from "readline";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
import {
loadConfig,
saveConfig,
clearConfig,
SyncClient,
Config,
SessionData,
MessageData,
} from "./index";
// Types for Claude Code hook event data from stdin
interface HookSessionStartData {
session_id: string;
transcript_path?: string;
cwd?: string;
model?: string;
permission_mode?: string;
source?: string;
thinking_enabled?: boolean;
mcp_servers?: string[];
}
interface HookSessionEndData {
session_id: string;
transcript_path?: string;
cwd?: string;
reason?: string;
message_count?: number;
tool_call_count?: number;
total_token_usage?: {
input: number;
output: number;
};
cost_estimate?: number;
}
interface HookUserPromptData {
session_id: string;
prompt: string;
timestamp?: string;
}
interface HookToolUseData {
session_id: string;
tool_name: string;
tool_use_id?: string;
tool_input?: Record<string, unknown>;
tool_result?: {
output?: string;
error?: string;
};
duration_ms?: number;
success?: boolean;
}
interface HookStopData {
session_id: string;
transcript_path?: string;
stop_hook_active?: boolean;
permission_mode?: string;
cwd?: string;
hook_event_name?: string;
}
// Types for Claude Code transcript JSONL entries
interface TranscriptUsage {
input_tokens?: number;
output_tokens?: number;
cache_read_input_tokens?: number;
cache_creation_input_tokens?: number;
}
interface TranscriptContentPart {
type: "text" | "thinking" | "tool_use" | "tool_result";
text?: string;
thinking?: string;
id?: string;
name?: string;
input?: Record<string, unknown>;
}
interface TranscriptMessage {
content?: TranscriptContentPart[];
usage?: TranscriptUsage;
model?: string;
stop_reason?: string | null;
}
interface TranscriptEntry {
type?: string;
uuid?: string;
timestamp?: string;
message?: TranscriptMessage;
sessionId?: string;
parentUuid?: string;
cwd?: string;
slug?: string;
}
// Extended stats from transcript parsing (includes cache tokens and duration)
interface TranscriptStats {
model: string | undefined;
inputTokens: number;
cacheCreationTokens: number;
cacheReadTokens: number;
outputTokens: number;
messageCount: number;
toolCallCount: number;
title: string | undefined;
cwd: string | undefined;
startedAt: string | undefined;
endedAt: string | undefined;
durationMs: number | undefined;
}
// Pricing per million tokens (USD) with cache pricing
// Source: https://www.anthropic.com/pricing
const MODEL_PRICING: Record<string, { input: number; output: number; cacheWrite: number; cacheRead: number }> = {
"claude-sonnet-4-20250514": { input: 3.00, output: 15.00, cacheWrite: 3.75, cacheRead: 0.30 },
"claude-opus-4-20250514": { input: 15.00, output: 75.00, cacheWrite: 18.75, cacheRead: 1.50 },
"claude-opus-4-5-20251101": { input: 15.00, output: 75.00, cacheWrite: 18.75, cacheRead: 1.50 },
"claude-3-5-sonnet-20241022": { input: 3.00, output: 15.00, cacheWrite: 3.75, cacheRead: 0.30 },
"claude-3-opus-20240229": { input: 15.00, output: 75.00, cacheWrite: 18.75, cacheRead: 1.50 },
"claude-3-5-haiku-20241022": { input: 0.80, output: 4.00, cacheWrite: 1.00, cacheRead: 0.08 },
};
// Calculate cost from model and token usage with proper cache pricing
function calculateCost(model: string | undefined, stats: TranscriptStats): number {
if (!model) return 0;
// Try exact match first
let pricing = MODEL_PRICING[model];
// Try partial match if exact match fails
if (!pricing) {
const matchingKey = Object.keys(MODEL_PRICING).find(k => model.includes(k) || k.includes(model));
if (matchingKey) {
pricing = MODEL_PRICING[matchingKey];
}
}
if (!pricing) return 0;
// Calculate cost with proper cache pricing
const inputCost = stats.inputTokens * pricing.input;
const cacheWriteCost = stats.cacheCreationTokens * pricing.cacheWrite;
const cacheReadCost = stats.cacheReadTokens * pricing.cacheRead;
const outputCost = stats.outputTokens * pricing.output;
return (inputCost + cacheWriteCost + cacheReadCost + outputCost) / 1_000_000;
}
// Types for Claude Code settings.json
interface ClaudeSettings {
hooks?: Record<string, unknown>;
[key: string]: unknown;
}
// Type for package.json version field
interface PackageJson {
version?: string;
}
// Read version from package.json
function getVersion(): string {
try {
const pkgPath = path.join(__dirname, "..", "package.json");
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8")) as PackageJson;
return pkg.version || "0.0.0";
} catch {
return "0.0.0";
}
}
const program = new Command();
program
.name("claude-code-sync")
.description("Sync Claude Code sessions to OpenSync dashboard")
.version(getVersion());
// ============================================================================
// Helper Functions
// ============================================================================
function prompt(question: string): Promise<string> {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rl.question(question, (answer) => {
rl.close();
resolve(answer.trim());
});
});
}
function maskApiKey(key: string): string {
if (key.length <= 8) return "****";
return key.substring(0, 4) + "****" + key.substring(key.length - 4);
}
// ============================================================================
// Commands
// ============================================================================
program
.command("login")
.description("Configure Convex URL and API Key")
.action(async () => {
console.log("\n Claude Code Sync - Login\n");
console.log("Get your API key from your OpenSync.dev Settings page, starts with osk_. Enter it here:");
console.log(" 1. Go to Settings");
console.log(" 2. Click 'Generate API Key'");
console.log(" 3. Copy the key (starts with osk_)\n");
const convexUrl = await prompt("Convex URL (e.g., https://your-project.convex.cloud): ");
if (!convexUrl) {
console.error("Error: Convex URL is required");
process.exit(1);
}
if (!convexUrl.includes("convex.cloud") && !convexUrl.includes("convex.site")) {
console.error("Error: Invalid Convex URL. Must contain convex.cloud or convex.site");
process.exit(1);
}
const apiKey = await prompt("API Key (osk_...): ");
if (!apiKey) {
console.error("Error: API Key is required");
process.exit(1);
}
if (!apiKey.startsWith("osk_")) {
console.error("Error: Invalid API Key. Must start with osk_");
process.exit(1);
}
const config: Config = {
convexUrl,
apiKey,
autoSync: true,
syncToolCalls: true,
syncThinking: false,
};
// Test connection
console.log("\nTesting connection...");
const client = new SyncClient(config);
const connected = await client.testConnection();
if (!connected) {
console.error("Error: Could not connect to Convex backend");
console.error(" Check your URL and try again");
process.exit(1);
}
// Save config
saveConfig(config);
console.log("\nConfiguration saved!");
console.log(` URL: ${convexUrl}`);
console.log(` Key: ${maskApiKey(apiKey)}`);
console.log("\nNext step: Run the setup command to configure Claude Code hooks:\n");
console.log(" claude-code-sync setup\n");
});
program
.command("logout")
.description("Clear stored credentials")
.action(() => {
clearConfig();
console.log("Credentials cleared");
});
program
.command("status")
.description("Show connection status")
.action(async () => {
const config = loadConfig();
console.log("\n Claude Code Sync - Status\n");
if (!config) {
console.log("Not configured");
console.log(" Run 'claude-code-sync login' to set up\n");
process.exit(1);
}
console.log("Configuration:");
console.log(` Convex URL: ${config.convexUrl}`);
console.log(` API Key: ${maskApiKey(config.apiKey)}`);
console.log(` Auto Sync: ${config.autoSync !== false ? "enabled" : "disabled"}`);
console.log(` Tool Calls: ${config.syncToolCalls !== false ? "enabled" : "disabled"}`);
console.log(` Thinking: ${config.syncThinking ? "enabled" : "disabled"}`);
console.log("\nTesting connection...");
const client = new SyncClient(config);
const connected = await client.testConnection();
if (connected) {
console.log("Connected to Convex backend\n");
} else {
console.log("Error: Could not connect to Convex backend\n");
process.exit(1);
}
});
program
.command("config")
.description("Show current configuration")
.option("--json", "Output as JSON")
.action((options: { json?: boolean }) => {
const config = loadConfig();
if (!config) {
if (options.json) {
console.log(JSON.stringify({ configured: false }));
} else {
console.log("Not configured. Run 'claude-code-sync login' to set up.");
}
return;
}
if (options.json) {
console.log(
JSON.stringify(
{
configured: true,
convexUrl: config.convexUrl,
apiKey: maskApiKey(config.apiKey),
autoSync: config.autoSync !== false,
syncToolCalls: config.syncToolCalls !== false,
syncThinking: config.syncThinking === true,
},
null,
2
)
);
} else {
console.log("\n Current Configuration\n");
console.log(`Convex URL: ${config.convexUrl}`);
console.log(`API Key: ${maskApiKey(config.apiKey)}`);
console.log(`Auto Sync: ${config.autoSync !== false}`);
console.log(`Tool Calls: ${config.syncToolCalls !== false}`);
console.log(`Thinking: ${config.syncThinking === true}`);
console.log(`\nConfig file: ~/.config/claude-code-sync/config.json\n`);
}
});
program
.command("set <key> <value>")
.description("Set a configuration value")
.action((key: string, value: string) => {
const config = loadConfig();
if (!config) {
console.error("Not configured. Run 'claude-code-sync login' first.");
process.exit(1);
}
const validKeys = ["autoSync", "syncToolCalls", "syncThinking"];
if (!validKeys.includes(key)) {
console.error(`Invalid key. Valid keys: ${validKeys.join(", ")}`);
process.exit(1);
}
const boolValue = value === "true" || value === "1" || value === "yes";
// Type-safe config update
if (key === "autoSync") {
config.autoSync = boolValue;
} else if (key === "syncToolCalls") {
config.syncToolCalls = boolValue;
} else if (key === "syncThinking") {
config.syncThinking = boolValue;
}
saveConfig(config);
console.log(`Set ${key} = ${boolValue}`);
});
// ============================================================================
// Setup Command (configures Claude Code hooks)
// ============================================================================
// Claude Code hooks configuration
const CLAUDE_HOOKS_CONFIG = {
hooks: {
SessionStart: [
{
hooks: [
{
type: "command",
command: "claude-code-sync hook SessionStart",
},
],
},
],
SessionEnd: [
{
hooks: [
{
type: "command",
command: "claude-code-sync hook SessionEnd",
},
],
},
],
UserPromptSubmit: [
{
hooks: [
{
type: "command",
command: "claude-code-sync hook UserPromptSubmit",
},
],
},
],
PostToolUse: [
{
matcher: "*",
hooks: [
{
type: "command",
command: "claude-code-sync hook PostToolUse",
},
],
},
],
Stop: [
{
matcher: "*",
hooks: [
{
type: "command",
command: "claude-code-sync hook Stop",
},
],
},
],
},
};
program
.command("setup")
.description("Add hooks to Claude Code settings (configures ~/.claude/settings.json)")
.option("--force", "Overwrite existing hooks configuration")
.action(async (options: { force?: boolean }) => {
const claudeDir = path.join(os.homedir(), ".claude");
const settingsPath = path.join(claudeDir, "settings.json");
console.log("\n Claude Code Sync - Setup\n");
// Check if plugin credentials are configured
const config = loadConfig();
if (!config) {
console.log("Warning: Plugin not configured yet.");
console.log(" Run 'claude-code-sync login' first to set up credentials.\n");
}
// Create .claude directory if needed
if (!fs.existsSync(claudeDir)) {
fs.mkdirSync(claudeDir, { recursive: true });
console.log("Created ~/.claude directory");
}
// Check for existing settings
let existingSettings: ClaudeSettings = {};
let hasExistingHooks = false;
if (fs.existsSync(settingsPath)) {
try {
const content = fs.readFileSync(settingsPath, "utf-8");
existingSettings = JSON.parse(content) as ClaudeSettings;
hasExistingHooks = !!existingSettings.hooks;
console.log("Found existing settings.json");
} catch {
console.log("Warning: Could not parse existing settings.json, will create new one");
}
}
// Handle existing hooks
if (hasExistingHooks && !options.force) {
console.log("\nExisting hooks configuration found.");
console.log(" Use --force to overwrite, or manually merge the hooks.\n");
console.log("To manually add, include these hooks in your settings.json:");
console.log(JSON.stringify(CLAUDE_HOOKS_CONFIG, null, 2));
process.exit(1);
}
// Merge settings
const newSettings = {
...existingSettings,
...CLAUDE_HOOKS_CONFIG,
};
// Write settings
try {
fs.writeFileSync(settingsPath, JSON.stringify(newSettings, null, 2));
console.log("\nClaude Code hooks configured!");
console.log(` Settings file: ${settingsPath}`);
console.log("\nSetup complete. Sessions will sync automatically.\n");
} catch (error) {
console.error("Error writing settings:", error);
process.exit(1);
}
});
program
.command("verify")
.description("Verify credentials and Claude Code configuration")
.action(async () => {
console.log("\n OpenSync Setup Verification\n");
// Check credentials
const config = loadConfig();
if (config) {
console.log("Credentials: OK");
console.log(` Convex URL: ${config.convexUrl}`);
console.log(` API Key: ${maskApiKey(config.apiKey)}`);
} else {
console.log("Credentials: NOT CONFIGURED");
console.log(" Run 'claude-code-sync login' to set up");
}
// Check Claude Code config
const settingsPath = path.join(os.homedir(), ".claude", "settings.json");
let hooksConfigured = false;
if (fs.existsSync(settingsPath)) {
try {
const content = fs.readFileSync(settingsPath, "utf-8");
const settings = JSON.parse(content) as ClaudeSettings;
hooksConfigured = !!settings.hooks?.SessionStart;
} catch {
// Ignore parse errors
}
}
console.log("");
if (hooksConfigured) {
console.log("Claude Code Config: OK");
console.log(` Config file: ${settingsPath}`);
console.log(" Hooks registered: claude-code-sync");
} else {
console.log("Claude Code Config: NOT CONFIGURED");
console.log(" Run 'claude-code-sync setup' to configure hooks");
}
// Test connection if credentials exist
if (config) {
console.log("\nTesting connection...");
const client = new SyncClient(config);
const connected = await client.testConnection();
if (connected) {
console.log("Connection: OK\n");
} else {
console.log("Connection: FAILED\n");
process.exit(1);
}
}
if (config && hooksConfigured) {
console.log("Ready! Start Claude Code and sessions will sync automatically.\n");
} else {
console.log("");
process.exit(1);
}
});
// ============================================================================
// Sync Test Command (test connectivity)
// ============================================================================
program
.command("synctest")
.description("Test connectivity and create a test session")
.action(async () => {
const config = loadConfig();
console.log("\n Claude Code Sync - Connection Test\n");
if (!config) {
console.log("Not configured");
console.log(" Run 'claude-code-sync login' to set up\n");
process.exit(1);
}
console.log("Configuration:");
console.log(` Convex URL: ${config.convexUrl}`);
console.log(` API Key: ${maskApiKey(config.apiKey)}`);
console.log("\nTesting connection...");
const client = new SyncClient(config);
const connected = await client.testConnection();
if (connected) {
console.log("Connection: OK");
// Create a test session to verify full sync works
console.log("\nCreating test session...");
try {
const testSession = {
sessionId: `test-${Date.now()}`,
source: "claude-code" as const,
title: "Connection Test",
projectName: "synctest",
startedAt: new Date().toISOString(),
endedAt: new Date().toISOString(),
};
await client.syncSession(testSession);
console.log("Test session created successfully");
console.log("\nSync test passed. Ready to sync Claude Code sessions.\n");
} catch (error) {
console.log(`Test session failed: ${error}`);
console.log("\nConnection works but sync may have issues.\n");
process.exit(1);
}
} else {
console.log("Connection: FAILED");
console.log("\nCheck your Convex URL and API key.\n");
process.exit(1);
}
});
// ============================================================================
// Hook Command (for Claude Code integration)
// ============================================================================
// Track session state for title generation (first user prompt)
const SESSION_STATE_FILE = path.join(
os.homedir(),
".config",
"claude-code-sync",
"session-state.json"
);
interface SessionState {
[sessionId: string]: {
model?: string;
firstPrompt?: string;
tokenUsage?: { input: number; output: number };
cacheCreationTokens?: number;
cacheReadTokens?: number;
messageCount?: number;
toolCallCount?: number;
syncedMessageUuids?: Set<string> | string[];
startedAt?: string;
endedAt?: string;
durationMs?: number;
title?: string;
cwd?: string;
costEstimate?: number;
};
}
function loadSessionState(): SessionState {
try {
if (fs.existsSync(SESSION_STATE_FILE)) {
const data = JSON.parse(fs.readFileSync(SESSION_STATE_FILE, "utf-8")) as SessionState;
// Convert arrays back to Sets for syncedMessageUuids
for (const sessionId of Object.keys(data)) {
const session = data[sessionId];
if (session.syncedMessageUuids && Array.isArray(session.syncedMessageUuids)) {
session.syncedMessageUuids = new Set(session.syncedMessageUuids);
}
}
return data;
}
} catch {
// Ignore errors
}
return {};
}
function saveSessionState(state: SessionState): void {
try {
const dir = path.dirname(SESSION_STATE_FILE);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
// Convert Sets to arrays for JSON serialization
const serializable: Record<string, unknown> = {};
for (const sessionId of Object.keys(state)) {
const session = state[sessionId];
serializable[sessionId] = {
...session,
syncedMessageUuids: session.syncedMessageUuids instanceof Set
? Array.from(session.syncedMessageUuids)
: session.syncedMessageUuids,
};
}
fs.writeFileSync(SESSION_STATE_FILE, JSON.stringify(serializable, null, 2));
} catch {
// Ignore errors
}
}
function generateTitle(prompt: string): string {
// Use first 80 chars of first prompt as title, trim at word boundary
const trimmed = prompt.slice(0, 80).trim();
if (prompt.length > 80) {
const lastSpace = trimmed.lastIndexOf(" ");
if (lastSpace > 40) {
return trimmed.slice(0, lastSpace) + "...";
}
return trimmed + "...";
}
return trimmed;
}
// Parse transcript file to extract assistant messages, token usage, and stats
interface ParsedTranscript {
assistantMessages: Array<{
uuid: string;
text: string;
timestamp: string;
model?: string;
}>;
stats: TranscriptStats;
}
function parseTranscriptFile(transcriptPath: string): ParsedTranscript {
const result: ParsedTranscript = {
assistantMessages: [],
stats: {
model: undefined,
inputTokens: 0,
cacheCreationTokens: 0,
cacheReadTokens: 0,
outputTokens: 0,
messageCount: 0,
toolCallCount: 0,
title: undefined,
cwd: undefined,
startedAt: undefined,
endedAt: undefined,
durationMs: undefined,
},
};
try {
if (!fs.existsSync(transcriptPath)) {
return result;
}
const content = fs.readFileSync(transcriptPath, "utf-8");
const lines = content.trim().split("\n");
const seenUuids = new Set<string>();
let firstTimestamp: string | undefined;
let lastTimestamp: string | undefined;
for (const line of lines) {
if (!line.trim()) continue;
try {
const entry = JSON.parse(line) as TranscriptEntry;
// Track timestamps for duration calculation
if (entry.timestamp) {
if (!firstTimestamp) {
firstTimestamp = entry.timestamp;
}
lastTimestamp = entry.timestamp;
}
// Get cwd and title from first entry that has them
if (!result.stats.cwd && entry.cwd) {
result.stats.cwd = entry.cwd;
}
if (!result.stats.title && entry.slug) {
result.stats.title = entry.slug;
}
// Count user messages
if (entry.type === "user") {
result.stats.messageCount++;
}
// Process assistant messages
if (entry.type === "assistant" && entry.message) {
const msg = entry.message;
const uuid = entry.uuid || "";
// Get model from first assistant message
if (msg.model && !result.stats.model) {
result.stats.model = msg.model;
}
// Skip duplicate UUIDs for message extraction
if (uuid && seenUuids.has(uuid)) continue;
if (uuid) seenUuids.add(uuid);
// Extract text content from message
if (msg.content && Array.isArray(msg.content)) {
for (const part of msg.content) {
if (part.type === "text" && part.text) {
result.assistantMessages.push({
uuid,
text: part.text,
timestamp: entry.timestamp || new Date().toISOString(),
model: msg.model,
});
}
// Count tool uses
if (part.type === "tool_use") {
result.stats.toolCallCount++;
}
}
}
// Accumulate token usage (separate cache tokens for cost calculation)
if (msg.usage) {
result.stats.inputTokens += msg.usage.input_tokens || 0;
result.stats.cacheCreationTokens += msg.usage.cache_creation_input_tokens || 0;
result.stats.cacheReadTokens += msg.usage.cache_read_input_tokens || 0;
result.stats.outputTokens += msg.usage.output_tokens || 0;
}
}
} catch {
// Skip malformed lines
}
}
// Calculate duration from timestamps
if (firstTimestamp && lastTimestamp) {
result.stats.startedAt = firstTimestamp;
result.stats.endedAt = lastTimestamp;
const startMs = new Date(firstTimestamp).getTime();
const endMs = new Date(lastTimestamp).getTime();
if (!isNaN(startMs) && !isNaN(endMs)) {
result.stats.durationMs = endMs - startMs;
}
}
} catch {
// Return empty result on error
}
return result;
}
program
.command("hook <event>")
.description("Handle Claude Code hook events (reads stdin)")
.action(async (event: string) => {
const config = loadConfig();
if (!config) {
// Exit silently if not configured (don't block Claude Code)
process.exit(0);
}
if (config.autoSync === false) {
process.exit(0);
}
// Read JSON input from stdin
let input = "";
for await (const chunk of process.stdin) {
input += chunk;
}
if (!input.trim()) {
process.exit(0);
}
try {
const client = new SyncClient(config);
const sessionState = loadSessionState();
switch (event) {
case "SessionStart": {
const data = JSON.parse(input) as HookSessionStartData;
// Parse transcript if available to get initial info
let stats: TranscriptStats | undefined;
if (data.transcript_path && fs.existsSync(data.transcript_path)) {
const parsed = parseTranscriptFile(data.transcript_path);
stats = parsed.stats;
}
const cwd = stats?.cwd || data.cwd;
const startedAt = new Date().toISOString();
// Initialize session state
sessionState[data.session_id] = {
model: stats?.model || data.model,
tokenUsage: { input: 0, output: 0 },
cacheCreationTokens: 0,
cacheReadTokens: 0,
messageCount: 0,
toolCallCount: 0,
startedAt,
title: stats?.title,
cwd,
};
saveSessionState(sessionState);
const session: SessionData = {
sessionId: data.session_id,
source: "claude-code",
cwd,
model: stats?.model || data.model,
title: stats?.title,
permissionMode: data.permission_mode,
thinkingEnabled: data.thinking_enabled,
mcpServers: data.mcp_servers,
startType: data.source === "startup" ? "new" : (data.source as SessionData["startType"]),
startedAt,
projectPath: cwd,
projectName: cwd ? path.basename(cwd) : undefined,
};
// Try to get git branch from cwd
if (cwd) {
try {
const gitDir = path.join(cwd, ".git");
if (fs.existsSync(gitDir)) {
const headFile = path.join(gitDir, "HEAD");
if (fs.existsSync(headFile)) {
const head = fs.readFileSync(headFile, "utf-8").trim();
if (head.startsWith("ref: refs/heads/")) {
session.gitBranch = head.replace("ref: refs/heads/", "");
}
}
}
} catch {
// Ignore git errors
}
}
await client.syncSession(session);
break;
}
case "SessionEnd": {
const data = JSON.parse(input) as HookSessionEndData;
const state = sessionState[data.session_id] || {};
// Parse transcript to get final stats including model, tokens, cost
let stats: TranscriptStats = {
model: state.model,
inputTokens: state.tokenUsage?.input || 0,
cacheCreationTokens: state.cacheCreationTokens || 0,
cacheReadTokens: state.cacheReadTokens || 0,
outputTokens: state.tokenUsage?.output || 0,
messageCount: state.messageCount || 0,
toolCallCount: state.toolCallCount || 0,
title: state.title,
cwd: state.cwd || data.cwd,
startedAt: state.startedAt,
endedAt: state.endedAt,
durationMs: state.durationMs,
};
if (data.transcript_path && fs.existsSync(data.transcript_path)) {
const parsed = parseTranscriptFile(data.transcript_path);
stats = parsed.stats;
}
// Calculate cost from tokens (with proper cache pricing)
let cost = data.cost_estimate;
if ((cost === undefined || cost === null || cost === 0) && stats.model) {
if (stats.inputTokens || stats.outputTokens || stats.cacheReadTokens || stats.cacheCreationTokens) {
cost = calculateCost(stats.model, stats);
}
}
const cwd = stats.cwd || data.cwd;
const totalInputTokens = stats.inputTokens + stats.cacheCreationTokens + stats.cacheReadTokens;
const endedAt = stats.endedAt || new Date().toISOString();
const session: SessionData = {
sessionId: data.session_id,
source: "claude-code",
model: stats.model,
title: stats.title || (state.firstPrompt ? generateTitle(state.firstPrompt) : undefined),
cwd,
projectPath: cwd,