screenhand 0.2.0 → 0.3.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 (212) hide show
  1. package/README.md +165 -446
  2. package/bin/darwin-arm64/macos-bridge +0 -0
  3. package/dist/mcp-desktop.js +3615 -400
  4. package/dist/scripts/export-help-center.js +112 -0
  5. package/dist/scripts/marketing-loop.js +117 -0
  6. package/dist/scripts/observer-daemon.js +288 -0
  7. package/dist/scripts/orchestrator-daemon.js +399 -0
  8. package/dist/scripts/threads-campaign.js +208 -0
  9. package/dist/src/community/fetcher.js +109 -0
  10. package/dist/src/community/index.js +6 -0
  11. package/dist/src/community/publisher.js +191 -0
  12. package/dist/src/community/remote-api.js +121 -0
  13. package/dist/src/community/types.js +3 -0
  14. package/dist/src/community/validator.js +95 -0
  15. package/dist/src/context-tracker.js +489 -0
  16. package/dist/src/ingestion/coverage-auditor.js +233 -0
  17. package/dist/src/ingestion/doc-parser.js +164 -0
  18. package/dist/src/ingestion/index.js +8 -0
  19. package/dist/src/ingestion/menu-scanner.js +152 -0
  20. package/dist/src/ingestion/reference-merger.js +186 -0
  21. package/dist/src/ingestion/shortcut-extractor.js +180 -0
  22. package/dist/src/ingestion/tutorial-extractor.js +170 -0
  23. package/dist/src/ingestion/types.js +3 -0
  24. package/dist/src/jobs/manager.js +82 -14
  25. package/dist/src/jobs/runner.js +138 -15
  26. package/dist/src/learning/engine.js +356 -0
  27. package/dist/src/learning/index.js +9 -0
  28. package/dist/src/learning/locator-policy.js +120 -0
  29. package/dist/src/learning/pattern-policy.js +89 -0
  30. package/dist/src/learning/recovery-policy.js +116 -0
  31. package/dist/src/learning/sensor-policy.js +115 -0
  32. package/dist/src/learning/timing-model.js +204 -0
  33. package/dist/src/learning/topology-policy.js +90 -0
  34. package/dist/src/learning/types.js +9 -0
  35. package/dist/src/logging/timeline-logger.js +4 -1
  36. package/dist/src/memory/playbook-seeds.js +200 -0
  37. package/dist/src/memory/recall.js +60 -8
  38. package/dist/src/memory/service.js +30 -5
  39. package/dist/src/memory/store.js +34 -5
  40. package/dist/src/native/bridge-client.js +253 -31
  41. package/dist/src/observer/state.js +199 -0
  42. package/dist/src/observer/types.js +43 -0
  43. package/dist/src/orchestrator/state.js +68 -0
  44. package/dist/src/orchestrator/types.js +22 -0
  45. package/dist/src/perception/ax-source.js +162 -0
  46. package/dist/src/perception/cdp-source.js +162 -0
  47. package/dist/src/perception/coordinator.js +771 -0
  48. package/dist/src/perception/frame-differ.js +287 -0
  49. package/dist/src/perception/index.js +22 -0
  50. package/dist/src/perception/manager.js +199 -0
  51. package/dist/src/perception/types.js +47 -0
  52. package/dist/src/perception/vision-source.js +399 -0
  53. package/dist/src/planner/deterministic.js +298 -0
  54. package/dist/src/planner/executor.js +870 -0
  55. package/dist/src/planner/goal-store.js +92 -0
  56. package/dist/src/planner/index.js +21 -0
  57. package/dist/src/planner/planner.js +520 -0
  58. package/dist/src/planner/tool-registry.js +71 -0
  59. package/dist/src/planner/types.js +22 -0
  60. package/dist/src/platform/explorer.js +213 -0
  61. package/dist/src/platform/help-center-markdown.js +527 -0
  62. package/dist/src/platform/learner.js +257 -0
  63. package/dist/src/playbook/engine.js +296 -11
  64. package/dist/src/playbook/mcp-recorder.js +204 -0
  65. package/dist/src/playbook/recorder.js +3 -2
  66. package/dist/src/playbook/runner.js +1 -1
  67. package/dist/src/playbook/store.js +139 -10
  68. package/dist/src/recovery/detectors.js +156 -0
  69. package/dist/src/recovery/engine.js +327 -0
  70. package/dist/src/recovery/index.js +20 -0
  71. package/dist/src/recovery/strategies.js +274 -0
  72. package/dist/src/recovery/types.js +20 -0
  73. package/dist/src/runtime/accessibility-adapter.js +55 -18
  74. package/dist/src/runtime/applescript-adapter.js +8 -2
  75. package/dist/src/runtime/cdp-chrome-adapter.js +1 -1
  76. package/dist/src/runtime/executor.js +23 -3
  77. package/dist/src/runtime/locator-cache.js +24 -2
  78. package/dist/src/runtime/service.js +59 -15
  79. package/dist/src/runtime/session-manager.js +4 -1
  80. package/dist/src/runtime/vision-adapter.js +2 -1
  81. package/dist/src/state/app-map-types.js +72 -0
  82. package/dist/src/state/app-map.js +1974 -0
  83. package/dist/src/state/entity-tracker.js +108 -0
  84. package/dist/src/state/fusion.js +96 -0
  85. package/dist/src/state/index.js +21 -0
  86. package/dist/src/state/ladder-generator.js +236 -0
  87. package/dist/src/state/persistence.js +156 -0
  88. package/dist/src/state/types.js +17 -0
  89. package/dist/src/state/world-model.js +1456 -0
  90. package/dist/src/util/atomic-write.js +19 -4
  91. package/dist/src/util/sanitize.js +146 -0
  92. package/dist-app-maps/com.figma.Desktop.json +959 -0
  93. package/dist-app-maps/com.hnc.Discord.json +1146 -0
  94. package/dist-app-maps/notion.id.json +2831 -0
  95. package/dist-playbooks/canva-screenhand-carousel.json +445 -0
  96. package/dist-playbooks/codex-desktop.json +76 -0
  97. package/dist-playbooks/competitor-research-stack.json +122 -0
  98. package/dist-playbooks/davinci-color-grade.json +153 -0
  99. package/dist-playbooks/davinci-edit-timeline.json +162 -0
  100. package/dist-playbooks/davinci-render.json +114 -0
  101. package/dist-playbooks/devto.json +52 -0
  102. package/dist-playbooks/discord.json +41 -0
  103. package/dist-playbooks/google-flow-create-project.json +59 -0
  104. package/dist-playbooks/google-flow-edit-image.json +90 -0
  105. package/dist-playbooks/google-flow-edit-video.json +90 -0
  106. package/dist-playbooks/google-flow-generate-image.json +68 -0
  107. package/dist-playbooks/google-flow-generate-video.json +191 -0
  108. package/dist-playbooks/google-flow-open-project.json +48 -0
  109. package/dist-playbooks/google-flow-open-scenebuilder.json +64 -0
  110. package/dist-playbooks/google-flow-search-assets.json +64 -0
  111. package/dist-playbooks/instagram.json +57 -0
  112. package/dist-playbooks/linkedin.json +52 -0
  113. package/dist-playbooks/n8n.json +43 -0
  114. package/dist-playbooks/reddit.json +52 -0
  115. package/dist-playbooks/threads.json +59 -0
  116. package/dist-playbooks/x-twitter.json +59 -0
  117. package/dist-playbooks/youtube.json +59 -0
  118. package/dist-references/canva.json +646 -0
  119. package/dist-references/codex-desktop.json +305 -0
  120. package/dist-references/davinci-resolve-keyboard.json +594 -0
  121. package/dist-references/davinci-resolve-menu-map.json +1139 -0
  122. package/dist-references/davinci-resolve-menus-batch1.json +116 -0
  123. package/dist-references/davinci-resolve-menus-batch2.json +372 -0
  124. package/dist-references/davinci-resolve-menus-batch3.json +330 -0
  125. package/dist-references/davinci-resolve-menus-batch4.json +297 -0
  126. package/dist-references/davinci-resolve-shortcuts.json +333 -0
  127. package/dist-references/devpost.json +186 -0
  128. package/dist-references/devto.json +317 -0
  129. package/dist-references/discord.json +549 -0
  130. package/dist-references/figma.json +1186 -0
  131. package/dist-references/finder.json +146 -0
  132. package/dist-references/google-ads-transparency.json +95 -0
  133. package/dist-references/google-flow.json +649 -0
  134. package/dist-references/instagram.json +341 -0
  135. package/dist-references/linkedin.json +324 -0
  136. package/dist-references/meta-ad-library.json +86 -0
  137. package/dist-references/n8n.json +387 -0
  138. package/dist-references/notes.json +27 -0
  139. package/dist-references/notion.json +163 -0
  140. package/dist-references/reddit.json +341 -0
  141. package/dist-references/threads.json +337 -0
  142. package/dist-references/x-twitter.json +403 -0
  143. package/dist-references/youtube.json +373 -0
  144. package/native/macos-bridge/Package.swift +22 -0
  145. package/native/macos-bridge/Sources/AccessibilityBridge.swift +482 -0
  146. package/native/macos-bridge/Sources/AppManagement.swift +339 -0
  147. package/native/macos-bridge/Sources/CoreGraphicsBridge.swift +537 -0
  148. package/native/macos-bridge/Sources/ObserverBridge.swift +120 -0
  149. package/native/macos-bridge/Sources/StreamCapture.swift +136 -0
  150. package/native/macos-bridge/Sources/VisionBridge.swift +238 -0
  151. package/native/macos-bridge/Sources/main.swift +498 -0
  152. package/native/windows-bridge/AppManagement.cs +234 -0
  153. package/native/windows-bridge/InputBridge.cs +436 -0
  154. package/native/windows-bridge/Program.cs +270 -0
  155. package/native/windows-bridge/ScreenCapture.cs +453 -0
  156. package/native/windows-bridge/UIAutomationBridge.cs +571 -0
  157. package/native/windows-bridge/WindowsBridge.csproj +17 -0
  158. package/package.json +12 -1
  159. package/scripts/postinstall.cjs +127 -0
  160. package/dist/.audit-log.jsonl +0 -55
  161. package/dist/.screenhand/memory/.lock +0 -1
  162. package/dist/.screenhand/memory/actions.jsonl +0 -85
  163. package/dist/.screenhand/memory/errors.jsonl +0 -5
  164. package/dist/.screenhand/memory/errors.jsonl.bak +0 -4
  165. package/dist/.screenhand/memory/state.json +0 -35
  166. package/dist/.screenhand/memory/state.json.bak +0 -35
  167. package/dist/.screenhand/memory/strategies.jsonl +0 -12
  168. package/dist/agent/cli.js +0 -73
  169. package/dist/agent/loop.js +0 -258
  170. package/dist/config.js +0 -9
  171. package/dist/index.js +0 -56
  172. package/dist/logging/timeline-logger.js +0 -29
  173. package/dist/mcp/mcp-stdio-server.js +0 -448
  174. package/dist/mcp/server.js +0 -347
  175. package/dist/mcp-entry.js +0 -59
  176. package/dist/memory/recall.js +0 -160
  177. package/dist/memory/research.js +0 -98
  178. package/dist/memory/seeds.js +0 -89
  179. package/dist/memory/session.js +0 -161
  180. package/dist/memory/store.js +0 -391
  181. package/dist/memory/types.js +0 -4
  182. package/dist/monitor/codex-monitor.js +0 -377
  183. package/dist/monitor/task-queue.js +0 -84
  184. package/dist/monitor/types.js +0 -49
  185. package/dist/native/bridge-client.js +0 -174
  186. package/dist/native/macos-bridge-client.js +0 -5
  187. package/dist/npm-publish-helper.js +0 -117
  188. package/dist/npm-token-cdp.js +0 -113
  189. package/dist/npm-token-create.js +0 -135
  190. package/dist/npm-token-finish.js +0 -126
  191. package/dist/playbook/engine.js +0 -193
  192. package/dist/playbook/index.js +0 -4
  193. package/dist/playbook/recorder.js +0 -519
  194. package/dist/playbook/runner.js +0 -392
  195. package/dist/playbook/store.js +0 -166
  196. package/dist/playbook/types.js +0 -4
  197. package/dist/runtime/accessibility-adapter.js +0 -377
  198. package/dist/runtime/app-adapter.js +0 -48
  199. package/dist/runtime/applescript-adapter.js +0 -283
  200. package/dist/runtime/ax-role-map.js +0 -80
  201. package/dist/runtime/browser-adapter.js +0 -36
  202. package/dist/runtime/cdp-chrome-adapter.js +0 -505
  203. package/dist/runtime/composite-adapter.js +0 -205
  204. package/dist/runtime/executor.js +0 -250
  205. package/dist/runtime/locator-cache.js +0 -12
  206. package/dist/runtime/planning-loop.js +0 -47
  207. package/dist/runtime/service.js +0 -372
  208. package/dist/runtime/session-manager.js +0 -28
  209. package/dist/runtime/state-observer.js +0 -105
  210. package/dist/runtime/vision-adapter.js +0 -208
  211. package/dist/test-mcp-protocol.js +0 -138
  212. package/dist/types.js +0 -1
@@ -1,392 +0,0 @@
1
- /**
2
- * Playbook Runner — the brain
3
- *
4
- * 1. Match task → playbook
5
- * 2. Execute playbook steps (fast, no AI)
6
- * 3. If step fails → ask AI to recover
7
- * 4. Save AI's recovery steps back into playbook
8
- * 5. Loop forever in monitor mode
9
- */
10
- import Anthropic from "@anthropic-ai/sdk";
11
- import { PlaybookEngine } from "./engine.js";
12
- import { PlaybookStore } from "./store.js";
13
- export class PlaybookRunner {
14
- runtime;
15
- engine;
16
- store;
17
- ai;
18
- model;
19
- maxRecovery;
20
- log;
21
- constructor(runtime, playbookDir, options = {}) {
22
- this.runtime = runtime;
23
- this.engine = new PlaybookEngine(runtime);
24
- this.store = new PlaybookStore(playbookDir);
25
- this.store.load();
26
- this.ai = new Anthropic();
27
- this.model = options.model ?? "claude-sonnet-4-20250514";
28
- this.maxRecovery = options.maxRecoveryAttempts ?? 3;
29
- this.log = options.onLog ?? ((msg) => console.error(`[PlaybookRunner] ${msg}`));
30
- }
31
- /**
32
- * Execute a task. Tries playbook first, falls back to AI.
33
- */
34
- async execute(sessionId, task) {
35
- // 1. Find matching playbook
36
- const playbook = this.store.matchByTask(task);
37
- if (playbook && playbook.steps.length > 0) {
38
- this.log(`Found playbook: ${playbook.name} (${playbook.successCount} successes)`);
39
- // 2. Run playbook
40
- const result = await this.engine.run(sessionId, playbook, {
41
- onStep: (i, step, res) => {
42
- this.log(` Step ${i + 1}/${playbook.steps.length}: ${step.description ?? step.action} → ${res}`);
43
- },
44
- });
45
- if (result.success) {
46
- this.store.recordOutcome(playbook.id, true);
47
- this.log(`Playbook completed successfully in ${result.durationMs}ms`);
48
- return result;
49
- }
50
- // 3. Playbook failed at a step — try AI recovery
51
- this.log(`Playbook failed at step ${result.failedAtStep}: ${result.error}`);
52
- const recovery = await this.aiRecover(sessionId, playbook, result);
53
- if (recovery) {
54
- this.store.recordOutcome(playbook.id, true);
55
- return { ...result, success: true, aiRecovery: recovery };
56
- }
57
- this.store.recordOutcome(playbook.id, false);
58
- return result;
59
- }
60
- // Playbook found but has no executable steps (legacy format with flows/selectors)
61
- // → Use AI mode but feed it the playbook's rich metadata as context
62
- if (playbook) {
63
- this.log(`Found reference playbook: ${playbook.name} (flows/selectors, no executable steps)`);
64
- return this.aiExecute(sessionId, task, playbook);
65
- }
66
- // No playbook found — pure AI mode
67
- this.log(`No playbook found for: "${task}". Using AI.`);
68
- return this.aiExecute(sessionId, task);
69
- }
70
- /**
71
- * AI recovery — when a playbook step fails, ask AI to fix it.
72
- */
73
- async aiRecover(sessionId, playbook, failResult) {
74
- const failedStep = playbook.steps[failResult.failedAtStep];
75
- if (!failedStep)
76
- return null;
77
- // Take screenshot for context
78
- let screenshotInfo = "";
79
- try {
80
- const shot = await this.runtime.screenshot({ sessionId });
81
- if (shot.ok)
82
- screenshotInfo = `Screenshot saved to: ${shot.data.path}`;
83
- }
84
- catch { /* ignore */ }
85
- // Get current page state
86
- let pageState = "";
87
- try {
88
- const tree = await this.runtime.elementTree({ sessionId, maxDepth: 4 });
89
- if (tree.ok) {
90
- pageState = JSON.stringify(tree.data).slice(0, 3000);
91
- }
92
- }
93
- catch { /* ignore */ }
94
- // Build rich context from playbook metadata
95
- const playbookContext = buildPlaybookContext(playbook);
96
- const prompt = `A playbook automation failed. Help me recover.
97
-
98
- Playbook: ${playbook.name}
99
- Platform: ${playbook.platform}
100
- Failed at step ${failResult.failedAtStep + 1}/${playbook.steps.length}:
101
- Action: ${failedStep.action}
102
- Target: ${JSON.stringify(failedStep.target)}
103
- Error: ${failResult.error}
104
-
105
- Steps completed before failure:
106
- ${playbook.steps.slice(0, failResult.failedAtStep).map((s, i) => ` ${i + 1}. ${s.description ?? s.action}`).join("\n")}
107
-
108
- Remaining steps after failure:
109
- ${playbook.steps.slice(failResult.failedAtStep).map((s, i) => ` ${failResult.failedAtStep + i + 1}. ${s.description ?? s.action}`).join("\n")}
110
-
111
- ${playbookContext}
112
-
113
- Current UI state (accessibility tree):
114
- ${pageState}
115
-
116
- ${screenshotInfo}
117
-
118
- What should I do to recover? Respond with a JSON array of recovery steps:
119
- [
120
- { "action": "press", "target": "...", "description": "..." },
121
- { "action": "wait", "ms": 1000 }
122
- ]
123
-
124
- Or if unrecoverable, respond with: { "unrecoverable": true, "reason": "..." }`;
125
- for (let attempt = 0; attempt < this.maxRecovery; attempt++) {
126
- try {
127
- const resp = await this.ai.messages.create({
128
- model: this.model,
129
- max_tokens: 1024,
130
- messages: [{ role: "user", content: prompt }],
131
- });
132
- const text = resp.content[0]?.type === "text" ? resp.content[0].text : "";
133
- const jsonMatch = text.match(/\[[\s\S]*\]|\{[\s\S]*\}/);
134
- if (!jsonMatch)
135
- continue;
136
- const parsed = JSON.parse(jsonMatch[0]);
137
- // Check if unrecoverable
138
- if (parsed.unrecoverable) {
139
- this.log(`AI says unrecoverable: ${parsed.reason}`);
140
- return null;
141
- }
142
- // Execute recovery steps
143
- const recoverySteps = Array.isArray(parsed) ? parsed : [parsed];
144
- this.log(`AI suggests ${recoverySteps.length} recovery steps`);
145
- for (const step of recoverySteps) {
146
- const stepResult = await this.engine.run(sessionId, {
147
- id: "recovery",
148
- name: "AI Recovery",
149
- description: "",
150
- platform: playbook.platform,
151
- steps: [step],
152
- version: "0",
153
- tags: [],
154
- successCount: 0,
155
- failCount: 0,
156
- });
157
- if (!stepResult.success) {
158
- this.log(`Recovery step failed: ${stepResult.error}`);
159
- continue;
160
- }
161
- }
162
- // Now try remaining playbook steps
163
- const remaining = {
164
- ...playbook,
165
- id: `${playbook.id}_remaining`,
166
- steps: playbook.steps.slice(failResult.failedAtStep + 1),
167
- };
168
- if (remaining.steps.length > 0) {
169
- const remainingResult = await this.engine.run(sessionId, remaining, {
170
- onStep: (i, step, res) => {
171
- const globalIdx = failResult.failedAtStep + 1 + i;
172
- this.log(` Step ${globalIdx + 1}/${playbook.steps.length}: ${step.description ?? step.action} → ${res}`);
173
- },
174
- });
175
- if (!remainingResult.success) {
176
- this.log(`Remaining steps failed at ${remainingResult.failedAtStep}`);
177
- return null;
178
- }
179
- }
180
- // Save recovery steps back into playbook for next time
181
- this.patchPlaybook(playbook, failResult.failedAtStep, recoverySteps);
182
- return `AI recovered with ${recoverySteps.length} steps, then completed remaining ${remaining.steps.length} steps`;
183
- }
184
- catch (err) {
185
- this.log(`AI recovery attempt ${attempt + 1} failed: ${err instanceof Error ? err.message : String(err)}`);
186
- }
187
- }
188
- return null;
189
- }
190
- /**
191
- * AI execution — optionally guided by a reference playbook's metadata.
192
- * When a playbook has selectors/flows/errors but no executable steps,
193
- * AI uses that knowledge to make smarter decisions.
194
- * After success, saves the steps as a new playbook.
195
- */
196
- async aiExecute(sessionId, task, refPlaybook) {
197
- const start = Date.now();
198
- const executedSteps = [];
199
- const playbookContext = refPlaybook ? buildPlaybookContext(refPlaybook) : "";
200
- // Simple AI loop — observe, decide, act
201
- for (let i = 0; i < 20; i++) {
202
- let pageState = "";
203
- try {
204
- const tree = await this.runtime.elementTree({ sessionId, maxDepth: 4 });
205
- if (tree.ok)
206
- pageState = JSON.stringify(tree.data).slice(0, 4000);
207
- }
208
- catch { /* ignore */ }
209
- const prompt = `Task: ${task}
210
-
211
- Steps taken so far:
212
- ${executedSteps.map((s, idx) => `${idx + 1}. ${s.description ?? s.action}`).join("\n") || "(none)"}
213
-
214
- Current UI state:
215
- ${pageState}
216
- ${playbookContext ? `\n--- PLAYBOOK REFERENCE ---\n${playbookContext}\nUse the selectors, flows, and error solutions above to guide your actions. Prefer data-testid selectors over text matching.\n---\n` : ""}
217
- What's the next step? Respond with ONE step as JSON:
218
- { "action": "press|type_into|navigate|key_combo|scroll|wait", "target": "...", "text": "...", "url": "...", "keys": [...], "ms": 1000, "description": "..." }
219
-
220
- Or if done: { "action": "done", "description": "Task complete" }`;
221
- try {
222
- const resp = await this.ai.messages.create({
223
- model: this.model,
224
- max_tokens: 512,
225
- messages: [{ role: "user", content: prompt }],
226
- });
227
- const text = resp.content[0]?.type === "text" ? resp.content[0].text : "";
228
- const jsonMatch = text.match(/\{[\s\S]*\}/);
229
- if (!jsonMatch)
230
- continue;
231
- const step = JSON.parse(jsonMatch[0]);
232
- if (step.action === "done") {
233
- // Save as new playbook for next time
234
- if (executedSteps.length > 0) {
235
- this.saveNewPlaybook(task, executedSteps);
236
- }
237
- return {
238
- playbook: "ai_generated",
239
- success: true,
240
- stepsCompleted: executedSteps.length,
241
- totalSteps: executedSteps.length,
242
- failedAtStep: -1,
243
- durationMs: Date.now() - start,
244
- };
245
- }
246
- // Execute the step
247
- const stepResult = await this.engine.run(sessionId, {
248
- id: "ai_step",
249
- name: "AI Step",
250
- description: "",
251
- platform: "unknown",
252
- steps: [step],
253
- version: "0",
254
- tags: [],
255
- successCount: 0,
256
- failCount: 0,
257
- });
258
- if (stepResult.success) {
259
- executedSteps.push(step);
260
- this.log(`AI step ${executedSteps.length}: ${step.description ?? step.action}`);
261
- }
262
- else {
263
- this.log(`AI step failed: ${stepResult.error}`);
264
- }
265
- await sleep(300);
266
- }
267
- catch (err) {
268
- this.log(`AI step error: ${err instanceof Error ? err.message : String(err)}`);
269
- }
270
- }
271
- return {
272
- playbook: "ai_generated",
273
- success: false,
274
- stepsCompleted: executedSteps.length,
275
- totalSteps: -1,
276
- failedAtStep: executedSteps.length,
277
- error: "Max AI steps reached",
278
- durationMs: Date.now() - start,
279
- };
280
- }
281
- /**
282
- * Patch a playbook — insert recovery steps at the failure point.
283
- */
284
- patchPlaybook(playbook, failedAt, recoverySteps) {
285
- const patched = {
286
- ...playbook,
287
- steps: [
288
- ...playbook.steps.slice(0, failedAt),
289
- ...recoverySteps,
290
- ...playbook.steps.slice(failedAt),
291
- ],
292
- version: bumpVersion(playbook.version),
293
- };
294
- this.store.save(patched);
295
- this.log(`Patched playbook ${playbook.id}: inserted ${recoverySteps.length} recovery steps at position ${failedAt}`);
296
- }
297
- /**
298
- * Save AI-generated steps as a new playbook.
299
- */
300
- saveNewPlaybook(task, steps) {
301
- const id = `auto_${Date.now()}`;
302
- const playbook = {
303
- id,
304
- name: task.slice(0, 80),
305
- description: `Auto-generated from AI execution: ${task}`,
306
- platform: "unknown",
307
- steps,
308
- version: "1.0.0",
309
- tags: task.toLowerCase().split(/\W+/).filter((w) => w.length >= 3),
310
- successCount: 1,
311
- failCount: 0,
312
- lastRun: new Date().toISOString(),
313
- };
314
- this.store.save(playbook);
315
- this.log(`Saved new playbook: ${id} (${steps.length} steps)`);
316
- }
317
- /** Get all loaded playbooks. */
318
- listPlaybooks() {
319
- return this.store.getAll();
320
- }
321
- /** Reload playbooks from disk. */
322
- reload() {
323
- this.store.load();
324
- }
325
- }
326
- function sleep(ms) {
327
- return new Promise((resolve) => setTimeout(resolve, ms));
328
- }
329
- function bumpVersion(version) {
330
- const parts = version.split(".").map(Number);
331
- if (parts.length === 3) {
332
- parts[2]++;
333
- return parts.join(".");
334
- }
335
- return version + ".1";
336
- }
337
- /**
338
- * Build a context string from playbook metadata for AI consumption.
339
- * Includes selectors, flows, known errors, detection expressions.
340
- */
341
- function buildPlaybookContext(playbook) {
342
- const parts = [];
343
- if (playbook.urls && Object.keys(playbook.urls).length > 0) {
344
- parts.push("URLS:\n" + Object.entries(playbook.urls).map(([k, v]) => ` ${k}: ${v}`).join("\n"));
345
- }
346
- if (playbook.selectors && Object.keys(playbook.selectors).length > 0) {
347
- parts.push("SELECTORS:");
348
- for (const [group, sels] of Object.entries(playbook.selectors)) {
349
- parts.push(` [${group}]`);
350
- for (const [name, sel] of Object.entries(sels)) {
351
- parts.push(` ${name}: ${sel}`);
352
- }
353
- }
354
- }
355
- if (playbook.flows && Object.keys(playbook.flows).length > 0) {
356
- parts.push("FLOWS:");
357
- for (const [name, flow] of Object.entries(playbook.flows)) {
358
- parts.push(` [${name}]`);
359
- for (const step of flow.steps) {
360
- parts.push(` - ${step}`);
361
- }
362
- if (flow.guards) {
363
- parts.push(` Guards:`);
364
- for (const g of flow.guards)
365
- parts.push(` ! ${g}`);
366
- }
367
- }
368
- }
369
- if (playbook.detection && Object.keys(playbook.detection).length > 0) {
370
- parts.push("DETECTION (JS expressions):");
371
- for (const [name, expr] of Object.entries(playbook.detection)) {
372
- parts.push(` ${name}: ${expr}`);
373
- }
374
- }
375
- if (playbook.errors && playbook.errors.length > 0) {
376
- parts.push("KNOWN ERRORS & SOLUTIONS:");
377
- for (const e of playbook.errors) {
378
- parts.push(` [${e.severity}] ${e.error}`);
379
- parts.push(` Context: ${e.context}`);
380
- parts.push(` Solution: ${e.solution}`);
381
- }
382
- }
383
- if (playbook.policyNotes && Object.keys(playbook.policyNotes).length > 0) {
384
- parts.push("POLICY NOTES:");
385
- for (const [cat, notes] of Object.entries(playbook.policyNotes)) {
386
- parts.push(` [${cat}]`);
387
- for (const n of notes)
388
- parts.push(` - ${n}`);
389
- }
390
- }
391
- return parts.join("\n");
392
- }
@@ -1,166 +0,0 @@
1
- /**
2
- * Playbook Store — load, save, match playbooks from disk
3
- *
4
- * Playbooks are stored as JSON files in the playbooks/ directory.
5
- * Each file can contain a Playbook directly, or a legacy format
6
- * that gets converted.
7
- */
8
- import fs from "node:fs";
9
- import path from "node:path";
10
- export class PlaybookStore {
11
- dir;
12
- playbooks = new Map();
13
- constructor(dir) {
14
- this.dir = dir;
15
- }
16
- /** Load all playbooks from disk into memory. */
17
- load() {
18
- this.playbooks.clear();
19
- if (!fs.existsSync(this.dir))
20
- return;
21
- const files = fs.readdirSync(this.dir).filter((f) => f.endsWith(".json"));
22
- for (const file of files) {
23
- try {
24
- const raw = JSON.parse(fs.readFileSync(path.join(this.dir, file), "utf-8"));
25
- const playbook = this.normalize(raw, file);
26
- if (playbook) {
27
- this.playbooks.set(playbook.id, playbook);
28
- }
29
- }
30
- catch {
31
- // Skip unparseable files
32
- }
33
- }
34
- }
35
- /** Get all loaded playbooks. */
36
- getAll() {
37
- return [...this.playbooks.values()];
38
- }
39
- /** Get a playbook by ID. */
40
- get(id) {
41
- return this.playbooks.get(id);
42
- }
43
- /** Find playbooks matching a URL. */
44
- matchByUrl(url) {
45
- return this.getAll().filter((p) => {
46
- if (!p.urlPatterns || p.urlPatterns.length === 0)
47
- return false;
48
- return p.urlPatterns.some((pattern) => {
49
- try {
50
- return new RegExp(pattern).test(url);
51
- }
52
- catch {
53
- return url.includes(pattern);
54
- }
55
- });
56
- });
57
- }
58
- /** Find playbooks matching tags. */
59
- matchByTags(tags) {
60
- const tagSet = new Set(tags.map((t) => t.toLowerCase()));
61
- return this.getAll()
62
- .filter((p) => p.tags.some((t) => tagSet.has(t.toLowerCase())))
63
- .sort((a, b) => b.successCount - a.successCount);
64
- }
65
- /** Find playbooks by platform. */
66
- matchByPlatform(platform) {
67
- return this.getAll()
68
- .filter((p) => p.platform.toLowerCase() === platform.toLowerCase())
69
- .sort((a, b) => b.successCount - a.successCount);
70
- }
71
- /** Find best playbook for a task description (simple keyword matching). */
72
- matchByTask(task) {
73
- const tokens = task.toLowerCase().split(/\W+/).filter((w) => w.length >= 3);
74
- if (tokens.length === 0)
75
- return null;
76
- let best = null;
77
- let bestScore = 0;
78
- for (const p of this.playbooks.values()) {
79
- const haystack = `${p.name} ${p.description} ${p.tags.join(" ")} ${p.platform}`.toLowerCase();
80
- let score = 0;
81
- for (const token of tokens) {
82
- if (haystack.includes(token))
83
- score++;
84
- }
85
- // Weight by reliability
86
- const reliability = p.successCount + p.failCount > 0
87
- ? p.successCount / (p.successCount + p.failCount)
88
- : 0.5;
89
- score *= reliability;
90
- if (score > bestScore) {
91
- bestScore = score;
92
- best = p;
93
- }
94
- }
95
- return bestScore > 0 ? best : null;
96
- }
97
- /** Save a playbook to disk. */
98
- save(playbook) {
99
- if (!fs.existsSync(this.dir)) {
100
- fs.mkdirSync(this.dir, { recursive: true });
101
- }
102
- const filename = `${playbook.id}.json`;
103
- fs.writeFileSync(path.join(this.dir, filename), JSON.stringify(playbook, null, 2) + "\n");
104
- this.playbooks.set(playbook.id, playbook);
105
- }
106
- /** Record a run outcome. */
107
- recordOutcome(id, success) {
108
- const playbook = this.playbooks.get(id);
109
- if (!playbook)
110
- return;
111
- if (success) {
112
- playbook.successCount++;
113
- }
114
- else {
115
- playbook.failCount++;
116
- }
117
- playbook.lastRun = new Date().toISOString();
118
- this.save(playbook);
119
- }
120
- /**
121
- * Normalize raw JSON into a Playbook.
122
- * Handles both new format (has steps array) and legacy format (has flows object).
123
- */
124
- normalize(raw, filename) {
125
- // Already in new format
126
- if (Array.isArray(raw.steps)) {
127
- return raw;
128
- }
129
- // Legacy format: has flows with steps arrays (like instagram_v2.json)
130
- if (raw.flows && typeof raw.flows === "object") {
131
- return this.convertLegacy(raw, filename);
132
- }
133
- return null;
134
- }
135
- /**
136
- * Convert legacy playbook format to new format.
137
- * Preserves all rich metadata: selectors, flows, errors, detection, policy notes.
138
- */
139
- convertLegacy(raw, filename) {
140
- const platform = raw.platform ?? filename.replace(".json", "");
141
- const id = raw.playbook ?? filename.replace(".json", "");
142
- const description = raw.description ?? "";
143
- return {
144
- id,
145
- name: `${platform} playbook`,
146
- description,
147
- platform,
148
- urlPatterns: raw.urls ? Object.values(raw.urls).map(escapeRegex) : [],
149
- steps: [], // Legacy playbooks use flows instead of direct steps
150
- tags: [platform, ...(raw.playbook ? [raw.playbook] : [])],
151
- version: raw.version ?? "1.0.0",
152
- successCount: 0,
153
- failCount: 0,
154
- // Preserve rich metadata
155
- ...(raw.urls ? { urls: raw.urls } : {}),
156
- ...(raw.selectors ? { selectors: raw.selectors } : {}),
157
- ...(raw.flows ? { flows: raw.flows } : {}),
158
- ...(raw.detection ? { detection: raw.detection } : {}),
159
- ...(raw.errors ? { errors: raw.errors } : {}),
160
- ...(raw.policy_notes ? { policyNotes: raw.policy_notes } : {}),
161
- };
162
- }
163
- }
164
- function escapeRegex(s) {
165
- return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
166
- }
@@ -1,4 +0,0 @@
1
- /**
2
- * Playbook types — executable automation recipes
3
- */
4
- export {};