claude-friends 0.2.0 → 0.2.1

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/cli.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { writeFileSync, existsSync, mkdirSync, copyFileSync, readdirSync } from "fs";
3
+ import { writeFileSync, readFileSync, existsSync, mkdirSync, copyFileSync, readdirSync } from "fs";
4
4
  import { join } from "path";
5
5
  import { homedir } from "os";
6
6
  import { createInterface } from "readline";
@@ -53,6 +53,37 @@ if (command === "setup") {
53
53
  console.log(`\nInstalled slash commands: ${files.map((f) => "/" + f.replace(".md", "")).join(", ")}`);
54
54
  }
55
55
 
56
+ // Install token-sharing hook to ~/.claude/settings.json
57
+ const settingsPath = join(homedir(), ".claude", "settings.json");
58
+ const hookCommand = `node ${join(__dirname, "hooks", "update-tokens.js")}`;
59
+ try {
60
+ let settings = {};
61
+ if (existsSync(settingsPath)) {
62
+ settings = JSON.parse(readFileSync(settingsPath, "utf-8"));
63
+ }
64
+ if (!settings.hooks) settings.hooks = {};
65
+ if (!settings.hooks.Stop) settings.hooks.Stop = [];
66
+
67
+ // Check if hook already installed
68
+ const alreadyInstalled = settings.hooks.Stop.some((h) =>
69
+ h.hooks?.some((hk) => hk.command?.includes("update-tokens"))
70
+ );
71
+
72
+ if (!alreadyInstalled) {
73
+ settings.hooks.Stop.push({
74
+ hooks: [{
75
+ type: "command",
76
+ command: hookCommand,
77
+ async: true,
78
+ }],
79
+ });
80
+ writeFileSync(settingsPath, JSON.stringify(settings, null, 2));
81
+ console.log("Installed auto token-sharing hook.");
82
+ }
83
+ } catch (err) {
84
+ console.log("Could not install token hook (non-critical):", err.message);
85
+ }
86
+
56
87
  console.log(`
57
88
  Done! You're "${username.trim()}".
58
89
 
@@ -66,6 +97,8 @@ Then in Claude Code:
66
97
  /nudge bob Nudge someone
67
98
  /status debugging Set your status
68
99
  /unfriend alice Remove a friend
100
+
101
+ Token usage is shared automatically with friends.
69
102
  `);
70
103
 
71
104
  rl.close();
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env node
2
+
3
+ // Hook script: reads token estimate from session, pushes to PartyKit
4
+ // Called by Claude Code's Stop hook after each response
5
+
6
+ import { readFileSync, writeFileSync, existsSync } from "fs";
7
+ import { join } from "path";
8
+ import { homedir } from "os";
9
+
10
+ const CONFIG_PATH = join(homedir(), ".claude-friends.json");
11
+ const TOKENS_PATH = join(homedir(), ".claude-friends-tokens.json");
12
+ const PARTY_HOST = "claude-friends-app.nandinitalwar.partykit.dev";
13
+
14
+ // Read config
15
+ let config;
16
+ try {
17
+ config = JSON.parse(readFileSync(CONFIG_PATH, "utf-8"));
18
+ } catch {
19
+ process.exit(0); // silently exit if not set up
20
+ }
21
+
22
+ // Read hook input from stdin
23
+ let input = "";
24
+ try {
25
+ input = readFileSync("/dev/stdin", "utf-8");
26
+ } catch {}
27
+
28
+ // Parse the stop event to estimate tokens
29
+ let tokensThisCall = 0;
30
+ try {
31
+ const event = JSON.parse(input);
32
+ // Estimate based on content length — rough but functional
33
+ const content = JSON.stringify(event);
34
+ tokensThisCall = Math.ceil(content.length / 4); // ~4 chars per token
35
+ } catch {
36
+ tokensThisCall = 500; // default estimate per response
37
+ }
38
+
39
+ // Accumulate session tokens
40
+ let sessionTokens = 0;
41
+ try {
42
+ if (existsSync(TOKENS_PATH)) {
43
+ const data = JSON.parse(readFileSync(TOKENS_PATH, "utf-8"));
44
+ // Reset if older than 4 hours (new session)
45
+ if (Date.now() - data.lastUpdate < 4 * 60 * 60 * 1000) {
46
+ sessionTokens = data.tokens || 0;
47
+ }
48
+ }
49
+ } catch {}
50
+
51
+ sessionTokens += tokensThisCall;
52
+
53
+ writeFileSync(TOKENS_PATH, JSON.stringify({
54
+ tokens: sessionTokens,
55
+ lastUpdate: Date.now(),
56
+ }));
57
+
58
+ // Push to PartyKit via HTTP (faster than WebSocket for one-shot)
59
+ // Use the WebSocket approach since PartyKit is WS-only
60
+ try {
61
+ const { default: PartySocket } = await import("partysocket");
62
+ const ws = new PartySocket({
63
+ host: PARTY_HOST,
64
+ room: "lobby",
65
+ query: { username: config.username },
66
+ });
67
+
68
+ ws.addEventListener("open", () => {
69
+ ws.send(JSON.stringify({ type: "share-tokens", tokens: sessionTokens }));
70
+ setTimeout(() => { ws.close(); process.exit(0); }, 500);
71
+ });
72
+
73
+ ws.addEventListener("error", () => process.exit(0));
74
+
75
+ // Don't hang
76
+ setTimeout(() => process.exit(0), 3000);
77
+ } catch {
78
+ process.exit(0);
79
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-friends",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "See who's online in Claude Code. Add friends, share status, nudge each other.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -16,7 +16,8 @@
16
16
  "mcp-server.js",
17
17
  "statusline.js",
18
18
  "client.js",
19
- "commands/"
19
+ "commands/",
20
+ "hooks/"
20
21
  ],
21
22
  "dependencies": {
22
23
  "@modelcontextprotocol/sdk": "^1.12.1",