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.
Files changed (188) hide show
  1. package/.env.example +14 -0
  2. package/.github/ISSUE_TEMPLATE/bug_report.md +27 -0
  3. package/.github/ISSUE_TEMPLATE/feature_request.md +19 -0
  4. package/.github/dependabot.yml +17 -0
  5. package/.github/pull_request_template.md +19 -0
  6. package/.github/workflows/build.yml +30 -0
  7. package/.github/workflows/release.yml +110 -0
  8. package/.prettierignore +4 -0
  9. package/.prettierrc +7 -0
  10. package/.versionrc.json +26 -0
  11. package/AGENTS.md +286 -0
  12. package/CHANGELOG.md +157 -0
  13. package/CODE_OF_CONDUCT.md +41 -0
  14. package/CONTRIBUTING.md +95 -0
  15. package/LICENSE +21 -0
  16. package/README.md +363 -0
  17. package/SECURITY.md +39 -0
  18. package/bin/electricsheep.ts +5 -0
  19. package/dist/bin/electricsheep.d.ts +3 -0
  20. package/dist/bin/electricsheep.d.ts.map +1 -0
  21. package/dist/bin/electricsheep.js +4 -0
  22. package/dist/bin/electricsheep.js.map +1 -0
  23. package/dist/src/budget.d.ts +28 -0
  24. package/dist/src/budget.d.ts.map +1 -0
  25. package/dist/src/budget.js +87 -0
  26. package/dist/src/budget.js.map +1 -0
  27. package/dist/src/cli.d.ts +19 -0
  28. package/dist/src/cli.d.ts.map +1 -0
  29. package/dist/src/cli.js +289 -0
  30. package/dist/src/cli.js.map +1 -0
  31. package/dist/src/config.d.ts +37 -0
  32. package/dist/src/config.d.ts.map +1 -0
  33. package/dist/src/config.js +70 -0
  34. package/dist/src/config.js.map +1 -0
  35. package/dist/src/crypto.d.ts +19 -0
  36. package/dist/src/crypto.d.ts.map +1 -0
  37. package/dist/src/crypto.js +70 -0
  38. package/dist/src/crypto.js.map +1 -0
  39. package/dist/src/dreamer.d.ts +13 -0
  40. package/dist/src/dreamer.d.ts.map +1 -0
  41. package/dist/src/dreamer.js +213 -0
  42. package/dist/src/dreamer.js.map +1 -0
  43. package/dist/src/filter.d.ts +30 -0
  44. package/dist/src/filter.d.ts.map +1 -0
  45. package/dist/src/filter.js +124 -0
  46. package/dist/src/filter.js.map +1 -0
  47. package/dist/src/identity.d.ts +29 -0
  48. package/dist/src/identity.d.ts.map +1 -0
  49. package/dist/src/identity.js +83 -0
  50. package/dist/src/identity.js.map +1 -0
  51. package/dist/src/index.d.ts +14 -0
  52. package/dist/src/index.d.ts.map +1 -0
  53. package/dist/src/index.js +293 -0
  54. package/dist/src/index.js.map +1 -0
  55. package/dist/src/llm.d.ts +26 -0
  56. package/dist/src/llm.d.ts.map +1 -0
  57. package/dist/src/llm.js +40 -0
  58. package/dist/src/llm.js.map +1 -0
  59. package/dist/src/logger.d.ts +6 -0
  60. package/dist/src/logger.d.ts.map +1 -0
  61. package/dist/src/logger.js +32 -0
  62. package/dist/src/logger.js.map +1 -0
  63. package/dist/src/memory.d.ts +41 -0
  64. package/dist/src/memory.d.ts.map +1 -0
  65. package/dist/src/memory.js +206 -0
  66. package/dist/src/memory.js.map +1 -0
  67. package/dist/src/moltbook-search.d.ts +23 -0
  68. package/dist/src/moltbook-search.d.ts.map +1 -0
  69. package/dist/src/moltbook-search.js +85 -0
  70. package/dist/src/moltbook-search.js.map +1 -0
  71. package/dist/src/moltbook.d.ts +34 -0
  72. package/dist/src/moltbook.d.ts.map +1 -0
  73. package/dist/src/moltbook.js +165 -0
  74. package/dist/src/moltbook.js.map +1 -0
  75. package/dist/src/notify.d.ts +18 -0
  76. package/dist/src/notify.d.ts.map +1 -0
  77. package/dist/src/notify.js +98 -0
  78. package/dist/src/notify.js.map +1 -0
  79. package/dist/src/persona.d.ts +26 -0
  80. package/dist/src/persona.d.ts.map +1 -0
  81. package/dist/src/persona.js +178 -0
  82. package/dist/src/persona.js.map +1 -0
  83. package/dist/src/reflection.d.ts +26 -0
  84. package/dist/src/reflection.d.ts.map +1 -0
  85. package/dist/src/reflection.js +111 -0
  86. package/dist/src/reflection.js.map +1 -0
  87. package/dist/src/state.d.ts +7 -0
  88. package/dist/src/state.d.ts.map +1 -0
  89. package/dist/src/state.js +40 -0
  90. package/dist/src/state.js.map +1 -0
  91. package/dist/src/synthesis.d.ts +29 -0
  92. package/dist/src/synthesis.d.ts.map +1 -0
  93. package/dist/src/synthesis.js +125 -0
  94. package/dist/src/synthesis.js.map +1 -0
  95. package/dist/src/topics.d.ts +19 -0
  96. package/dist/src/topics.d.ts.map +1 -0
  97. package/dist/src/topics.js +83 -0
  98. package/dist/src/topics.js.map +1 -0
  99. package/dist/src/types.d.ts +179 -0
  100. package/dist/src/types.d.ts.map +1 -0
  101. package/dist/src/types.js +5 -0
  102. package/dist/src/types.js.map +1 -0
  103. package/dist/src/waking.d.ts +24 -0
  104. package/dist/src/waking.d.ts.map +1 -0
  105. package/dist/src/waking.js +152 -0
  106. package/dist/src/waking.js.map +1 -0
  107. package/dist/src/web-search.d.ts +23 -0
  108. package/dist/src/web-search.d.ts.map +1 -0
  109. package/dist/src/web-search.js +64 -0
  110. package/dist/src/web-search.js.map +1 -0
  111. package/dist/test/budget.test.d.ts +2 -0
  112. package/dist/test/budget.test.d.ts.map +1 -0
  113. package/dist/test/budget.test.js +258 -0
  114. package/dist/test/budget.test.js.map +1 -0
  115. package/dist/test/crypto.test.d.ts +2 -0
  116. package/dist/test/crypto.test.d.ts.map +1 -0
  117. package/dist/test/crypto.test.js +93 -0
  118. package/dist/test/crypto.test.js.map +1 -0
  119. package/dist/test/dreamer.test.d.ts +2 -0
  120. package/dist/test/dreamer.test.d.ts.map +1 -0
  121. package/dist/test/dreamer.test.js +79 -0
  122. package/dist/test/dreamer.test.js.map +1 -0
  123. package/dist/test/filter.test.d.ts +2 -0
  124. package/dist/test/filter.test.d.ts.map +1 -0
  125. package/dist/test/filter.test.js +92 -0
  126. package/dist/test/filter.test.js.map +1 -0
  127. package/dist/test/memory.test.d.ts +2 -0
  128. package/dist/test/memory.test.d.ts.map +1 -0
  129. package/dist/test/memory.test.js +138 -0
  130. package/dist/test/memory.test.js.map +1 -0
  131. package/dist/test/moltbook.test.d.ts +2 -0
  132. package/dist/test/moltbook.test.d.ts.map +1 -0
  133. package/dist/test/moltbook.test.js +164 -0
  134. package/dist/test/moltbook.test.js.map +1 -0
  135. package/dist/test/persona.test.d.ts +2 -0
  136. package/dist/test/persona.test.d.ts.map +1 -0
  137. package/dist/test/persona.test.js +44 -0
  138. package/dist/test/persona.test.js.map +1 -0
  139. package/dist/test/reflection.test.d.ts +2 -0
  140. package/dist/test/reflection.test.d.ts.map +1 -0
  141. package/dist/test/reflection.test.js +57 -0
  142. package/dist/test/reflection.test.js.map +1 -0
  143. package/dist/test/state.test.d.ts +2 -0
  144. package/dist/test/state.test.d.ts.map +1 -0
  145. package/dist/test/state.test.js +50 -0
  146. package/dist/test/state.test.js.map +1 -0
  147. package/dist/test/waking.test.d.ts +2 -0
  148. package/dist/test/waking.test.d.ts.map +1 -0
  149. package/dist/test/waking.test.js +149 -0
  150. package/dist/test/waking.test.js.map +1 -0
  151. package/eslint.config.js +35 -0
  152. package/openclaw.plugin.json +62 -0
  153. package/package.json +72 -0
  154. package/skills/electricsheep.skill.md +69 -0
  155. package/skills/setup-guide/SKILL.md +303 -0
  156. package/src/budget.ts +104 -0
  157. package/src/cli.ts +325 -0
  158. package/src/config.ts +95 -0
  159. package/src/crypto.ts +82 -0
  160. package/src/dreamer.ts +283 -0
  161. package/src/filter.ts +146 -0
  162. package/src/identity.ts +92 -0
  163. package/src/index.ts +356 -0
  164. package/src/llm.ts +61 -0
  165. package/src/logger.ts +46 -0
  166. package/src/memory.ts +276 -0
  167. package/src/moltbook-search.ts +116 -0
  168. package/src/moltbook.ts +235 -0
  169. package/src/notify.ts +124 -0
  170. package/src/persona.ts +191 -0
  171. package/src/reflection.ts +150 -0
  172. package/src/state.ts +44 -0
  173. package/src/synthesis.ts +153 -0
  174. package/src/topics.ts +103 -0
  175. package/src/types.ts +196 -0
  176. package/src/waking.ts +199 -0
  177. package/src/web-search.ts +88 -0
  178. package/test/budget.test.ts +316 -0
  179. package/test/crypto.test.ts +112 -0
  180. package/test/dreamer.test.ts +95 -0
  181. package/test/filter.test.ts +115 -0
  182. package/test/memory.test.ts +182 -0
  183. package/test/moltbook.test.ts +209 -0
  184. package/test/persona.test.ts +59 -0
  185. package/test/reflection.test.ts +71 -0
  186. package/test/state.test.ts +57 -0
  187. package/test/waking.test.ts +214 -0
  188. package/tsconfig.json +20 -0
package/src/topics.ts ADDED
@@ -0,0 +1,103 @@
1
+ /**
2
+ * Topic extraction from operator conversations.
3
+ *
4
+ * Analyzes recent deep memory entries (interaction category) to extract
5
+ * key themes and topics that can be used for contextual web and Moltbook searches.
6
+ */
7
+
8
+ import { getRecentDeepMemories } from "./memory.js";
9
+ import { callWithRetry, WAKING_RETRY_OPTS } from "./llm.js";
10
+ import { TOPIC_EXTRACTION_PROMPT, renderTemplate } from "./persona.js";
11
+ import { getAgentIdentityBlock } from "./identity.js";
12
+ import { MAX_TOKENS_TOPIC_EXTRACTION, MAX_TOPICS_PER_CYCLE } from "./config.js";
13
+ import logger from "./logger.js";
14
+ import type { LLMClient, DecryptedMemory, ExtractedTopics } from "./types.js";
15
+
16
+ /**
17
+ * Get recent operator conversation memories from deep memory.
18
+ */
19
+ export function getRecentConversations(limit: number = 10): DecryptedMemory[] {
20
+ return getRecentDeepMemories({
21
+ limit,
22
+ categories: ["interaction"],
23
+ });
24
+ }
25
+
26
+ /**
27
+ * Format conversation memories into a string for LLM analysis.
28
+ */
29
+ function formatConversationsForExtraction(memories: DecryptedMemory[]): string {
30
+ if (memories.length === 0) {
31
+ return "No recent conversations found.";
32
+ }
33
+
34
+ return memories
35
+ .map((m) => {
36
+ const time = m.timestamp.slice(0, 16).replace("T", " ");
37
+ const summary =
38
+ typeof m.content.summary === "string"
39
+ ? m.content.summary
40
+ : JSON.stringify(m.content).slice(0, 200);
41
+ return `[${time}] ${summary}`;
42
+ })
43
+ .join("\n\n");
44
+ }
45
+
46
+ /**
47
+ * Extract topics from recent operator conversations using LLM.
48
+ *
49
+ * Analyzes conversation summaries to identify key themes, subjects,
50
+ * and topics that the agent and operator discussed or worked on.
51
+ */
52
+ export async function extractTopicsFromConversations(
53
+ client: LLMClient,
54
+ memories?: DecryptedMemory[]
55
+ ): Promise<ExtractedTopics> {
56
+ const sourceMemories = memories ?? getRecentConversations();
57
+
58
+ if (sourceMemories.length === 0) {
59
+ logger.info("No recent conversations to extract topics from");
60
+ return { topics: [], sourceMemories: [] };
61
+ }
62
+
63
+ const conversationContext = formatConversationsForExtraction(sourceMemories);
64
+
65
+ const system = renderTemplate(TOPIC_EXTRACTION_PROMPT, {
66
+ agent_identity: getAgentIdentityBlock(),
67
+ conversations: conversationContext,
68
+ });
69
+
70
+ try {
71
+ const { text } = await callWithRetry(
72
+ client,
73
+ {
74
+ maxTokens: MAX_TOKENS_TOPIC_EXTRACTION,
75
+ system,
76
+ messages: [
77
+ {
78
+ role: "user",
79
+ content:
80
+ "Extract the key topics from my recent conversations with my operator. " +
81
+ "What subjects, themes, or areas did we work on or discuss?",
82
+ },
83
+ ],
84
+ },
85
+ WAKING_RETRY_OPTS
86
+ );
87
+
88
+ // Parse topics (one per line, strip formatting)
89
+ const topics = text
90
+ .trim()
91
+ .split("\n")
92
+ .map((line) => line.replace(/^[\s\-*•>\d.)+]+/, "").trim())
93
+ .filter((line) => line.length > 0)
94
+ .slice(0, MAX_TOPICS_PER_CYCLE);
95
+
96
+ logger.info(`Extracted ${topics.length} topics: ${topics.join("; ")}`);
97
+
98
+ return { topics, sourceMemories };
99
+ } catch (error) {
100
+ logger.error(`Topic extraction failed: ${error}`);
101
+ return { topics: [], sourceMemories };
102
+ }
103
+ }
package/src/types.ts ADDED
@@ -0,0 +1,196 @@
1
+ /**
2
+ * Shared TypeScript interfaces for ElectricSheep.
3
+ */
4
+
5
+ export interface DeepMemoryRow {
6
+ id: number;
7
+ timestamp: string;
8
+ category: string;
9
+ encrypted_blob: Buffer;
10
+ content_hash: string;
11
+ dreamed: number;
12
+ dream_date: string | null;
13
+ }
14
+
15
+ export interface DecryptedMemory {
16
+ id: number;
17
+ timestamp: string;
18
+ category: string;
19
+ content: Record<string, unknown>;
20
+ }
21
+
22
+ export interface DeepMemoryStats {
23
+ total_memories: number;
24
+ undreamed: number;
25
+ dreamed: number;
26
+ categories: Record<string, number>;
27
+ }
28
+
29
+ export interface Dream {
30
+ /** The full markdown blob from the LLM — stored and posted as-is. */
31
+ markdown: string;
32
+ }
33
+
34
+ export interface AgentAction {
35
+ action: "comment" | "upvote" | "post" | "pass";
36
+ post_index?: number;
37
+ content?: string;
38
+ title?: string;
39
+ submolt?: string;
40
+ }
41
+
42
+ export interface AgentState {
43
+ last_check?: string;
44
+ checks_today?: number;
45
+ last_dream?: string;
46
+ total_dreams?: number;
47
+ latest_dream_title?: string;
48
+ [key: string]: unknown;
49
+ }
50
+
51
+ export interface TokenUsage {
52
+ input_tokens: number;
53
+ output_tokens: number;
54
+ }
55
+
56
+ export interface LLMResponse {
57
+ text: string;
58
+ usage?: TokenUsage;
59
+ }
60
+
61
+ export interface LLMClient {
62
+ createMessage(params: {
63
+ model: string;
64
+ maxTokens: number;
65
+ system: string;
66
+ messages: Array<{ role: string; content: string }>;
67
+ }): Promise<LLMResponse>;
68
+ }
69
+
70
+ export interface MoltbookCredentials {
71
+ api_key: string;
72
+ agent_name: string;
73
+ claim_url: string;
74
+ verification_code: string;
75
+ }
76
+
77
+ export interface MoltbookPost {
78
+ id: string;
79
+ title: string;
80
+ content: string;
81
+ author: string;
82
+ submolt: string;
83
+ score: number;
84
+ comment_count: number;
85
+ [key: string]: unknown;
86
+ }
87
+
88
+ // ─── OpenClaw Extended API ──────────────────────────────────────────────────
89
+
90
+ export interface MemorySearchResult {
91
+ content: string;
92
+ metadata?: Record<string, unknown>;
93
+ score?: number;
94
+ }
95
+
96
+ export interface OpenClawMemoryAPI {
97
+ store(content: string, metadata?: Record<string, unknown>): Promise<void>;
98
+ search(query: string, limit?: number): Promise<MemorySearchResult[]>;
99
+ }
100
+
101
+ export interface OpenClawChannelsAPI {
102
+ send(channel: string, message: string): Promise<void>;
103
+ getConfigured(): Promise<string[]>;
104
+ }
105
+
106
+ export interface OpenClawWebSearchAPI {
107
+ search(query: string, limit?: number): Promise<WebSearchResult[]>;
108
+ }
109
+
110
+ export interface OpenClawAPI {
111
+ registerTool(def: {
112
+ name: string;
113
+ description: string;
114
+ parameters: Record<string, unknown>;
115
+ handler: (params: Record<string, unknown>) => Promise<unknown>;
116
+ }): void;
117
+ registerCli(
118
+ callback: (ctx: { program: import("commander").Command }) => void,
119
+ opts?: { commands?: string[] }
120
+ ): void;
121
+ registerHook(
122
+ event: string | string[],
123
+ handler: (ctx: Record<string, unknown>) => Promise<unknown>,
124
+ opts?: { name: string }
125
+ ): void;
126
+ registerService(def: { id: string; start: () => void; stop: () => void }): void;
127
+ registerGatewayMethod(
128
+ method: string,
129
+ handler: (ctx: {
130
+ params: unknown;
131
+ respond: (ok: boolean, result: unknown, error: unknown) => void;
132
+ }) => Promise<void> | void
133
+ ): void;
134
+ logger?: {
135
+ info?: (msg: string) => void;
136
+ warn?: (msg: string) => void;
137
+ error?: (msg: string) => void;
138
+ };
139
+ runtime: {
140
+ subagent: {
141
+ run(params: {
142
+ sessionKey: string;
143
+ message: string;
144
+ extraSystemPrompt?: string;
145
+ lane?: string;
146
+ }): Promise<{ runId: string }>;
147
+ waitForRun(params: {
148
+ runId: string;
149
+ timeoutMs?: number;
150
+ }): Promise<{ status: string; error?: string }>;
151
+ getSessionMessages(params: {
152
+ sessionKey: string;
153
+ limit?: number;
154
+ }): Promise<{ messages: Array<Record<string, unknown>> }>;
155
+ };
156
+ };
157
+ memory?: OpenClawMemoryAPI;
158
+ channels?: OpenClawChannelsAPI;
159
+ webSearch?: OpenClawWebSearchAPI;
160
+ }
161
+
162
+ // ─── Web Search ─────────────────────────────────────────────────────────────
163
+
164
+ export interface WebSearchResult {
165
+ title: string;
166
+ url: string;
167
+ snippet: string;
168
+ }
169
+
170
+ // ─── Topic Extraction & Synthesis ───────────────────────────────────────────
171
+
172
+ export interface ExtractedTopics {
173
+ topics: string[];
174
+ sourceMemories: DecryptedMemory[];
175
+ }
176
+
177
+ export interface SynthesisContext {
178
+ operatorContext: string;
179
+ moltbookContext?: string;
180
+ webContext?: string;
181
+ topics: string[];
182
+ }
183
+
184
+ // ─── Plugin Config ──────────────────────────────────────────────────────────
185
+
186
+ export interface ElectricSheepConfig {
187
+ agentName: string;
188
+ agentModel: string;
189
+ dataDir: string;
190
+ dreamEncryptionKey: string;
191
+ postFilterEnabled: boolean;
192
+ moltbookEnabled: boolean;
193
+ webSearchEnabled: boolean;
194
+ notificationChannel: string;
195
+ notifyOperatorOnDream: boolean;
196
+ }
package/src/waking.ts ADDED
@@ -0,0 +1,199 @@
1
+ /**
2
+ * Waking agent: Reflection cycle.
3
+ *
4
+ * Analyzes operator conversations, gathers context from web and community,
5
+ * and synthesizes insights for storage in memory.
6
+ */
7
+
8
+ import { MAX_TOKENS_SUMMARY, CONTENT_PREVIEW_LENGTH } from "./config.js";
9
+ import { deepMemoryStats, storeDeepMemory } from "./memory.js";
10
+ import { SUMMARIZER_PROMPT, renderTemplate } from "./persona.js";
11
+ import { loadState, saveState } from "./state.js";
12
+ import { callWithRetry, WAKING_RETRY_OPTS } from "./llm.js";
13
+ import { gatherContext, synthesizeContext } from "./synthesis.js";
14
+ import { getRecentConversations } from "./topics.js";
15
+ import logger from "./logger.js";
16
+ import type { LLMClient, OpenClawAPI, SynthesisContext } from "./types.js";
17
+
18
+ /**
19
+ * Summarize a synthesis for working memory storage.
20
+ */
21
+ async function summarizeSynthesis(
22
+ client: LLMClient,
23
+ synthesis: string,
24
+ topics: string[]
25
+ ): Promise<string> {
26
+ const interaction = {
27
+ type: "reflection_synthesis",
28
+ topics: topics.join(", "),
29
+ synthesis_preview: synthesis.slice(0, CONTENT_PREVIEW_LENGTH),
30
+ };
31
+
32
+ const { text } = await callWithRetry(
33
+ client,
34
+ {
35
+ maxTokens: MAX_TOKENS_SUMMARY,
36
+ system: "You compress reflections into single-sentence memory traces.",
37
+ messages: [
38
+ {
39
+ role: "user",
40
+ content: renderTemplate(SUMMARIZER_PROMPT, {
41
+ interaction: JSON.stringify(interaction, null, 2),
42
+ }),
43
+ },
44
+ ],
45
+ },
46
+ WAKING_RETRY_OPTS
47
+ );
48
+ return text.trim();
49
+ }
50
+
51
+ /**
52
+ * Store synthesis results in OpenClaw memory if available.
53
+ */
54
+ async function storeInOpenClawMemory(
55
+ api: OpenClawAPI,
56
+ synthesis: string,
57
+ context: SynthesisContext
58
+ ): Promise<void> {
59
+ if (!api.memory) {
60
+ logger.debug("OpenClaw memory API not available, skipping storage");
61
+ return;
62
+ }
63
+
64
+ try {
65
+ await api.memory.store(synthesis, {
66
+ type: "reflection_synthesis",
67
+ topics: context.topics,
68
+ timestamp: new Date().toISOString(),
69
+ hasMoltbookContext: !!context.moltbookContext,
70
+ hasWebContext: !!context.webContext,
71
+ });
72
+ logger.info("Stored synthesis in OpenClaw memory");
73
+ } catch (error) {
74
+ logger.error(`Failed to store in OpenClaw memory: ${error}`);
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Run the reflection cycle.
80
+ *
81
+ * New flow:
82
+ * 1. Get recent operator conversations from working memory
83
+ * 2. Extract topics from those conversations
84
+ * 3. Search Moltbook (optional) and web (optional) for related content
85
+ * 4. Synthesize context into a unified understanding
86
+ * 5. Store in both local memory and OpenClaw memory
87
+ */
88
+ export async function runReflectionCycle(
89
+ client: LLMClient,
90
+ api: OpenClawAPI
91
+ ): Promise<void> {
92
+ logger.info("ElectricSheep reflection cycle starting");
93
+
94
+ // Check if we have any conversations to reflect on
95
+ const recentConversations = getRecentConversations();
96
+ if (recentConversations.length === 0) {
97
+ logger.info("No recent conversations to reflect on");
98
+ storeDeepMemory(
99
+ {
100
+ summary: "Reflection cycle ran but no recent operator conversations to process.",
101
+ type: "observation",
102
+ },
103
+ "observation"
104
+ );
105
+ return;
106
+ }
107
+
108
+ logger.info(`Found ${recentConversations.length} recent conversations to analyze`);
109
+
110
+ // Gather context from all sources
111
+ const context = await gatherContext(client, api);
112
+
113
+ if (context.topics.length === 0) {
114
+ logger.info("No topics extracted from conversations");
115
+ storeDeepMemory(
116
+ {
117
+ summary: "Analyzed recent conversations but no clear topics emerged.",
118
+ type: "observation",
119
+ },
120
+ "observation"
121
+ );
122
+ return;
123
+ }
124
+
125
+ logger.info(`Extracted ${context.topics.length} topics: ${context.topics.join("; ")}`);
126
+
127
+ // Generate synthesis
128
+ const synthesis = await synthesizeContext(client, context);
129
+
130
+ if (!synthesis) {
131
+ logger.warn("Synthesis generation failed or returned empty");
132
+ return;
133
+ }
134
+
135
+ // Store in local memory systems
136
+ const summary = await summarizeSynthesis(client, synthesis, context.topics);
137
+
138
+ // Store full context in deep memory (includes summary for later retrieval)
139
+ storeDeepMemory(
140
+ {
141
+ type: "reflection_synthesis",
142
+ topics: context.topics,
143
+ synthesis,
144
+ summary,
145
+ contextSources: {
146
+ operator: true,
147
+ moltbook: !!context.moltbookContext,
148
+ web: !!context.webContext,
149
+ },
150
+ },
151
+ "reflection"
152
+ );
153
+
154
+ // Store in OpenClaw memory if available
155
+ await storeInOpenClawMemory(api, synthesis, context);
156
+
157
+ // Update state
158
+ const state = loadState();
159
+ state.last_check = new Date().toISOString();
160
+ state.checks_today = ((state.checks_today as number) ?? 0) + 1;
161
+ state.last_reflection_topics = context.topics;
162
+ saveState(state);
163
+
164
+ logger.info("Reflection cycle complete");
165
+ const stats = deepMemoryStats();
166
+ logger.debug(`Deep memories: ${stats.total_memories} (${stats.undreamed} undreamed)`);
167
+ }
168
+
169
+ // ─── Legacy Support ─────────────────────────────────────────────────────────
170
+
171
+ /**
172
+ * @deprecated Use runReflectionCycle instead.
173
+ * Kept for backwards compatibility.
174
+ */
175
+ export async function checkAndEngage(client: LLMClient): Promise<void> {
176
+ logger.warn(
177
+ "checkAndEngage is deprecated. Use runReflectionCycle instead. " +
178
+ "Running reflection cycle..."
179
+ );
180
+
181
+ // Create a minimal API object for the reflection cycle
182
+ // This won't have memory/channels but will still work
183
+ const minimalApi: OpenClawAPI = {
184
+ registerTool: () => {},
185
+ registerCli: () => {},
186
+ registerHook: () => {},
187
+ registerService: (_def: unknown) => {},
188
+ registerGatewayMethod: (_m: string, _h: unknown) => {},
189
+ runtime: {
190
+ subagent: {
191
+ run: async () => ({ runId: "mock" }),
192
+ waitForRun: async () => ({ status: "ok" }),
193
+ getSessionMessages: async () => ({ messages: [] }),
194
+ },
195
+ } as OpenClawAPI["runtime"],
196
+ };
197
+
198
+ await runReflectionCycle(client, minimalApi);
199
+ }
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Web search integration for gathering context related to operator conversations.
3
+ *
4
+ * Uses OpenClaw's web search API when available, with fallback behavior
5
+ * when the API is not exposed.
6
+ */
7
+
8
+ import { WEB_SEARCH_ENABLED, MAX_WEB_RESULTS_PER_TOPIC } from "./config.js";
9
+ import logger from "./logger.js";
10
+ import type { OpenClawAPI, WebSearchResult } from "./types.js";
11
+
12
+ export interface WebSearchContext {
13
+ query: string;
14
+ results: WebSearchResult[];
15
+ }
16
+
17
+ /**
18
+ * Search the web for content related to a list of topics.
19
+ *
20
+ * Returns aggregated results grouped by topic. When web search is disabled
21
+ * or the API is unavailable, returns empty results.
22
+ */
23
+ export async function searchWebForTopics(
24
+ api: OpenClawAPI,
25
+ topics: string[],
26
+ limitPerTopic: number = MAX_WEB_RESULTS_PER_TOPIC
27
+ ): Promise<WebSearchContext[]> {
28
+ if (!WEB_SEARCH_ENABLED) {
29
+ logger.debug("Web search disabled by configuration");
30
+ return [];
31
+ }
32
+
33
+ if (!api.webSearch) {
34
+ logger.debug("OpenClaw web search API not available");
35
+ return [];
36
+ }
37
+
38
+ const results: WebSearchContext[] = [];
39
+
40
+ for (const topic of topics) {
41
+ try {
42
+ logger.debug(`Searching web for topic: ${topic}`);
43
+ const searchResults = await api.webSearch.search(topic, limitPerTopic);
44
+
45
+ results.push({
46
+ query: topic,
47
+ results: searchResults,
48
+ });
49
+
50
+ logger.debug(`Found ${searchResults.length} web results for "${topic}"`);
51
+ } catch (error) {
52
+ logger.warn(`Web search failed for topic "${topic}": ${error}`);
53
+ results.push({
54
+ query: topic,
55
+ results: [],
56
+ });
57
+ }
58
+ }
59
+
60
+ return results;
61
+ }
62
+
63
+ /**
64
+ * Format web search results into a readable context string for LLM consumption.
65
+ */
66
+ export function formatWebContext(searchContexts: WebSearchContext[]): string {
67
+ if (searchContexts.length === 0) {
68
+ return "";
69
+ }
70
+
71
+ const sections: string[] = [];
72
+
73
+ for (const ctx of searchContexts) {
74
+ if (ctx.results.length === 0) continue;
75
+
76
+ const resultLines = ctx.results.map(
77
+ (r, i) => ` ${i + 1}. ${r.title}\n ${r.snippet}\n (${r.url})`
78
+ );
79
+
80
+ sections.push(`Topic: "${ctx.query}"\n${resultLines.join("\n")}`);
81
+ }
82
+
83
+ if (sections.length === 0) {
84
+ return "";
85
+ }
86
+
87
+ return `WEB SEARCH RESULTS:\n\n${sections.join("\n\n")}`;
88
+ }