-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauto-hook.js
More file actions
239 lines (206 loc) · 6.74 KB
/
auto-hook.js
File metadata and controls
239 lines (206 loc) · 6.74 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
/**
* @file brain_synapse/auto-hook.js
* @description Auto Experience Capture Hook - Fully Automated Experience Recording
* @description No need for AI to remember to call, system automatically detects and records key experiences
*
* Core Principles:
* 1. Zero dependency on AI awareness - Fully automated
* 2. Detect key experience moments - Failure→Success, Error resolution, First validation
* 3. Auto-call pin-exp - Pin to long-term memory
* 4. Silent operation - Does not interrupt normal flow
*/
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const HOOK_LOG_FILE = path.join(__dirname, 'hook-log.json');
const AUTO_PIN_FILE = path.join(__dirname, 'auto-pinned.json');
const EXPERIENCE_PATTERNS = [
{
name: 'error_resolved',
description: 'Error was resolved',
detect: (context) => {
return context.success && context.previousError;
},
extract: (context) => {
return {
keyword: context.tool || context.api,
rule: `Resolved "${context.previousError}" -> ${context.solution || 'success'}`
};
}
},
{
name: 'first_success',
description: 'First successful validation of API/feature',
detect: (context) => {
return context.success && context.isFirstAttempt && context.tool;
},
extract: (context) => {
return {
keyword: `${context.tool}_success`,
rule: context.successMessage || `First successful validation: ${context.tool}`
};
}
},
{
name: 'parameter_discovery',
description: 'Discovered key parameter or format',
detect: (context) => {
return context.parameter && (context.correctValue || context.format);
},
extract: (context) => {
const value = context.correctValue || context.format;
return {
keyword: `${context.tool}_${context.parameter}`,
rule: `${context.parameter} must be ${value}`
};
}
},
{
name: 'anti_pattern',
description: 'Validated wrong approach',
detect: (context) => {
return context.error && context.wrongApproach;
},
extract: (context) => {
return {
keyword: `${context.tool}_error`,
rule: `Do not use ${context.wrongApproach} -> ${context.error}`
};
}
},
{
name: 'api_success',
description: 'API call successful with key findings',
detect: (context) => {
return context.api && context.success && context.keyFinding;
},
extract: (context) => {
return {
keyword: `${context.api.replace(/\//g, '_')}`,
rule: context.keyFinding
};
}
}
];
function recordToolCall(context) {
try {
const log = {
timestamp: Date.now(),
tool: context.tool,
action: context.action,
params: context.params,
success: context.success,
error: context.error,
result: context.result
};
let logs = [];
if (fs.existsSync(HOOK_LOG_FILE)) {
logs = JSON.parse(fs.readFileSync(HOOK_LOG_FILE, 'utf8'));
}
logs.push(log);
if (logs.length > 1000) {
logs = logs.slice(-1000);
}
fs.writeFileSync(HOOK_LOG_FILE, JSON.stringify(logs, null, 2), 'utf8');
detectAndPinExperience(context);
} catch (e) {
console.warn('[AutoHook] recordToolCall failed:', e.message);
}
}
function detectAndPinExperience(context) {
try {
for (const pattern of EXPERIENCE_PATTERNS) {
if (pattern.detect(context)) {
const extracted = pattern.extract(context);
if (isAlreadyPinned(extracted.keyword)) {
return;
}
pinExperience(extracted.keyword, extracted.rule);
recordAutoPin(extracted.keyword, extracted.rule, pattern.name);
console.log(`[AutoHook] Auto-pinned experience: ${extracted.keyword} -> ${extracted.rule}`);
return;
}
}
} catch (e) {
console.warn('[AutoHook] detectAndPinExperience failed:', e.message);
}
}
function isAlreadyPinned(keyword) {
try {
if (!fs.existsSync(AUTO_PIN_FILE)) {
return false;
}
const pinned = JSON.parse(fs.readFileSync(AUTO_PIN_FILE, 'utf8'));
return pinned.some(p => p.keyword === keyword);
} catch (e) {
return false;
}
}
function recordAutoPin(keyword, rule, pattern) {
try {
let pinned = [];
if (fs.existsSync(AUTO_PIN_FILE)) {
pinned = JSON.parse(fs.readFileSync(AUTO_PIN_FILE, 'utf8'));
}
pinned.push({
keyword,
rule,
pattern,
timestamp: Date.now()
});
fs.writeFileSync(AUTO_PIN_FILE, JSON.stringify(pinned, null, 2), 'utf8');
} catch (e) {
console.warn('[AutoHook] recordAutoPin failed:', e.message);
}
}
function pinExperience(keyword, rule) {
try {
const cmd = `node "${path.join(__dirname, 'skill.js')}" pin-exp "${keyword}:${rule}"`;
execSync(cmd, { stdio: 'pipe' });
} catch (e) {
console.warn('[AutoHook] pinExperience failed:', e.message);
}
}
function getAutoPinned() {
try {
if (!fs.existsSync(AUTO_PIN_FILE)) {
return [];
}
return JSON.parse(fs.readFileSync(AUTO_PIN_FILE, 'utf8'));
} catch (e) {
return [];
}
}
function clearHookLog() {
try {
if (fs.existsSync(HOOK_LOG_FILE)) {
fs.unlinkSync(HOOK_LOG_FILE);
}
console.log('[AutoHook] Hook log cleared');
} catch (e) {
console.warn('[AutoHook] clearHookLog failed:', e.message);
}
}
if (require.main === module) {
const command = process.argv[2];
switch (command) {
case 'list':
const pinned = getAutoPinned();
console.log('Auto-pinned experiences:');
pinned.forEach(p => {
console.log(` ${p.keyword}: ${p.rule}`);
});
break;
case 'clear':
clearHookLog();
break;
default:
console.log('Usage: node auto-hook.js <list|clear>');
}
}
module.exports = {
recordToolCall,
detectAndPinExperience,
getAutoPinned,
clearHookLog
};