clearctx 3.0.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.
@@ -0,0 +1,118 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ /**
5
+ * continuity-hook.js — Entry point for Claude Code lifecycle hooks.
6
+ *
7
+ * Claude Code hooks call this script with the event type as the first argument.
8
+ * Hook JSON is passed on stdin.
9
+ *
10
+ * Usage (called by Claude Code, not by users):
11
+ * node continuity-hook.js SessionStart < {hook JSON on stdin}
12
+ * node continuity-hook.js Stop < {hook JSON on stdin}
13
+ * node continuity-hook.js PreCompact < {hook JSON on stdin}
14
+ * node continuity-hook.js SessionEnd < {hook JSON on stdin}
15
+ *
16
+ * The script reads all stdin, parses it as JSON, extracts working_directory
17
+ * and session_id, then calls the appropriate handler from continuity-hooks.js.
18
+ *
19
+ * On SessionStart and PreCompact, the handler can return a JSON object with
20
+ * { additionalContext: "string" } which gets injected into Claude's conversation.
21
+ *
22
+ * On Stop and SessionEnd, there is no return value needed (async hooks).
23
+ *
24
+ * CRITICAL: This script must NEVER crash or throw unhandled errors.
25
+ * A crashing hook blocks Claude Code. Wrap everything in try/catch.
26
+ * On any error, log to stderr and exit 0 (success) so Claude Code continues.
27
+ */
28
+
29
+ // Top-level try/catch to ensure we NEVER crash
30
+ try {
31
+ // Get the event type from the first CLI argument
32
+ const eventType = process.argv[2];
33
+
34
+ // If no event type provided, exit silently
35
+ if (!eventType) {
36
+ process.exit(0);
37
+ }
38
+
39
+ // Read all of stdin (it's a single JSON blob, not streaming)
40
+ let stdinData = '';
41
+ const chunks = [];
42
+
43
+ process.stdin.setEncoding('utf8');
44
+ process.stdin.on('data', (chunk) => {
45
+ chunks.push(chunk);
46
+ });
47
+
48
+ process.stdin.on('end', () => {
49
+ try {
50
+ stdinData = chunks.join('');
51
+
52
+ // Parse the JSON input from Claude Code
53
+ let input = {};
54
+ if (stdinData.trim()) {
55
+ input = JSON.parse(stdinData);
56
+ }
57
+
58
+ // For Stop events, check the stop_hook_active flag to prevent infinite loops
59
+ if (eventType === 'Stop' && input.stop_hook_active) {
60
+ process.exit(0);
61
+ }
62
+
63
+ // Load the handler module
64
+ const handlers = require('../src/continuity-hooks');
65
+
66
+ // Map event types to handler functions
67
+ const handlerMap = {
68
+ 'SessionStart': handlers.handleSessionStart,
69
+ 'Stop': handlers.handleStop,
70
+ 'PreCompact': handlers.handlePreCompact,
71
+ 'SessionEnd': handlers.handleSessionEnd
72
+ };
73
+
74
+ // Find the handler for this event type
75
+ const handler = handlerMap[eventType];
76
+ if (!handler) {
77
+ // Unknown event type — exit silently
78
+ process.exit(0);
79
+ }
80
+
81
+ // Call the handler with the parsed input
82
+ const result = handler(input);
83
+
84
+ // If the handler returned additionalContext, write it to stdout
85
+ // so Claude Code can inject it into the conversation
86
+ if (result && result.additionalContext) {
87
+ const output = JSON.stringify({
88
+ hookSpecificOutput: {
89
+ additionalContext: result.additionalContext
90
+ }
91
+ });
92
+ process.stdout.write(output);
93
+ }
94
+
95
+ process.exit(0);
96
+
97
+ } catch (err) {
98
+ // Log the error to stderr so it shows in debug logs but doesn't crash Claude Code
99
+ console.error('[continuity-hook] Error:', err.message);
100
+ process.exit(0); // Always exit 0 — never block Claude Code
101
+ }
102
+ });
103
+
104
+ // Handle stdin errors gracefully
105
+ process.stdin.on('error', () => {
106
+ process.exit(0);
107
+ });
108
+
109
+ // If stdin is already closed (piped empty), trigger 'end' manually
110
+ if (process.stdin.isTTY) {
111
+ process.stdin.emit('end');
112
+ }
113
+
114
+ } catch (err) {
115
+ // Outermost catch — if even requiring modules fails, exit cleanly
116
+ console.error('[continuity-hook] Fatal:', err.message);
117
+ process.exit(0);
118
+ }
package/bin/mcp.js ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * clearctx MCP Server entry point.
5
+ *
6
+ * This starts the MCP (Model Context Protocol) server that exposes
7
+ * multi-session tools to Claude Code as native tools.
8
+ *
9
+ * Claude Code connects to this server via stdio. Once connected,
10
+ * tools like spawn_session, send_message, delegate_task appear
11
+ * in Claude's tool list — just like Read, Edit, Grep, etc.
12
+ *
13
+ * Usage (in Claude Code MCP config):
14
+ * {
15
+ * "mcpServers": {
16
+ * "multi-session": {
17
+ * "command": "ctx-mcp"
18
+ * }
19
+ * }
20
+ * }
21
+ *
22
+ * Or run directly:
23
+ * node bin/mcp.js
24
+ */
25
+
26
+ const { startServer } = require('../src/mcp-server');
27
+ startServer();