-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathworker-module.js
More file actions
106 lines (86 loc) · 3.09 KB
/
Copy pathworker-module.js
File metadata and controls
106 lines (86 loc) · 3.09 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
/**
* Attempt to use Claude Code as a direct module import
* This would be the most elegant solution if Claude exposes a programmatic API
*/
// Approach 1: Try to import Claude's internal modules
async function tryDirectImport () {
try {
// Claude Code is installed via npm as @anthropic-ai/claude-code
// Try to access its internal API
const claudePath = require.resolve('@anthropic-ai/claude-code')
console.log('Claude Code found at:', claudePath)
// Try to import the main module
const claude = await import('@anthropic-ai/claude-code')
console.log('Claude exports:', Object.keys(claude))
return claude
} catch (err) {
console.error('Cannot import Claude as module:', err.message)
return null
}
}
// Approach 2: Use require.cache to intercept Claude's modules
function interceptClaudeModules () {
const originalRequire = Module.prototype.require
const interceptedModules = new Map()
Module.prototype.require = function (id) {
const module = originalRequire.apply(this, arguments)
// Intercept any Claude-related modules
if (id.includes('claude') || id.includes('anthropic')) {
console.log('Intercepted module:', id)
interceptedModules.set(id, module)
// You can wrap or modify the module here
return new Proxy(module, {
get (target, prop) {
console.log(`Accessing ${id}.${String(prop)}`)
return target[prop]
}
})
}
return module
}
return interceptedModules
}
// Approach 3: Monkey-patch child_process to intercept Claude spawning
function interceptClaudeSpawn () {
const { spawn } = require('child_process')
const originalSpawn = spawn
require('child_process').spawn = function (...args) {
const [command, cmdArgs, options] = args
// Detect if this is spawning Claude
if (command.includes('claude')) {
console.log('Intercepting Claude spawn:', { command, cmdArgs, options })
// You can modify args, inject hooks, etc.
// Example: Add custom environment variables
if (options && options.env) {
options.env.CLAUDE_HOOK = 'enabled'
}
}
return originalSpawn.apply(this, args)
}
}
// Example usage in your worker
export class ModuleBasedClaudeWorker {
constructor () {
this.claudeModule = null
}
async initialize () {
// Try method 1: Direct import
this.claudeModule = await tryDirectImport()
if (!this.claudeModule) {
// Fall back to method 2: Interception
console.log('Using interception-based approach')
this.interceptedModules = interceptClaudeModules()
interceptClaudeSpawn()
}
}
async send (message) {
if (this.claudeModule && this.claudeModule.sendMessage) {
// If Claude exposes a programmatic API
return this.claudeModule.sendMessage(message)
}
// Otherwise fall back to process spawning
throw new Error('No programmatic API available')
}
}
// Export for testing
export { tryDirectImport, interceptClaudeModules, interceptClaudeSpawn }