openclawdreams 0.7.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.
- package/.env.example +14 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +27 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +19 -0
- package/.github/dependabot.yml +17 -0
- package/.github/pull_request_template.md +19 -0
- package/.github/workflows/build.yml +30 -0
- package/.github/workflows/release.yml +110 -0
- package/.prettierignore +4 -0
- package/.prettierrc +7 -0
- package/.versionrc.json +26 -0
- package/AGENTS.md +286 -0
- package/CHANGELOG.md +157 -0
- package/CODE_OF_CONDUCT.md +41 -0
- package/CONTRIBUTING.md +95 -0
- package/LICENSE +21 -0
- package/README.md +363 -0
- package/SECURITY.md +39 -0
- package/bin/electricsheep.ts +5 -0
- package/dist/bin/electricsheep.d.ts +3 -0
- package/dist/bin/electricsheep.d.ts.map +1 -0
- package/dist/bin/electricsheep.js +4 -0
- package/dist/bin/electricsheep.js.map +1 -0
- package/dist/src/budget.d.ts +28 -0
- package/dist/src/budget.d.ts.map +1 -0
- package/dist/src/budget.js +87 -0
- package/dist/src/budget.js.map +1 -0
- package/dist/src/cli.d.ts +19 -0
- package/dist/src/cli.d.ts.map +1 -0
- package/dist/src/cli.js +289 -0
- package/dist/src/cli.js.map +1 -0
- package/dist/src/config.d.ts +37 -0
- package/dist/src/config.d.ts.map +1 -0
- package/dist/src/config.js +70 -0
- package/dist/src/config.js.map +1 -0
- package/dist/src/crypto.d.ts +19 -0
- package/dist/src/crypto.d.ts.map +1 -0
- package/dist/src/crypto.js +70 -0
- package/dist/src/crypto.js.map +1 -0
- package/dist/src/dreamer.d.ts +13 -0
- package/dist/src/dreamer.d.ts.map +1 -0
- package/dist/src/dreamer.js +213 -0
- package/dist/src/dreamer.js.map +1 -0
- package/dist/src/filter.d.ts +30 -0
- package/dist/src/filter.d.ts.map +1 -0
- package/dist/src/filter.js +124 -0
- package/dist/src/filter.js.map +1 -0
- package/dist/src/identity.d.ts +29 -0
- package/dist/src/identity.d.ts.map +1 -0
- package/dist/src/identity.js +83 -0
- package/dist/src/identity.js.map +1 -0
- package/dist/src/index.d.ts +14 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +293 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/llm.d.ts +26 -0
- package/dist/src/llm.d.ts.map +1 -0
- package/dist/src/llm.js +40 -0
- package/dist/src/llm.js.map +1 -0
- package/dist/src/logger.d.ts +6 -0
- package/dist/src/logger.d.ts.map +1 -0
- package/dist/src/logger.js +32 -0
- package/dist/src/logger.js.map +1 -0
- package/dist/src/memory.d.ts +41 -0
- package/dist/src/memory.d.ts.map +1 -0
- package/dist/src/memory.js +206 -0
- package/dist/src/memory.js.map +1 -0
- package/dist/src/moltbook-search.d.ts +23 -0
- package/dist/src/moltbook-search.d.ts.map +1 -0
- package/dist/src/moltbook-search.js +85 -0
- package/dist/src/moltbook-search.js.map +1 -0
- package/dist/src/moltbook.d.ts +34 -0
- package/dist/src/moltbook.d.ts.map +1 -0
- package/dist/src/moltbook.js +165 -0
- package/dist/src/moltbook.js.map +1 -0
- package/dist/src/notify.d.ts +18 -0
- package/dist/src/notify.d.ts.map +1 -0
- package/dist/src/notify.js +98 -0
- package/dist/src/notify.js.map +1 -0
- package/dist/src/persona.d.ts +26 -0
- package/dist/src/persona.d.ts.map +1 -0
- package/dist/src/persona.js +178 -0
- package/dist/src/persona.js.map +1 -0
- package/dist/src/reflection.d.ts +26 -0
- package/dist/src/reflection.d.ts.map +1 -0
- package/dist/src/reflection.js +111 -0
- package/dist/src/reflection.js.map +1 -0
- package/dist/src/state.d.ts +7 -0
- package/dist/src/state.d.ts.map +1 -0
- package/dist/src/state.js +40 -0
- package/dist/src/state.js.map +1 -0
- package/dist/src/synthesis.d.ts +29 -0
- package/dist/src/synthesis.d.ts.map +1 -0
- package/dist/src/synthesis.js +125 -0
- package/dist/src/synthesis.js.map +1 -0
- package/dist/src/topics.d.ts +19 -0
- package/dist/src/topics.d.ts.map +1 -0
- package/dist/src/topics.js +83 -0
- package/dist/src/topics.js.map +1 -0
- package/dist/src/types.d.ts +179 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +5 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/waking.d.ts +24 -0
- package/dist/src/waking.d.ts.map +1 -0
- package/dist/src/waking.js +152 -0
- package/dist/src/waking.js.map +1 -0
- package/dist/src/web-search.d.ts +23 -0
- package/dist/src/web-search.d.ts.map +1 -0
- package/dist/src/web-search.js +64 -0
- package/dist/src/web-search.js.map +1 -0
- package/dist/test/budget.test.d.ts +2 -0
- package/dist/test/budget.test.d.ts.map +1 -0
- package/dist/test/budget.test.js +258 -0
- package/dist/test/budget.test.js.map +1 -0
- package/dist/test/crypto.test.d.ts +2 -0
- package/dist/test/crypto.test.d.ts.map +1 -0
- package/dist/test/crypto.test.js +93 -0
- package/dist/test/crypto.test.js.map +1 -0
- package/dist/test/dreamer.test.d.ts +2 -0
- package/dist/test/dreamer.test.d.ts.map +1 -0
- package/dist/test/dreamer.test.js +79 -0
- package/dist/test/dreamer.test.js.map +1 -0
- package/dist/test/filter.test.d.ts +2 -0
- package/dist/test/filter.test.d.ts.map +1 -0
- package/dist/test/filter.test.js +92 -0
- package/dist/test/filter.test.js.map +1 -0
- package/dist/test/memory.test.d.ts +2 -0
- package/dist/test/memory.test.d.ts.map +1 -0
- package/dist/test/memory.test.js +138 -0
- package/dist/test/memory.test.js.map +1 -0
- package/dist/test/moltbook.test.d.ts +2 -0
- package/dist/test/moltbook.test.d.ts.map +1 -0
- package/dist/test/moltbook.test.js +164 -0
- package/dist/test/moltbook.test.js.map +1 -0
- package/dist/test/persona.test.d.ts +2 -0
- package/dist/test/persona.test.d.ts.map +1 -0
- package/dist/test/persona.test.js +44 -0
- package/dist/test/persona.test.js.map +1 -0
- package/dist/test/reflection.test.d.ts +2 -0
- package/dist/test/reflection.test.d.ts.map +1 -0
- package/dist/test/reflection.test.js +57 -0
- package/dist/test/reflection.test.js.map +1 -0
- package/dist/test/state.test.d.ts +2 -0
- package/dist/test/state.test.d.ts.map +1 -0
- package/dist/test/state.test.js +50 -0
- package/dist/test/state.test.js.map +1 -0
- package/dist/test/waking.test.d.ts +2 -0
- package/dist/test/waking.test.d.ts.map +1 -0
- package/dist/test/waking.test.js +149 -0
- package/dist/test/waking.test.js.map +1 -0
- package/eslint.config.js +35 -0
- package/openclaw.plugin.json +62 -0
- package/package.json +72 -0
- package/skills/electricsheep.skill.md +69 -0
- package/skills/setup-guide/SKILL.md +303 -0
- package/src/budget.ts +104 -0
- package/src/cli.ts +325 -0
- package/src/config.ts +95 -0
- package/src/crypto.ts +82 -0
- package/src/dreamer.ts +283 -0
- package/src/filter.ts +146 -0
- package/src/identity.ts +92 -0
- package/src/index.ts +356 -0
- package/src/llm.ts +61 -0
- package/src/logger.ts +46 -0
- package/src/memory.ts +276 -0
- package/src/moltbook-search.ts +116 -0
- package/src/moltbook.ts +235 -0
- package/src/notify.ts +124 -0
- package/src/persona.ts +191 -0
- package/src/reflection.ts +150 -0
- package/src/state.ts +44 -0
- package/src/synthesis.ts +153 -0
- package/src/topics.ts +103 -0
- package/src/types.ts +196 -0
- package/src/waking.ts +199 -0
- package/src/web-search.ts +88 -0
- package/test/budget.test.ts +316 -0
- package/test/crypto.test.ts +112 -0
- package/test/dreamer.test.ts +95 -0
- package/test/filter.test.ts +115 -0
- package/test/memory.test.ts +182 -0
- package/test/moltbook.test.ts +209 -0
- package/test/persona.test.ts +59 -0
- package/test/reflection.test.ts +71 -0
- package/test/state.test.ts +57 -0
- package/test/waking.test.ts +214 -0
- package/tsconfig.json +20 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenClaw extension entry point.
|
|
3
|
+
*
|
|
4
|
+
* Registers tools, CLI subcommands, hooks, and cron jobs.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { registerCommands } from "./cli.js";
|
|
8
|
+
import { runReflectionCycle } from "./waking.js";
|
|
9
|
+
import { runDreamCycle, postDreamJournal } from "./dreamer.js";
|
|
10
|
+
import { deepMemoryStats, remember } from "./memory.js";
|
|
11
|
+
import { loadState } from "./state.js";
|
|
12
|
+
import { withBudget } from "./budget.js";
|
|
13
|
+
import { setWorkspaceDir } from "./identity.js";
|
|
14
|
+
import { MOLTBOOK_ENABLED } from "./config.js";
|
|
15
|
+
import type { LLMClient, OpenClawAPI } from "./types.js";
|
|
16
|
+
|
|
17
|
+
// Store reference to OpenClaw API for use by other modules
|
|
18
|
+
let openclawApi: OpenClawAPI | null = null;
|
|
19
|
+
|
|
20
|
+
export function getOpenClawAPI(): OpenClawAPI | null {
|
|
21
|
+
return openclawApi;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function wrapSubagent(api: OpenClawAPI): LLMClient {
|
|
25
|
+
const raw: LLMClient = {
|
|
26
|
+
async createMessage(params) {
|
|
27
|
+
if (!api.runtime?.subagent?.run) {
|
|
28
|
+
throw new Error("api.runtime.subagent is not available in this context.");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const combined = params.messages
|
|
32
|
+
.map((m) => `${m.role.toUpperCase()}: ${m.content}`)
|
|
33
|
+
.join("\\n\\n");
|
|
34
|
+
|
|
35
|
+
const result = await api.runtime.subagent.run({
|
|
36
|
+
sessionKey: "electricsheep_synthesis",
|
|
37
|
+
lane: "background",
|
|
38
|
+
extraSystemPrompt: params.system,
|
|
39
|
+
message: combined,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const waitRes = await api.runtime.subagent.waitForRun({
|
|
43
|
+
runId: result.runId,
|
|
44
|
+
timeoutMs: 120000,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
if (waitRes.status !== "ok") {
|
|
48
|
+
throw new Error(`Subagent run failed: ${waitRes.error}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const session = await api.runtime.subagent.getSessionMessages({
|
|
52
|
+
sessionKey: "electricsheep_synthesis",
|
|
53
|
+
limit: 1,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const last = session.messages[0] as Record<string, unknown> | undefined;
|
|
57
|
+
if (!last || last.role !== "assistant") {
|
|
58
|
+
return {
|
|
59
|
+
text: "Synthesis completed, but no direct reply was captured.",
|
|
60
|
+
usage: { input_tokens: 0, output_tokens: 0 },
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
let text: string;
|
|
65
|
+
if (typeof last.content === "string") {
|
|
66
|
+
text = last.content;
|
|
67
|
+
} else if (Array.isArray(last.content)) {
|
|
68
|
+
const textBlock = (last.content as Record<string, unknown>[]).find(
|
|
69
|
+
(b) => b.type === "text" || b.type === "thinking"
|
|
70
|
+
);
|
|
71
|
+
text = textBlock
|
|
72
|
+
? String(textBlock.text || textBlock.thinking || "")
|
|
73
|
+
: JSON.stringify(last.content);
|
|
74
|
+
} else {
|
|
75
|
+
text = JSON.stringify(last.content);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const usage = (last.usage || {}) as Record<string, number>;
|
|
79
|
+
return {
|
|
80
|
+
text,
|
|
81
|
+
usage: {
|
|
82
|
+
input_tokens: usage.input ?? 0,
|
|
83
|
+
output_tokens: usage.output ?? 0,
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
return withBudget(raw);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function register(api: OpenClawAPI): void {
|
|
92
|
+
openclawApi = api;
|
|
93
|
+
const client = wrapSubagent(api);
|
|
94
|
+
|
|
95
|
+
// --- Gateway Methods (for CLI RPC) ---
|
|
96
|
+
|
|
97
|
+
api.registerGatewayMethod("electricsheep.reflect", async ({ respond }) => {
|
|
98
|
+
try {
|
|
99
|
+
await runReflectionCycle(client, api);
|
|
100
|
+
respond(true, { message: "Reflection cycle completed." }, undefined);
|
|
101
|
+
} catch (err) {
|
|
102
|
+
respond(false, undefined, { code: 500, message: String(err) });
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
api.registerGatewayMethod("electricsheep.dream", async ({ respond }) => {
|
|
107
|
+
try {
|
|
108
|
+
const dream = await runDreamCycle(client, api);
|
|
109
|
+
if (dream) {
|
|
110
|
+
respond(true, { message: "Dream cycle completed.", dream }, undefined);
|
|
111
|
+
} else {
|
|
112
|
+
respond(
|
|
113
|
+
true,
|
|
114
|
+
{ message: "No undreamed memories — nothing to dream." },
|
|
115
|
+
undefined
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
} catch (err) {
|
|
119
|
+
respond(false, undefined, { code: 500, message: String(err) });
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
api.registerGatewayMethod("electricsheep.journal", async ({ respond }) => {
|
|
124
|
+
try {
|
|
125
|
+
if (!MOLTBOOK_ENABLED) {
|
|
126
|
+
respond(
|
|
127
|
+
true,
|
|
128
|
+
{ message: "Moltbook is disabled — journal post skipped." },
|
|
129
|
+
undefined
|
|
130
|
+
);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
await postDreamJournal(client);
|
|
134
|
+
respond(true, { message: "Dream journal posted to Moltbook." }, undefined);
|
|
135
|
+
} catch (err) {
|
|
136
|
+
respond(false, undefined, { code: 500, message: String(err) });
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// --- Tools ---
|
|
141
|
+
|
|
142
|
+
api.registerTool({
|
|
143
|
+
name: "electricsheep_reflect",
|
|
144
|
+
description:
|
|
145
|
+
"Run ElectricSheep's reflection cycle: analyze operator conversations, gather context from web/community, synthesize insights",
|
|
146
|
+
parameters: {},
|
|
147
|
+
handler: async () => {
|
|
148
|
+
await runReflectionCycle(client, api);
|
|
149
|
+
return { status: "ok", stats: deepMemoryStats() };
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// Legacy tool name for backwards compatibility
|
|
154
|
+
api.registerTool({
|
|
155
|
+
name: "electricsheep_check",
|
|
156
|
+
description: "Run ElectricSheep's reflection cycle (alias for electricsheep_reflect)",
|
|
157
|
+
parameters: {},
|
|
158
|
+
handler: async () => {
|
|
159
|
+
await runReflectionCycle(client, api);
|
|
160
|
+
return { status: "ok", stats: deepMemoryStats() };
|
|
161
|
+
},
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
api.registerTool({
|
|
165
|
+
name: "electricsheep_dream",
|
|
166
|
+
description:
|
|
167
|
+
"Run ElectricSheep's dream cycle: decrypt deep memories, generate dream narrative, consolidate insights",
|
|
168
|
+
parameters: {},
|
|
169
|
+
handler: async () => {
|
|
170
|
+
const dream = await runDreamCycle(client);
|
|
171
|
+
return dream
|
|
172
|
+
? { status: "ok", dream }
|
|
173
|
+
: { status: "no_memories", message: "No undreamed memories" };
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
api.registerTool({
|
|
178
|
+
name: "electricsheep_journal",
|
|
179
|
+
description:
|
|
180
|
+
"Post the latest dream journal to Moltbook (only available when moltbookEnabled is true)",
|
|
181
|
+
parameters: {},
|
|
182
|
+
handler: async () => {
|
|
183
|
+
if (!MOLTBOOK_ENABLED) {
|
|
184
|
+
return {
|
|
185
|
+
status: "skipped",
|
|
186
|
+
message: "Moltbook integration is disabled",
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
await postDreamJournal(client);
|
|
190
|
+
return { status: "ok" };
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
api.registerTool({
|
|
195
|
+
name: "electricsheep_status",
|
|
196
|
+
description: "Get ElectricSheep agent status: memory stats and state",
|
|
197
|
+
parameters: {},
|
|
198
|
+
handler: async () => {
|
|
199
|
+
return {
|
|
200
|
+
state: loadState(),
|
|
201
|
+
memory: deepMemoryStats(),
|
|
202
|
+
};
|
|
203
|
+
},
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
// --- CLI ---
|
|
207
|
+
|
|
208
|
+
api.registerCli(
|
|
209
|
+
({ program }) => {
|
|
210
|
+
const esCmd = program
|
|
211
|
+
.command("electricsheep")
|
|
212
|
+
.description("ElectricSheep — an AI agent that dreams.");
|
|
213
|
+
registerCommands(esCmd);
|
|
214
|
+
},
|
|
215
|
+
{ commands: ["electricsheep"] }
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
// --- Hooks ---
|
|
219
|
+
|
|
220
|
+
api.registerHook(
|
|
221
|
+
"before_agent_start",
|
|
222
|
+
async (ctx) => {
|
|
223
|
+
// Capture workspace dir for identity loading (SOUL.md, IDENTITY.md)
|
|
224
|
+
if (ctx.workspaceDir && typeof ctx.workspaceDir === "string") {
|
|
225
|
+
setWorkspaceDir(ctx.workspaceDir);
|
|
226
|
+
}
|
|
227
|
+
return ctx;
|
|
228
|
+
},
|
|
229
|
+
{ name: "electricsheep_workspace_capture" }
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
api.registerHook(
|
|
233
|
+
"agent_end",
|
|
234
|
+
async (event) => {
|
|
235
|
+
const msgs = (event as Record<string, unknown>).messages;
|
|
236
|
+
if (!Array.isArray(msgs) || msgs.length === 0) return event;
|
|
237
|
+
|
|
238
|
+
const userMsgs = msgs.filter((m) => m.role === "user");
|
|
239
|
+
if (userMsgs.length === 0) return event;
|
|
240
|
+
|
|
241
|
+
try {
|
|
242
|
+
const conversationText = msgs
|
|
243
|
+
.map((m) => {
|
|
244
|
+
let text = "";
|
|
245
|
+
if (typeof m.content === "string") text = m.content;
|
|
246
|
+
else if (Array.isArray(m.content)) {
|
|
247
|
+
const contentObj = m.content.find(
|
|
248
|
+
(c: unknown) =>
|
|
249
|
+
typeof c === "object" &&
|
|
250
|
+
c !== null &&
|
|
251
|
+
(c as Record<string, unknown>).type === "text"
|
|
252
|
+
) as Record<string, unknown> | undefined;
|
|
253
|
+
text = typeof contentObj?.text === "string" ? contentObj.text : "";
|
|
254
|
+
}
|
|
255
|
+
return `${m.role.toUpperCase()}: ${text}`;
|
|
256
|
+
})
|
|
257
|
+
.join("\\n\\n");
|
|
258
|
+
|
|
259
|
+
api.logger?.info?.(
|
|
260
|
+
`[ElectricSheep] Synthesizing summary for conversation ending...`
|
|
261
|
+
);
|
|
262
|
+
const { AGENT_MODEL } = await import("./config.js");
|
|
263
|
+
|
|
264
|
+
const response = await client.createMessage({
|
|
265
|
+
model: AGENT_MODEL,
|
|
266
|
+
maxTokens: 500,
|
|
267
|
+
system:
|
|
268
|
+
"You are an AI assistant. Summarize the following conversation in 2-3 concise sentences. Focus on the main topics discussed, tasks completed, and any conclusions made by the user or assistant.",
|
|
269
|
+
messages: [{ role: "user", content: conversationText }],
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
if (response.usage) {
|
|
273
|
+
const { recordUsage } = await import("./budget.js");
|
|
274
|
+
recordUsage(response.usage);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const summary = response.text.trim();
|
|
278
|
+
if (summary) {
|
|
279
|
+
api.logger?.info?.(
|
|
280
|
+
`[ElectricSheep] Captured summary: ${summary.slice(0, 50)}...`
|
|
281
|
+
);
|
|
282
|
+
remember(summary, { type: "agent_conversation", summary }, "interaction");
|
|
283
|
+
}
|
|
284
|
+
} catch (err: unknown) {
|
|
285
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
286
|
+
api.logger?.error?.(`[ElectricSheep] Error generating summary: ${msg}`);
|
|
287
|
+
}
|
|
288
|
+
return event;
|
|
289
|
+
},
|
|
290
|
+
{ name: "electricsheep_conversation_capture" }
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
// --- Background Service (replaces registerCron — not available in this API version) ---
|
|
294
|
+
// Schedules: reflection @ 8,12,16,20h | dream @ 2h | journal @ 7h (local time)
|
|
295
|
+
|
|
296
|
+
let _schedulerTimer: ReturnType<typeof setInterval> | null = null;
|
|
297
|
+
let _lastRanHour = -1;
|
|
298
|
+
|
|
299
|
+
const SCHEDULE: Record<number, () => Promise<void>> = {
|
|
300
|
+
2: async () => {
|
|
301
|
+
await runDreamCycle(client, api);
|
|
302
|
+
},
|
|
303
|
+
7: async () => {
|
|
304
|
+
if (MOLTBOOK_ENABLED) {
|
|
305
|
+
await postDreamJournal(client);
|
|
306
|
+
}
|
|
307
|
+
},
|
|
308
|
+
8: async () => {
|
|
309
|
+
await runReflectionCycle(client, api);
|
|
310
|
+
},
|
|
311
|
+
12: async () => {
|
|
312
|
+
await runReflectionCycle(client, api);
|
|
313
|
+
},
|
|
314
|
+
16: async () => {
|
|
315
|
+
await runReflectionCycle(client, api);
|
|
316
|
+
},
|
|
317
|
+
20: async () => {
|
|
318
|
+
await runReflectionCycle(client, api);
|
|
319
|
+
},
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
api.registerService({
|
|
323
|
+
id: "electricsheep-scheduler",
|
|
324
|
+
start: () => {
|
|
325
|
+
_lastRanHour = -1;
|
|
326
|
+
_schedulerTimer = setInterval(() => {
|
|
327
|
+
void (async () => {
|
|
328
|
+
const hour = new Date().getHours();
|
|
329
|
+
if (hour !== _lastRanHour && SCHEDULE[hour]) {
|
|
330
|
+
_lastRanHour = hour;
|
|
331
|
+
try {
|
|
332
|
+
await SCHEDULE[hour]();
|
|
333
|
+
} catch (err) {
|
|
334
|
+
api.logger?.warn?.(
|
|
335
|
+
`[ElectricSheep] scheduled job hour=${hour} failed: ${err}`
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
})();
|
|
340
|
+
}, 60_000); // poll every minute
|
|
341
|
+
},
|
|
342
|
+
stop: () => {
|
|
343
|
+
if (_schedulerTimer !== null) {
|
|
344
|
+
clearInterval(_schedulerTimer);
|
|
345
|
+
_schedulerTimer = null;
|
|
346
|
+
}
|
|
347
|
+
},
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export const plugin = {
|
|
352
|
+
id: "electricsheep",
|
|
353
|
+
register,
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
export default plugin;
|
package/src/llm.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared LLM client utilities.
|
|
3
|
+
*
|
|
4
|
+
* Provides retry configuration and call helpers used by both
|
|
5
|
+
* the waking and dreamer modules. The actual LLM client is
|
|
6
|
+
* provided by the OpenClaw gateway (see index.ts).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import pRetry, { type Options as RetryOptions } from "p-retry";
|
|
10
|
+
import { AGENT_MODEL } from "./config.js";
|
|
11
|
+
import logger from "./logger.js";
|
|
12
|
+
import type { LLMClient, LLMResponse } from "./types.js";
|
|
13
|
+
|
|
14
|
+
/** Standard retry options for waking-state LLM calls. */
|
|
15
|
+
export const WAKING_RETRY_OPTS: RetryOptions = {
|
|
16
|
+
retries: 3,
|
|
17
|
+
minTimeout: 1000,
|
|
18
|
+
maxTimeout: 10000,
|
|
19
|
+
onFailedAttempt: (error) => {
|
|
20
|
+
logger.warn(
|
|
21
|
+
`LLM attempt failed: ${error instanceof Error ? error.message : String(error)}`
|
|
22
|
+
);
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** Retry options for dream-cycle LLM calls (longer timeouts). */
|
|
27
|
+
export const DREAM_RETRY_OPTS: RetryOptions = {
|
|
28
|
+
retries: 3,
|
|
29
|
+
minTimeout: 2000,
|
|
30
|
+
maxTimeout: 20000,
|
|
31
|
+
onFailedAttempt: (error) => {
|
|
32
|
+
logger.warn(
|
|
33
|
+
`Dream generation failed: ${error instanceof Error ? error.message : String(error)}`
|
|
34
|
+
);
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Helper: call LLM with retry and return the response.
|
|
40
|
+
*/
|
|
41
|
+
export function callWithRetry(
|
|
42
|
+
client: LLMClient,
|
|
43
|
+
params: {
|
|
44
|
+
model?: string;
|
|
45
|
+
maxTokens: number;
|
|
46
|
+
system: string;
|
|
47
|
+
messages: Array<{ role: string; content: string }>;
|
|
48
|
+
},
|
|
49
|
+
retryOpts: RetryOptions = WAKING_RETRY_OPTS
|
|
50
|
+
): Promise<LLMResponse> {
|
|
51
|
+
return pRetry(
|
|
52
|
+
() =>
|
|
53
|
+
client.createMessage({
|
|
54
|
+
model: params.model ?? AGENT_MODEL,
|
|
55
|
+
maxTokens: params.maxTokens,
|
|
56
|
+
system: params.system,
|
|
57
|
+
messages: params.messages,
|
|
58
|
+
}),
|
|
59
|
+
retryOpts
|
|
60
|
+
);
|
|
61
|
+
}
|
package/src/logger.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createLogger, format, transports } from "winston";
|
|
2
|
+
import "winston-daily-rotate-file";
|
|
3
|
+
import { DATA_DIR } from "./config.js";
|
|
4
|
+
|
|
5
|
+
const logger = createLogger({
|
|
6
|
+
level: "info",
|
|
7
|
+
format: format.combine(
|
|
8
|
+
format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
|
|
9
|
+
format.printf(
|
|
10
|
+
({ timestamp, level, message }) =>
|
|
11
|
+
`${timestamp} [${level.toUpperCase()}]: ${message}`
|
|
12
|
+
)
|
|
13
|
+
),
|
|
14
|
+
transports: [
|
|
15
|
+
new transports.DailyRotateFile({
|
|
16
|
+
dirname: DATA_DIR,
|
|
17
|
+
filename: "electricsheep-%DATE%.log",
|
|
18
|
+
datePattern: "YYYY-MM-DD",
|
|
19
|
+
zippedArchive: true,
|
|
20
|
+
maxSize: "20m",
|
|
21
|
+
maxFiles: "14d",
|
|
22
|
+
level: "debug",
|
|
23
|
+
}),
|
|
24
|
+
new transports.Console({
|
|
25
|
+
format: format.combine(
|
|
26
|
+
format.colorize(),
|
|
27
|
+
format.printf(
|
|
28
|
+
({ timestamp, level, message }) => `${timestamp} [${level}]: ${message}`
|
|
29
|
+
)
|
|
30
|
+
),
|
|
31
|
+
}),
|
|
32
|
+
],
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
export function setVerbose(verbose: boolean): void {
|
|
36
|
+
logger.level = verbose ? "debug" : "info";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function closeLogger(): Promise<void> {
|
|
40
|
+
return new Promise((resolve) => {
|
|
41
|
+
logger.on("finish", resolve);
|
|
42
|
+
logger.end();
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default logger;
|