@clagentic/console 1.0.0-beta.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 (221) hide show
  1. package/CREDITS.md +3 -0
  2. package/LICENSE +21 -0
  3. package/README.md +250 -0
  4. package/bin/cli.js +2812 -0
  5. package/lib/agents-favorites.js +164 -0
  6. package/lib/agents.js +282 -0
  7. package/lib/ask-user-mcp-server.js +120 -0
  8. package/lib/browser-mcp-server.js +484 -0
  9. package/lib/build-user-env.js +63 -0
  10. package/lib/certs/fullchain.pem +47 -0
  11. package/lib/certs/privkey.pem +5 -0
  12. package/lib/cli-sessions.js +265 -0
  13. package/lib/codex-defaults.js +18 -0
  14. package/lib/config.js +332 -0
  15. package/lib/crisis-safety.js +60 -0
  16. package/lib/daemon-projects.js +164 -0
  17. package/lib/daemon.js +1602 -0
  18. package/lib/dm.js +128 -0
  19. package/lib/ipc.js +125 -0
  20. package/lib/ipv4-only.js +39 -0
  21. package/lib/mcp-local.js +385 -0
  22. package/lib/notes.js +141 -0
  23. package/lib/os-users.js +529 -0
  24. package/lib/pages.js +1325 -0
  25. package/lib/project-connection.js +302 -0
  26. package/lib/project-external-trigger.js +236 -0
  27. package/lib/project-file-watch.js +120 -0
  28. package/lib/project-filesystem.js +528 -0
  29. package/lib/project-http.js +753 -0
  30. package/lib/project-image.js +95 -0
  31. package/lib/project-knowledge.js +88 -0
  32. package/lib/project-loop.js +1304 -0
  33. package/lib/project-mcp.js +374 -0
  34. package/lib/project-memory.js +39 -0
  35. package/lib/project-notifications.js +262 -0
  36. package/lib/project-sessions.js +1581 -0
  37. package/lib/project-user-mention.js +193 -0
  38. package/lib/project-user-message.js +628 -0
  39. package/lib/project.js +1303 -0
  40. package/lib/public/app.js +1031 -0
  41. package/lib/public/apple-touch-icon.png +0 -0
  42. package/lib/public/claude-code-avatar.png +0 -0
  43. package/lib/public/codex-avatar.png +0 -0
  44. package/lib/public/console-icon.png +0 -0
  45. package/lib/public/css/admin.css +736 -0
  46. package/lib/public/css/agent-picker.css +119 -0
  47. package/lib/public/css/base.css +235 -0
  48. package/lib/public/css/channel-layout.css +441 -0
  49. package/lib/public/css/command-palette.css +559 -0
  50. package/lib/public/css/debate.css +1609 -0
  51. package/lib/public/css/diff.css +139 -0
  52. package/lib/public/css/filebrowser.css +1976 -0
  53. package/lib/public/css/highlight.css +144 -0
  54. package/lib/public/css/home-hub.css +545 -0
  55. package/lib/public/css/icon-strip.css +1856 -0
  56. package/lib/public/css/input.css +1349 -0
  57. package/lib/public/css/loop.css +1248 -0
  58. package/lib/public/css/mention.css +317 -0
  59. package/lib/public/css/menus.css +857 -0
  60. package/lib/public/css/messages.css +1833 -0
  61. package/lib/public/css/mobile-nav.css +1332 -0
  62. package/lib/public/css/notifications-center.css +296 -0
  63. package/lib/public/css/overlays.css +1273 -0
  64. package/lib/public/css/playbook.css +264 -0
  65. package/lib/public/css/profile.css +397 -0
  66. package/lib/public/css/rewind.css +671 -0
  67. package/lib/public/css/scheduler-modal.css +1495 -0
  68. package/lib/public/css/scheduler.css +1420 -0
  69. package/lib/public/css/server-settings.css +909 -0
  70. package/lib/public/css/session-search.css +159 -0
  71. package/lib/public/css/sidebar.css +2012 -0
  72. package/lib/public/css/skills.css +789 -0
  73. package/lib/public/css/sticky-notes.css +854 -0
  74. package/lib/public/css/stt.css +155 -0
  75. package/lib/public/css/title-bar.css +674 -0
  76. package/lib/public/css/tooltip.css +67 -0
  77. package/lib/public/css/user-settings.css +702 -0
  78. package/lib/public/emoji-data.json +1 -0
  79. package/lib/public/favicon-banded-32.png +0 -0
  80. package/lib/public/favicon-banded.png +0 -0
  81. package/lib/public/favicon-dark.svg +32 -0
  82. package/lib/public/favicon.svg +32 -0
  83. package/lib/public/icon-192.png +0 -0
  84. package/lib/public/icon-512.png +0 -0
  85. package/lib/public/icon-banded-76.png +0 -0
  86. package/lib/public/icon-banded-96.png +0 -0
  87. package/lib/public/icon-mono.svg +25 -0
  88. package/lib/public/icons/email/gmail.svg +7 -0
  89. package/lib/public/icons/email/outlook.svg +35 -0
  90. package/lib/public/icons/email/yahoo.svg +1 -0
  91. package/lib/public/index.html +1946 -0
  92. package/lib/public/manifest.json +27 -0
  93. package/lib/public/markdown-worker.js +43 -0
  94. package/lib/public/marked.min.js +6 -0
  95. package/lib/public/modules/admin.js +954 -0
  96. package/lib/public/modules/agent-picker.js +429 -0
  97. package/lib/public/modules/app-connection.js +216 -0
  98. package/lib/public/modules/app-cursors.js +492 -0
  99. package/lib/public/modules/app-dm.js +265 -0
  100. package/lib/public/modules/app-favicon.js +231 -0
  101. package/lib/public/modules/app-header.js +242 -0
  102. package/lib/public/modules/app-home-hub.js +432 -0
  103. package/lib/public/modules/app-loop-ui.js +691 -0
  104. package/lib/public/modules/app-loop-wizard.js +439 -0
  105. package/lib/public/modules/app-messages.js +1415 -0
  106. package/lib/public/modules/app-misc.js +463 -0
  107. package/lib/public/modules/app-notifications.js +651 -0
  108. package/lib/public/modules/app-panels.js +1061 -0
  109. package/lib/public/modules/app-projects.js +875 -0
  110. package/lib/public/modules/app-rate-limit.js +529 -0
  111. package/lib/public/modules/app-rendering.js +739 -0
  112. package/lib/public/modules/app-skills-install.js +285 -0
  113. package/lib/public/modules/avatar.js +36 -0
  114. package/lib/public/modules/command-palette.js +491 -0
  115. package/lib/public/modules/console-logo.js +162 -0
  116. package/lib/public/modules/context-sources.js +365 -0
  117. package/lib/public/modules/diff.js +412 -0
  118. package/lib/public/modules/dom-refs.js +21 -0
  119. package/lib/public/modules/emoji-picker.js +222 -0
  120. package/lib/public/modules/events.js +21 -0
  121. package/lib/public/modules/filebrowser.js +1966 -0
  122. package/lib/public/modules/fileicons.js +172 -0
  123. package/lib/public/modules/icons.js +80 -0
  124. package/lib/public/modules/input.js +1102 -0
  125. package/lib/public/modules/longpress.js +74 -0
  126. package/lib/public/modules/markdown.js +486 -0
  127. package/lib/public/modules/mcp-ui.js +295 -0
  128. package/lib/public/modules/mention.js +1093 -0
  129. package/lib/public/modules/notifications.js +613 -0
  130. package/lib/public/modules/playbook.js +578 -0
  131. package/lib/public/modules/profile.js +594 -0
  132. package/lib/public/modules/project-settings.js +849 -0
  133. package/lib/public/modules/project-switcher.js +375 -0
  134. package/lib/public/modules/qrcode.js +87 -0
  135. package/lib/public/modules/rewind.js +381 -0
  136. package/lib/public/modules/scheduler-config.js +1611 -0
  137. package/lib/public/modules/scheduler-history.js +131 -0
  138. package/lib/public/modules/scheduler.js +1689 -0
  139. package/lib/public/modules/server-settings.js +842 -0
  140. package/lib/public/modules/session-search.js +456 -0
  141. package/lib/public/modules/settings-defaults.js +253 -0
  142. package/lib/public/modules/sidebar-mobile.js +1425 -0
  143. package/lib/public/modules/sidebar-projects.js +2022 -0
  144. package/lib/public/modules/sidebar-sessions.js +1548 -0
  145. package/lib/public/modules/sidebar-users.js +449 -0
  146. package/lib/public/modules/sidebar.js +348 -0
  147. package/lib/public/modules/skills.js +793 -0
  148. package/lib/public/modules/state.js +3 -0
  149. package/lib/public/modules/sticky-notes.js +1221 -0
  150. package/lib/public/modules/store.js +34 -0
  151. package/lib/public/modules/stt.js +266 -0
  152. package/lib/public/modules/terminal.js +987 -0
  153. package/lib/public/modules/theme.js +812 -0
  154. package/lib/public/modules/tool-palette.js +562 -0
  155. package/lib/public/modules/tools.js +2285 -0
  156. package/lib/public/modules/tooltip.js +150 -0
  157. package/lib/public/modules/user-settings.js +544 -0
  158. package/lib/public/modules/utils.js +56 -0
  159. package/lib/public/modules/ws-ref.js +26 -0
  160. package/lib/public/style.css +34 -0
  161. package/lib/public/sw.js +159 -0
  162. package/lib/push.js +149 -0
  163. package/lib/scheduler.js +471 -0
  164. package/lib/sdk-bridge.js +1792 -0
  165. package/lib/sdk-message-processor.js +740 -0
  166. package/lib/sdk-message-queue.js +42 -0
  167. package/lib/sdk-skill-discovery.js +131 -0
  168. package/lib/sdk-worker.js +677 -0
  169. package/lib/server-admin.js +712 -0
  170. package/lib/server-auth.js +737 -0
  171. package/lib/server-dm.js +170 -0
  172. package/lib/server-palette.js +104 -0
  173. package/lib/server-settings.js +553 -0
  174. package/lib/server-skills.js +280 -0
  175. package/lib/server.js +1390 -0
  176. package/lib/session-search.js +693 -0
  177. package/lib/sessions.js +946 -0
  178. package/lib/smtp.js +221 -0
  179. package/lib/terminal-manager.js +219 -0
  180. package/lib/terminal.js +39 -0
  181. package/lib/themes/ayu-light.json +9 -0
  182. package/lib/themes/catppuccin-latte.json +9 -0
  183. package/lib/themes/catppuccin-mocha.json +9 -0
  184. package/lib/themes/clagentic-dark.json +10 -0
  185. package/lib/themes/clagentic-light.json +10 -0
  186. package/lib/themes/dracula.json +9 -0
  187. package/lib/themes/everforest-light.json +9 -0
  188. package/lib/themes/everforest.json +9 -0
  189. package/lib/themes/github-light.json +9 -0
  190. package/lib/themes/gruvbox-dark.json +9 -0
  191. package/lib/themes/gruvbox-light.json +9 -0
  192. package/lib/themes/monokai.json +9 -0
  193. package/lib/themes/nord-light.json +9 -0
  194. package/lib/themes/nord.json +9 -0
  195. package/lib/themes/one-dark.json +9 -0
  196. package/lib/themes/one-light.json +9 -0
  197. package/lib/themes/rose-pine-dawn.json +9 -0
  198. package/lib/themes/rose-pine.json +9 -0
  199. package/lib/themes/solarized-dark.json +9 -0
  200. package/lib/themes/solarized-light.json +9 -0
  201. package/lib/themes/tokyo-night-light.json +9 -0
  202. package/lib/themes/tokyo-night.json +9 -0
  203. package/lib/updater.js +124 -0
  204. package/lib/user-presence.js +92 -0
  205. package/lib/users-auth.js +146 -0
  206. package/lib/users-permissions.js +118 -0
  207. package/lib/users-preferences.js +254 -0
  208. package/lib/users.js +465 -0
  209. package/lib/utils.js +64 -0
  210. package/lib/worktree.js +134 -0
  211. package/lib/ws-schema.js +464 -0
  212. package/lib/yoke/adapters/claude-worker.js +759 -0
  213. package/lib/yoke/adapters/claude.js +1599 -0
  214. package/lib/yoke/adapters/codex.js +1559 -0
  215. package/lib/yoke/codex-app-server.js +352 -0
  216. package/lib/yoke/index.js +319 -0
  217. package/lib/yoke/instructions.js +61 -0
  218. package/lib/yoke/interface.js +98 -0
  219. package/lib/yoke/mcp-bridge-server.js +302 -0
  220. package/lib/yoke/package.json +7 -0
  221. package/package.json +72 -0
@@ -0,0 +1,471 @@
1
+ /**
2
+ * Loop Registry — unified storage for all Ralph Loops (one-off + scheduled).
3
+ *
4
+ * Stores loop records in ~/.clay/loops/{encodedCwd}.jsonl
5
+ * Each record represents a job defined via clay-ralph (PROMPT.md + JUDGE.md).
6
+ * Records with a `cron` field are checked every 30s and auto-triggered.
7
+ * Records without cron are one-off (standard Ralph Loop behavior).
8
+ */
9
+
10
+ var fs = require("fs");
11
+ var path = require("path");
12
+ var { CONFIG_DIR } = require("./config");
13
+ var { encodeCwd } = require("./utils");
14
+
15
+ // --- Cron parser (5-field: minute hour day-of-month month day-of-week) ---
16
+
17
+ function parseCronField(field, min, max) {
18
+ var values = [];
19
+ var parts = field.split(",");
20
+ for (var i = 0; i < parts.length; i++) {
21
+ var part = parts[i].trim();
22
+
23
+ // wildcard with step: */N
24
+ if (part.indexOf("/") !== -1) {
25
+ var slashParts = part.split("/");
26
+ var step = parseInt(slashParts[1], 10);
27
+ var rangeStr = slashParts[0];
28
+ var rangeMin = min;
29
+ var rangeMax = max;
30
+ if (rangeStr !== "*") {
31
+ var rp = rangeStr.split("-");
32
+ rangeMin = parseInt(rp[0], 10);
33
+ rangeMax = rp.length > 1 ? parseInt(rp[1], 10) : rangeMin;
34
+ }
35
+ for (var v = rangeMin; v <= rangeMax; v += step) {
36
+ values.push(v);
37
+ }
38
+ continue;
39
+ }
40
+
41
+ // wildcard
42
+ if (part === "*") {
43
+ for (var v = min; v <= max; v++) {
44
+ values.push(v);
45
+ }
46
+ continue;
47
+ }
48
+
49
+ // range: N-M
50
+ if (part.indexOf("-") !== -1) {
51
+ var rangeParts = part.split("-");
52
+ var from = parseInt(rangeParts[0], 10);
53
+ var to = parseInt(rangeParts[1], 10);
54
+ for (var v = from; v <= to; v++) {
55
+ values.push(v);
56
+ }
57
+ continue;
58
+ }
59
+
60
+ // single value
61
+ values.push(parseInt(part, 10));
62
+ }
63
+ return values;
64
+ }
65
+
66
+ function parseCron(expr) {
67
+ var fields = expr.trim().split(/\s+/);
68
+ if (fields.length !== 5) return null;
69
+ return {
70
+ minutes: parseCronField(fields[0], 0, 59),
71
+ hours: parseCronField(fields[1], 0, 23),
72
+ daysOfMonth: parseCronField(fields[2], 1, 31),
73
+ months: parseCronField(fields[3], 1, 12),
74
+ daysOfWeek: parseCronField(fields[4], 0, 6),
75
+ };
76
+ }
77
+
78
+ function cronMatches(parsed, date) {
79
+ var minute = date.getMinutes();
80
+ var hour = date.getHours();
81
+ var dayOfMonth = date.getDate();
82
+ var month = date.getMonth() + 1;
83
+ var dayOfWeek = date.getDay();
84
+
85
+ return (
86
+ parsed.minutes.indexOf(minute) !== -1 &&
87
+ parsed.hours.indexOf(hour) !== -1 &&
88
+ parsed.daysOfMonth.indexOf(dayOfMonth) !== -1 &&
89
+ parsed.months.indexOf(month) !== -1 &&
90
+ parsed.daysOfWeek.indexOf(dayOfWeek) !== -1
91
+ );
92
+ }
93
+
94
+ /**
95
+ * Calculate next run time from a cron expression after a given date.
96
+ * Brute-force: check each minute for up to 366 days.
97
+ */
98
+ function nextRunTime(cronExpr, after) {
99
+ var parsed = parseCron(cronExpr);
100
+ if (!parsed) return null;
101
+
102
+ var d = new Date(after || Date.now());
103
+ d.setSeconds(0, 0);
104
+ d.setMinutes(d.getMinutes() + 1);
105
+
106
+ var limit = 366 * 24 * 60;
107
+ for (var i = 0; i < limit; i++) {
108
+ if (cronMatches(parsed, d)) {
109
+ return d.getTime();
110
+ }
111
+ d.setMinutes(d.getMinutes() + 1);
112
+ }
113
+ return null;
114
+ }
115
+
116
+ // --- Loop Registry factory ---
117
+
118
+ function createLoopRegistry(opts) {
119
+ var cwd = opts.cwd;
120
+ var onTrigger = opts.onTrigger;
121
+ var onChange = opts.onChange;
122
+
123
+ var encoded = encodeCwd(cwd);
124
+ var registryDir = path.join(CONFIG_DIR, "loops");
125
+ var registryPath = path.join(registryDir, encoded + ".jsonl");
126
+
127
+ var records = [];
128
+ var timerId = null;
129
+ var CHECK_INTERVAL = 30 * 1000;
130
+ var lastTriggeredMinute = {};
131
+
132
+ // --- Persistence (JSONL) ---
133
+
134
+ function load() {
135
+ try {
136
+ var raw = fs.readFileSync(registryPath, "utf8");
137
+ var lines = raw.trim().split("\n");
138
+ records = [];
139
+ for (var i = 0; i < lines.length; i++) {
140
+ if (!lines[i].trim()) continue;
141
+ try {
142
+ var rec = JSON.parse(lines[i]);
143
+ // Recalculate nextRunAt for scheduled records
144
+ if (rec.cron && rec.enabled) {
145
+ rec.nextRunAt = nextRunTime(rec.cron);
146
+ } else if (!rec.cron && rec.enabled && rec.date && rec.time && rec.source === "schedule") {
147
+ // One-off: recalculate from date+time
148
+ var dtP = rec.date.split("-");
149
+ var tmP = rec.time.split(":");
150
+ var runD = new Date(parseInt(dtP[0], 10), parseInt(dtP[1], 10) - 1, parseInt(dtP[2], 10), parseInt(tmP[0], 10), parseInt(tmP[1], 10), 0);
151
+ rec.nextRunAt = runD.getTime();
152
+ }
153
+ records.push(rec);
154
+ } catch (e) {
155
+ // skip malformed line
156
+ }
157
+ }
158
+ } catch (e) {
159
+ records = [];
160
+ }
161
+ }
162
+
163
+ function save() {
164
+ try {
165
+ fs.mkdirSync(registryDir, { recursive: true });
166
+ var lines = [];
167
+ for (var i = 0; i < records.length; i++) {
168
+ lines.push(JSON.stringify(records[i]));
169
+ }
170
+ var tmpPath = registryPath + ".tmp";
171
+ fs.writeFileSync(tmpPath, lines.join("\n") + "\n");
172
+ fs.renameSync(tmpPath, registryPath);
173
+ } catch (e) {
174
+ console.error("[loop-registry] Failed to save:", e.message);
175
+ }
176
+ }
177
+
178
+ // --- Timer (scheduled loops only) ---
179
+
180
+ function startTimer() {
181
+ if (timerId) return;
182
+ timerId = setInterval(function () {
183
+ tick();
184
+ }, CHECK_INTERVAL);
185
+ tick();
186
+ }
187
+
188
+ function stopTimer() {
189
+ if (timerId) {
190
+ clearInterval(timerId);
191
+ timerId = null;
192
+ }
193
+ }
194
+
195
+ function tick() {
196
+ var now = Date.now();
197
+ var nowMinuteKey = Math.floor(now / 60000);
198
+
199
+ for (var i = 0; i < records.length; i++) {
200
+ var rec = records[i];
201
+ if (!rec.enabled) continue;
202
+ if (!rec.nextRunAt) continue;
203
+ if (rec.nextRunAt > now) continue;
204
+
205
+ // Avoid double-trigger within same minute
206
+ var triggerKey = rec.id + "_" + nowMinuteKey;
207
+ if (lastTriggeredMinute[triggerKey]) continue;
208
+ lastTriggeredMinute[triggerKey] = true;
209
+
210
+ // Clean old trigger keys
211
+ var keys = Object.keys(lastTriggeredMinute);
212
+ for (var k = 0; k < keys.length; k++) {
213
+ var keyParts = keys[k].split("_");
214
+ var keyMinute = parseInt(keyParts[keyParts.length - 1], 10);
215
+ if (keyMinute < nowMinuteKey - 1) {
216
+ delete lastTriggeredMinute[keys[k]];
217
+ }
218
+ }
219
+
220
+ // Check recurrenceEnd conditions before triggering
221
+ if (rec.recurrenceEnd) {
222
+ var shouldDisable = false;
223
+ if (rec.recurrenceEnd.type === "until" && rec.recurrenceEnd.date) {
224
+ var reParts = rec.recurrenceEnd.date.split("-");
225
+ var endDate = new Date(parseInt(reParts[0], 10), parseInt(reParts[1], 10) - 1, parseInt(reParts[2], 10), 23, 59, 59, 999);
226
+ if (now > endDate.getTime()) {
227
+ shouldDisable = true;
228
+ }
229
+ } else if (rec.recurrenceEnd.type === "after" && rec.recurrenceEnd.count > 0) {
230
+ if ((rec.runs || []).length >= rec.recurrenceEnd.count) {
231
+ shouldDisable = true;
232
+ }
233
+ }
234
+ if (shouldDisable) {
235
+ rec.enabled = false;
236
+ rec.nextRunAt = null;
237
+ save();
238
+ if (onChange) onChange(records);
239
+ continue;
240
+ }
241
+ }
242
+
243
+ // Check intervalEnd conditions before triggering
244
+ if (rec.intervalEnd) {
245
+ var skipTrigger = false;
246
+ if (rec.intervalEnd.type === "until" && rec.intervalEnd.time) {
247
+ // Stop at a specific time of day
248
+ var nowDate = new Date(now);
249
+ var ieParts = rec.intervalEnd.time.split(":");
250
+ var stopH = parseInt(ieParts[0], 10);
251
+ var stopM = parseInt(ieParts[1], 10) || 0;
252
+ var nowMinOfDay = nowDate.getHours() * 60 + nowDate.getMinutes();
253
+ if (nowMinOfDay >= stopH * 60 + stopM) {
254
+ skipTrigger = true;
255
+ }
256
+ } else if (rec.intervalEnd.type === "after" && rec.intervalEnd.count > 0) {
257
+ // Stop after N runs today
258
+ var todayStart = new Date(now);
259
+ todayStart.setHours(0, 0, 0, 0);
260
+ var todayStartMs = todayStart.getTime();
261
+ var todayRuns = (rec.runs || []).filter(function (r) { return r.startedAt >= todayStartMs; });
262
+ if (todayRuns.length >= rec.intervalEnd.count) {
263
+ skipTrigger = true;
264
+ }
265
+ }
266
+ if (skipTrigger) {
267
+ // Advance nextRunAt without triggering
268
+ if (rec.cron) {
269
+ rec.nextRunAt = nextRunTime(rec.cron, now);
270
+ }
271
+ save();
272
+ continue;
273
+ }
274
+ }
275
+
276
+ // Update nextRunAt
277
+ rec.lastRunAt = now;
278
+ if (rec.cron) {
279
+ rec.nextRunAt = nextRunTime(rec.cron, now);
280
+ } else {
281
+ // One-off schedule: disable after firing
282
+ rec.nextRunAt = null;
283
+ rec.enabled = false;
284
+ }
285
+ save();
286
+ if (onChange) onChange(records);
287
+
288
+ console.log("[loop-registry] Triggering scheduled loop: " + rec.name + " (" + rec.id + ")");
289
+ if (onTrigger) {
290
+ try { onTrigger(rec); } catch (e) {
291
+ console.error("[loop-registry] Trigger error:", e.message);
292
+ }
293
+ }
294
+ }
295
+ }
296
+
297
+ // --- CRUD ---
298
+
299
+ function register(data) {
300
+ var rec = {
301
+ id: data.id || ("loop_" + Date.now() + "_" + require("crypto").randomBytes(3).toString("hex")),
302
+ name: data.name || "Untitled",
303
+ task: data.task || "",
304
+ cron: data.cron || null,
305
+ enabled: data.cron ? (data.enabled !== false) : false,
306
+ maxIterations: (data.maxIterations >= 1) ? data.maxIterations : 20,
307
+ createdAt: Date.now(),
308
+ updatedAt: Date.now(),
309
+ lastRunAt: null,
310
+ lastRunResult: null,
311
+ nextRunAt: null,
312
+ description: data.description || "",
313
+ date: data.date || null,
314
+ time: data.time || null,
315
+ allDay: data.allDay !== undefined ? data.allDay : true,
316
+ linkedTaskId: data.linkedTaskId || null,
317
+ craftingSessionId: data.craftingSessionId || null,
318
+ source: data.source || null,
319
+ color: data.color || null,
320
+ recurrenceEnd: data.recurrenceEnd || null,
321
+ mode: data.mode || "loop",
322
+ prompt: data.prompt || null,
323
+ skipIfRunning: data.skipIfRunning !== undefined ? data.skipIfRunning : true,
324
+ intervalEnd: data.intervalEnd || null,
325
+ runs: [],
326
+ };
327
+ if (rec.cron && rec.enabled) {
328
+ rec.nextRunAt = nextRunTime(rec.cron);
329
+ } else if (!rec.cron && rec.date && rec.time && rec.source === "schedule") {
330
+ // One-off schedule: compute nextRunAt from date + time
331
+ var dtParts = rec.date.split("-");
332
+ var tmParts = rec.time.split(":");
333
+ var runDate = new Date(parseInt(dtParts[0], 10), parseInt(dtParts[1], 10) - 1, parseInt(dtParts[2], 10), parseInt(tmParts[0], 10), parseInt(tmParts[1], 10), 0);
334
+ rec.nextRunAt = runDate.getTime();
335
+ rec.enabled = true;
336
+ }
337
+ records.push(rec);
338
+ save();
339
+ if (onChange) onChange(records);
340
+ return rec;
341
+ }
342
+
343
+ function update(id, data) {
344
+ var rec = getById(id);
345
+ if (!rec) return null;
346
+
347
+ if (data.name !== undefined) rec.name = data.name;
348
+ if (data.cron !== undefined) rec.cron = data.cron;
349
+ if (data.enabled !== undefined) rec.enabled = data.enabled;
350
+ if (data.maxIterations !== undefined) rec.maxIterations = data.maxIterations;
351
+ if (data.date !== undefined) rec.date = data.date;
352
+ if (data.time !== undefined) rec.time = data.time;
353
+ if (data.recurrenceEnd !== undefined) rec.recurrenceEnd = data.recurrenceEnd;
354
+ if (data.mode !== undefined) rec.mode = data.mode;
355
+ if (data.prompt !== undefined) rec.prompt = data.prompt;
356
+ if (data.description !== undefined) rec.description = data.description;
357
+ if (data.color !== undefined) rec.color = data.color;
358
+ if (data.allDay !== undefined) rec.allDay = data.allDay;
359
+ if (data.linkedTaskId !== undefined) rec.linkedTaskId = data.linkedTaskId;
360
+ if (data.skipIfRunning !== undefined) rec.skipIfRunning = data.skipIfRunning;
361
+ if (data.intervalEnd !== undefined) rec.intervalEnd = data.intervalEnd;
362
+ rec.updatedAt = Date.now();
363
+ if (rec.cron && rec.enabled) {
364
+ rec.nextRunAt = nextRunTime(rec.cron);
365
+ } else if (!rec.cron && rec.date && rec.time && rec.source === "schedule") {
366
+ var dtP2 = rec.date.split("-");
367
+ var tmP2 = rec.time.split(":");
368
+ var runD2 = new Date(parseInt(dtP2[0], 10), parseInt(dtP2[1], 10) - 1, parseInt(dtP2[2], 10), parseInt(tmP2[0], 10), parseInt(tmP2[1], 10), 0);
369
+ rec.nextRunAt = runD2.getTime();
370
+ rec.enabled = true;
371
+ } else {
372
+ rec.nextRunAt = null;
373
+ }
374
+
375
+ save();
376
+ if (onChange) onChange(records);
377
+ return rec;
378
+ }
379
+
380
+ function updateRecord(id, data) {
381
+ var rec = getById(id);
382
+ if (!rec) return null;
383
+ var keys = Object.keys(data);
384
+ for (var i = 0; i < keys.length; i++) {
385
+ rec[keys[i]] = data[keys[i]];
386
+ }
387
+ save();
388
+ if (onChange) onChange(records);
389
+ return rec;
390
+ }
391
+
392
+ function remove(id) {
393
+ var idx = -1;
394
+ for (var i = 0; i < records.length; i++) {
395
+ if (records[i].id === id) { idx = i; break; }
396
+ }
397
+ if (idx === -1) return false;
398
+ records.splice(idx, 1);
399
+ save();
400
+ if (onChange) onChange(records);
401
+ return true;
402
+ }
403
+
404
+ function toggleEnabled(id) {
405
+ var rec = getById(id);
406
+ if (!rec || !rec.cron) return null; // only toggle scheduled loops
407
+ rec.enabled = !rec.enabled;
408
+ rec.updatedAt = Date.now();
409
+ rec.nextRunAt = rec.enabled ? nextRunTime(rec.cron) : null;
410
+ save();
411
+ if (onChange) onChange(records);
412
+ return rec;
413
+ }
414
+
415
+ function recordRun(id, result) {
416
+ var rec = getById(id);
417
+ if (!rec) return;
418
+ rec.lastRunAt = result.startedAt || Date.now();
419
+ rec.lastRunResult = result.reason || null;
420
+ rec.runs.push({
421
+ startedAt: result.startedAt || rec.lastRunAt,
422
+ finishedAt: Date.now(),
423
+ result: result.reason || "unknown",
424
+ iterations: result.iterations || 0,
425
+ });
426
+ // Keep only last 20 run entries
427
+ if (rec.runs.length > 20) {
428
+ rec.runs = rec.runs.slice(-20);
429
+ }
430
+ save();
431
+ if (onChange) onChange(records);
432
+ }
433
+
434
+ function getAll() {
435
+ return records;
436
+ }
437
+
438
+ function getById(id) {
439
+ for (var i = 0; i < records.length; i++) {
440
+ if (records[i].id === id) return records[i];
441
+ }
442
+ return null;
443
+ }
444
+
445
+ function getScheduled() {
446
+ var result = [];
447
+ for (var i = 0; i < records.length; i++) {
448
+ if (records[i].cron) result.push(records[i]);
449
+ }
450
+ return result;
451
+ }
452
+
453
+ return {
454
+ load: load,
455
+ save: save,
456
+ startTimer: startTimer,
457
+ stopTimer: stopTimer,
458
+ register: register,
459
+ update: update,
460
+ updateRecord: updateRecord,
461
+ remove: remove,
462
+ toggleEnabled: toggleEnabled,
463
+ recordRun: recordRun,
464
+ getAll: getAll,
465
+ getById: getById,
466
+ getScheduled: getScheduled,
467
+ nextRunTime: nextRunTime,
468
+ };
469
+ }
470
+
471
+ module.exports = { createLoopRegistry: createLoopRegistry };