@qcluffy/agent-bootstrap 0.0.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.
Files changed (53) hide show
  1. package/AGENTS.md +294 -0
  2. package/BOOTSTRAP.md +268 -0
  3. package/DEMO.md +103 -0
  4. package/HEARTBEAT.md +444 -0
  5. package/IDENTITY.md +239 -0
  6. package/LICENSE +21 -0
  7. package/MEMORY.md +324 -0
  8. package/README.md +582 -0
  9. package/SOUL.md +254 -0
  10. package/TOOLS.md +317 -0
  11. package/USER.md +274 -0
  12. package/bootstrap-system/HOOK.md +31 -0
  13. package/bootstrap-system/README.md +109 -0
  14. package/dist/index.d.ts.map +1 -0
  15. package/dist/index.js +155 -0
  16. package/dist/index.js.map +1 -0
  17. package/dist/systems/bootstrap.d.ts.map +1 -0
  18. package/dist/systems/bootstrap.js +272 -0
  19. package/dist/systems/bootstrap.js.map +1 -0
  20. package/dist/systems/cognition.d.ts.map +1 -0
  21. package/dist/systems/cognition.js +254 -0
  22. package/dist/systems/cognition.js.map +1 -0
  23. package/dist/systems/emotion.d.ts.map +1 -0
  24. package/dist/systems/emotion.js +194 -0
  25. package/dist/systems/emotion.js.map +1 -0
  26. package/dist/systems/input.d.ts.map +1 -0
  27. package/dist/systems/input.js +161 -0
  28. package/dist/systems/input.js.map +1 -0
  29. package/dist/systems/output.d.ts.map +1 -0
  30. package/dist/systems/output.js +224 -0
  31. package/dist/systems/output.js.map +1 -0
  32. package/dist/test.js +54 -0
  33. package/emotion-system/README.md +195 -0
  34. package/hooks/agent-lifecycle/handler.js +109 -0
  35. package/hooks/auto-bootstrap/handler.js +145 -0
  36. package/hooks/bootstrap-system/HOOK.md +74 -0
  37. package/hooks/bootstrap-system/handler.js +220 -0
  38. package/hooks/cognition-system/HOOK.md +75 -0
  39. package/hooks/cognition-system/handler.js +186 -0
  40. package/hooks/emotion-system/HOOK.md +81 -0
  41. package/hooks/emotion-system/handler.js +239 -0
  42. package/hooks/heartbeat-system/HOOK.md +63 -0
  43. package/hooks/heartbeat-system/handler.js +121 -0
  44. package/hooks/input-system/HOOK.md +79 -0
  45. package/hooks/input-system/handler.js +181 -0
  46. package/hooks/memory-system/HOOK.md +43 -0
  47. package/hooks/memory-system/handler.js +213 -0
  48. package/hooks/output-system/HOOK.md +79 -0
  49. package/hooks/output-system/handler.js +195 -0
  50. package/memory-system/README.md +291 -0
  51. package/openclaw.plugin.json +51 -0
  52. package/package.json +30 -0
  53. package/requirements.txt +11 -0
@@ -0,0 +1,220 @@
1
+ /**
2
+ * Bootstrap System Hook Handler
3
+ *
4
+ * 集成 Python bootstrap-system 到 OpenClaw
5
+ */
6
+
7
+ const fs = require('fs').promises;
8
+ const path = require('path');
9
+ const { spawn } = require('child_process');
10
+ const os = require('os');
11
+
12
+ // 配置
13
+ const CONFIG = {
14
+ bootstrapSystemPath: 'templates/bootstrap-system',
15
+ mainScript: 'main.py',
16
+ };
17
+
18
+ /**
19
+ * 运行 Python bootstrap-system CLI
20
+ */
21
+ async function runPythonCli(args = []) {
22
+ const workspaceDir = process.env.OPENCLAW_WORKSPACE ||
23
+ path.join(os.homedir(), '.openclaw', 'workspace');
24
+ const pythonScript = path.join(workspaceDir, CONFIG.bootstrapSystemPath, CONFIG.mainScript);
25
+
26
+ return new Promise((resolve) => {
27
+ fs.access(pythonScript).then(() => {
28
+ const proc = spawn('python3', [pythonScript, ...args], {
29
+ cwd: workspaceDir,
30
+ env: {
31
+ ...process.env,
32
+ OPENCLAW_WORKSPACE: workspaceDir,
33
+ PYTHONPATH: path.join(workspaceDir, CONFIG.bootstrapSystemPath)
34
+ }
35
+ });
36
+
37
+ let output = '';
38
+ let error = '';
39
+
40
+ proc.stdout.on('data', (data) => { output += data.toString(); });
41
+ proc.stderr.on('data', (data) => { error += data.toString(); });
42
+
43
+ proc.on('close', (code) => {
44
+ resolve({ success: code === 0, output, error });
45
+ });
46
+
47
+ proc.on('error', (err) => {
48
+ resolve({ success: false, error: err.message });
49
+ });
50
+
51
+ }).catch(() => {
52
+ resolve({ success: false, error: 'Bootstrap system not found' });
53
+ });
54
+ });
55
+ }
56
+
57
+ /**
58
+ * 从会话消息中提取文本内容
59
+ */
60
+ function extractMessageText(message) {
61
+ if (!message || !message.content) return '';
62
+
63
+ if (typeof message.content === 'string') {
64
+ return message.content;
65
+ }
66
+
67
+ if (Array.isArray(message.content)) {
68
+ return message.content
69
+ .filter((c) => c.type === 'text')
70
+ .map((c) => c.text || '')
71
+ .join('');
72
+ }
73
+
74
+ return '';
75
+ }
76
+
77
+ /**
78
+ * 处理 session_start - 检查是否需要引导
79
+ */
80
+ async function handleSessionStart(event) {
81
+ console.log('[bootstrap-system] Checking bootstrap status...');
82
+
83
+ const result = await runPythonCli(['status', '--json']);
84
+
85
+ if (result.success && result.output) {
86
+ try {
87
+ const status = JSON.parse(result.output);
88
+ event.context = event.context || {};
89
+ event.context.bootstrap = status;
90
+
91
+ if (!status.is_completed && status.status === 'not_started') {
92
+ // 需要开始引导
93
+ event.needsBootstrap = true;
94
+ console.log('[bootstrap-system] Bootstrap needed');
95
+ } else if (status.is_completed) {
96
+ console.log('[bootstrap-system] Bootstrap already completed');
97
+ }
98
+ } catch (e) {
99
+ console.log('[bootstrap-system] Parse error:', e.message);
100
+ }
101
+ }
102
+
103
+ return event;
104
+ }
105
+
106
+ /**
107
+ * 处理 bootstrap 命令
108
+ */
109
+ async function handleBootstrapCommand(event) {
110
+ const action = event.action;
111
+ const input = event.input || '';
112
+
113
+ if (action === 'start' || action === 'begin') {
114
+ // 开始引导
115
+ const result = await runPythonCli(['start']);
116
+ event.response = result.output;
117
+ }
118
+ else if (action === 'step') {
119
+ // 查看当前步骤
120
+ const result = await runPythonCli(['step']);
121
+ event.response = result.output;
122
+ }
123
+ else if (action === 'submit' || action === 'next') {
124
+ // 提交输入
125
+ const result = await runPythonCli(['submit', input]);
126
+ event.response = result.output;
127
+
128
+ // 检查是否完成
129
+ if (result.output.includes('初始化完成') || result.output.includes('completed')) {
130
+ event.bootstrapCompleted = true;
131
+ }
132
+ }
133
+ else if (action === 'status') {
134
+ // 查看状态
135
+ const result = await runPythonCli(['status']);
136
+ event.response = result.output;
137
+ }
138
+ else if (action === 'reset') {
139
+ // 重置
140
+ const result = await runPythonCli(['reset']);
141
+ event.response = '✓ 引导已重置,请重新开始';
142
+ }
143
+
144
+ return event;
145
+ }
146
+
147
+ /**
148
+ * 处理用户消息 - 检查是否是引导输入
149
+ */
150
+ async function handleMessage(event) {
151
+ const context = event.context || {};
152
+
153
+ // 检查是否正在进行引导
154
+ if (context.bootstrap && context.bootstrap.status === 'in_progress') {
155
+ const messages = context.messages || [];
156
+ const reversed = [...messages].reverse();
157
+ const lastUserMsg = reversed.find((m) => m.role === 'user');
158
+
159
+ if (lastUserMsg) {
160
+ const userText = extractMessageText(lastUserMsg);
161
+
162
+ if (userText && !userText.startsWith('/')) {
163
+ // 提交作为引导输入
164
+ console.log('[bootstrap-system] Submitting bootstrap input:', userText.substring(0, 20));
165
+ const result = await runPythonCli(['submit', userText]);
166
+
167
+ event.bootstrapResponse = result.output;
168
+
169
+ if (result.output.includes('初始化完成') || result.output.includes('completed')) {
170
+ event.bootstrapCompleted = true;
171
+ }
172
+ }
173
+ }
174
+ }
175
+
176
+ return event;
177
+ }
178
+
179
+ /**
180
+ * 主 handler
181
+ */
182
+ async function handle(event) {
183
+ const type = event.type || event.event || '';
184
+ const action = event.action || '';
185
+
186
+ console.log(`[bootstrap-system] Event: ${type}/${action}`);
187
+
188
+ try {
189
+ if (type === 'session' || type === 'session:start') {
190
+ await handleSessionStart(event);
191
+ }
192
+ else if (type === 'message' || type === 'message:received') {
193
+ await handleMessage(event);
194
+ }
195
+ else if (type === 'command') {
196
+ // 处理引导命令
197
+ if (action === 'bootstrap' || action === '引导' || action === '开始') {
198
+ await handleBootstrapCommand(event);
199
+ }
200
+ else if (['start', 'step', 'submit', 'status', 'reset'].includes(action)) {
201
+ await handleBootstrapCommand(event);
202
+ }
203
+ }
204
+ } catch (error) {
205
+ console.error('[bootstrap-system] Handler error:', error.message);
206
+ }
207
+
208
+ return event;
209
+ }
210
+
211
+ module.exports = handle;
212
+ module.exports.handle = handle;
213
+ module.exports.metadata = {
214
+ name: 'bootstrap-system',
215
+ description: '初始化引导系统,交互式收集用户配置、塑造Agent人格',
216
+ events: ['session', 'message', 'command'],
217
+ version: '1.0.0',
218
+ };
219
+
220
+ module.exports.default = handle;
@@ -0,0 +1,75 @@
1
+ ---
2
+ name: cognition-system
3
+ description: "认知系统,推理规划、任务分解、学习适应、决策判断"
4
+ homepage: https://github.com/openclaw/openclaw
5
+ metadata:
6
+ {
7
+ "openclaw": {
8
+ "emoji": "🧠",
9
+ "events": ["message", "command"],
10
+ "requires": { "config": ["workspace.dir"] },
11
+ "install": [{ "id": "workspace", "kind": "workspace", "label": "User workspace" }]
12
+ }
13
+ }
14
+ ---
15
+
16
+ # Cognition System Hook
17
+
18
+ > 推理规划与决策的"大脑"
19
+
20
+ ## 概述
21
+
22
+ 认知系统负责推理规划、任务分解、学习适应和决策判断。
23
+
24
+ ## 功能
25
+
26
+ ### 1. 推理引擎
27
+ 基于上下文进行逻辑推理
28
+
29
+ ### 2. 任务规划器
30
+ 把复杂任务拆解成步骤:
31
+ - 代码任务:分析 → 设计 → 实现 → 测试
32
+ - 写作任务:大纲 → 撰写 → 修改
33
+ - 调研任务:搜索 → 收集 → 总结
34
+
35
+ ### 3. 学习引擎
36
+ 从互动中学习用户偏好:
37
+ - 沟通风格(直接/友好)
38
+ - 详细程度(简洁/详细)
39
+ - 常用操作
40
+
41
+ ### 4. 决策判断
42
+ 道德/安全决策辅助:
43
+ - 拒绝有害请求
44
+ - 拒绝非法请求
45
+ - 保护隐私
46
+ - 敏感操作警告
47
+
48
+ ## 使用方法
49
+
50
+ ```bash
51
+ # 处理意图
52
+ python3 templates/cognition-system/main.py process '{"action": "write", "type": "code"}'
53
+
54
+ # 查看状态
55
+ python3 templates/cognition-system/main.py status
56
+
57
+ # 学习反馈
58
+ python3 templates/cognition-system/main.py learn "详细一点"
59
+
60
+ # 决策评估
61
+ python3 templates/cognition-system/main.py decide "删除文件"
62
+ ```
63
+
64
+ ## 工作流程
65
+
66
+ ```
67
+ 用户输入 → INPUT(意图识别) → COGNITION(推理规划) → OUTPUT(执行输出)
68
+
69
+ 任务分解 + 决策判断 + 学习适应
70
+ ```
71
+
72
+ ## 依赖
73
+
74
+ - Python 3.8+
75
+ - input-system(意图识别结果)
@@ -0,0 +1,186 @@
1
+ /**
2
+ * Cognition System Hook Handler
3
+ *
4
+ * 集成 Python cognition-system 到 OpenClaw
5
+ */
6
+
7
+ const fs = require('fs').promises;
8
+ const path = require('path');
9
+ const { spawn } = require('child_process');
10
+ const os = require('os');
11
+
12
+ // 配置
13
+ const CONFIG = {
14
+ cognitionSystemPath: 'templates/cognition-system',
15
+ mainScript: 'main.py',
16
+ };
17
+
18
+ /**
19
+ * 运行 Python cognition-system CLI
20
+ */
21
+ async function runPythonCli(args = []) {
22
+ const workspaceDir = process.env.OPENCLAW_WORKSPACE ||
23
+ path.join(os.homedir(), '.openclaw', 'workspace');
24
+ const pythonScript = path.join(workspaceDir, CONFIG.cognitionSystemPath, CONFIG.mainScript);
25
+
26
+ return new Promise((resolve) => {
27
+ fs.access(pythonScript).then(() => {
28
+ const proc = spawn('python3', [pythonScript, ...args], {
29
+ cwd: workspaceDir,
30
+ env: {
31
+ ...process.env,
32
+ OPENCLAW_WORKSPACE: workspaceDir,
33
+ PYTHONPATH: path.join(workspaceDir, CONFIG.cognitionSystemPath)
34
+ }
35
+ });
36
+
37
+ let output = '';
38
+ let error = '';
39
+
40
+ proc.stdout.on('data', (data) => { output += data.toString(); });
41
+ proc.stderr.on('data', (data) => { error += data.toString(); });
42
+
43
+ proc.on('close', (code) => {
44
+ resolve({ success: code === 0, output, error });
45
+ });
46
+
47
+ proc.on('error', (err) => {
48
+ resolve({ success: false, error: err.message });
49
+ });
50
+
51
+ }).catch(() => {
52
+ resolve({ success: false, error: 'Cognition system not found' });
53
+ });
54
+ });
55
+ }
56
+
57
+ /**
58
+ * 从会话消息中提取文本内容
59
+ */
60
+ function extractMessageText(message) {
61
+ if (!message || !message.content) return '';
62
+
63
+ if (typeof message.content === 'string') {
64
+ return message.content;
65
+ }
66
+
67
+ if (Array.isArray(message.content)) {
68
+ return message.content
69
+ .filter((c) => c.type === 'text')
70
+ .map((c) => c.text || '')
71
+ .join('');
72
+ }
73
+
74
+ return '';
75
+ }
76
+
77
+ /**
78
+ * 处理 message:received - 认知处理
79
+ */
80
+ async function handleMessageReceived(event) {
81
+ console.log('[cognition-system] Processing with cognition...');
82
+
83
+ const context = event.context || {};
84
+ const messages = context.messages || [];
85
+
86
+ if (messages.length === 0) return event;
87
+
88
+ // 获取最新用户消息
89
+ const reversed = [...messages].reverse();
90
+ const lastUserMsg = reversed.find((m) => m.role === 'user');
91
+
92
+ if (!lastUserMsg) return event;
93
+
94
+ const userText = extractMessageText(lastUserMsg);
95
+
96
+ if (!userText || userText.startsWith('/')) return event;
97
+
98
+ // 获取已分析的意图(来自 input-system)
99
+ const intent = context.intent || {};
100
+
101
+ if (intent.type) {
102
+ // 使用认知系统处理
103
+ const result = await runPythonCli(['process', JSON.stringify({
104
+ action: intent.action || intent.type,
105
+ type: intent.type,
106
+ }), '--json']);
107
+
108
+ if (result.success && result.output) {
109
+ try {
110
+ const cognitionResult = JSON.parse(result.output);
111
+ context.cognition = cognitionResult;
112
+
113
+ // 根据任务步骤调整回复策略
114
+ if (cognitionResult.allowed && cognitionResult.next_step) {
115
+ context.replyStrategy = {
116
+ ...context.replyStrategy,
117
+ task: cognitionResult.task,
118
+ nextStep: cognitionResult.next_step,
119
+ };
120
+ }
121
+
122
+ console.log('[cognition-system] Cognition result:', cognitionResult.allowed ? 'allowed' : 'denied');
123
+ } catch (e) {
124
+ console.log('[cognition-system] Parse error:', e.message);
125
+ }
126
+ }
127
+ }
128
+
129
+ return event;
130
+ }
131
+
132
+ /**
133
+ * 处理 feedback 命令
134
+ */
135
+ async function handleFeedback(event) {
136
+ const feedback = event.feedback || event.text || '';
137
+
138
+ if (feedback) {
139
+ const result = await runPythonCli(['learn', feedback]);
140
+ console.log('[cognition-system] Learning:', feedback);
141
+ }
142
+
143
+ return event;
144
+ }
145
+
146
+ /**
147
+ * 主 handler
148
+ */
149
+ async function handle(event) {
150
+ const type = event.type || event.event || '';
151
+ const action = event.action || '';
152
+
153
+ console.log(`[cognition-system] Event: ${type}/${action}`);
154
+
155
+ try {
156
+ if (type === 'message' || type === 'message:received') {
157
+ await handleMessageReceived(event);
158
+ }
159
+ else if (type === 'command') {
160
+ if (action === 'learn' || action === 'feedback') {
161
+ await handleFeedback(event);
162
+ }
163
+ else if (action === 'cognition' || action === 'status') {
164
+ const result = await runPythonCli(['status', '--json']);
165
+ if (result.success) {
166
+ event.response = result.output;
167
+ }
168
+ }
169
+ }
170
+ } catch (error) {
171
+ console.error('[cognition-system] Handler error:', error.message);
172
+ }
173
+
174
+ return event;
175
+ }
176
+
177
+ module.exports = handle;
178
+ module.exports.handle = handle;
179
+ module.exports.metadata = {
180
+ name: 'cognition-system',
181
+ description: '认知系统,推理规划、任务分解、学习适应、决策判断',
182
+ events: ['message', 'command'],
183
+ version: '1.0.0',
184
+ };
185
+
186
+ module.exports.default = handle;
@@ -0,0 +1,81 @@
1
+ ---
2
+ name: emotion-system
3
+ description: "集成情感系统,管理 Agent 心情、能量、连接感和压力"
4
+ homepage: https://github.com/openclaw/openclaw
5
+ metadata:
6
+ {
7
+ "openclaw": {
8
+ "emoji": "💖",
9
+ "events": ["session", "message", "lifecycle", "heartbeat", "command"],
10
+ "requires": { "config": ["workspace.dir"] },
11
+ "install": [{ "id": "workspace", "kind": "workspace", "label": "User workspace" }]
12
+ }
13
+ }
14
+ ---
15
+
16
+ # Emotion System Hook
17
+
18
+ > 管理 Agent 情感状态,让 Agent 更有"灵魂"
19
+
20
+ ## 概述
21
+
22
+ 这个 Hook 会在以下时机触发:
23
+
24
+ - `session_start`: 会话开始时加载情感状态
25
+ - `message_received`: 收到用户消息时分析情感
26
+ - `agent_end`: Agent 回复完成后更新情感(提升连接感)
27
+ - `heartbeat`: 心跳时应用自然衰减
28
+ - `/emotion` 或 `/状态`: 命令时显示当前情感状态
29
+
30
+ ## 功能
31
+
32
+ 1. **情感状态管理**: 心情、能量、连接感、压力值
33
+ 2. **消息情感分析**: 分析用户消息的情感倾向
34
+ 3. **互动反馈**: 根据互动结果调整情感
35
+ 4. **自然衰减**: 模拟情感的自然变化
36
+ 5. **状态展示**: 可视化当前情感状态
37
+
38
+ ## 情感状态
39
+
40
+ | 维度 | 范围 | 说明 |
41
+ |------|------|------|
42
+ | mood | 9种心情 | curious, happy, calm, focused, tired, anxious, excited, thoughtful, neutral |
43
+ | energy | 0-100% | 能量水平,自然衰减 |
44
+ | connection | 0-100% | 与用户的连接感,互动提升 |
45
+ | stress | 0-100% | 压力值,失败增加,成功减少 |
46
+
47
+ ## 使用方法
48
+
49
+ ### 查看当前情感状态
50
+ ```bash
51
+ python3 templates/emotion-system/main.py state
52
+ ```
53
+
54
+ ### 分析消息情感
55
+ ```bash
56
+ python3 templates/emotion-system/main.py analyze "太棒了!"
57
+ ```
58
+
59
+ ### 提升连接感
60
+ ```bash
61
+ python3 templates/emotion-system/main.py boost
62
+ ```
63
+
64
+ ### 互动成功
65
+ ```bash
66
+ python3 templates/emotion-system/main.py success
67
+ ```
68
+
69
+ ### 自然衰减
70
+ ```bash
71
+ python3 templates/emotion-system/main.py decay
72
+ ```
73
+
74
+ ## 配置
75
+
76
+ 无需额外配置,Hook 会自动使用 workspace 目录下的 emotion-system。
77
+
78
+ ## 依赖
79
+
80
+ - Python 3.8+
81
+ - emotion-system 模块(在 workspace/templates/ 目录下)