@saltpub/agent-autoloop 0.0.0-stage → 0.1.3

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 (177) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/INSTALL.md +120 -0
  3. package/LICENSE +21 -0
  4. package/README.en.md +268 -0
  5. package/README.md +264 -2
  6. package/SECURITY.md +49 -0
  7. package/dist/bin/agent-autoloop.js +2 -0
  8. package/dist/cli/agents.js +14 -0
  9. package/dist/cli/backup.js +144 -0
  10. package/dist/cli/dashboard/app.js +424 -0
  11. package/dist/cli/dashboard/index.html +15 -0
  12. package/dist/cli/dashboard/style.css +629 -0
  13. package/dist/cli/dashboard.js +94 -0
  14. package/dist/cli/explain.js +84 -0
  15. package/dist/cli/main.js +1142 -0
  16. package/dist/cli/mcp.js +151 -0
  17. package/dist/cli/runs.js +40 -0
  18. package/dist/cli/spend.js +21 -0
  19. package/dist/cli/status.js +272 -0
  20. package/dist/cli/try.js +6 -0
  21. package/dist/cli/ui.js +75 -0
  22. package/dist/cli/uninstall.js +86 -0
  23. package/dist/cli/update.js +65 -0
  24. package/dist/cli/why.js +115 -0
  25. package/dist/host/background-launcher.js +12 -0
  26. package/dist/host/background.js +24 -0
  27. package/dist/host/channels.js +4 -0
  28. package/dist/host/command.js +98 -0
  29. package/dist/host/config.js +37 -0
  30. package/dist/host/files.js +22 -0
  31. package/dist/host/home.js +5 -0
  32. package/dist/host/http.js +45 -0
  33. package/dist/host/launcher.js +188 -0
  34. package/dist/host/lines.js +15 -0
  35. package/dist/host/node-path.js +33 -0
  36. package/dist/host/npm.js +28 -0
  37. package/dist/host/plugins.js +26 -0
  38. package/dist/host/program.js +14 -0
  39. package/dist/host/runtime.js +179 -0
  40. package/dist/host/server.js +159 -0
  41. package/dist/host/service.js +325 -0
  42. package/dist/host/state.js +203 -0
  43. package/dist/host/up.js +162 -0
  44. package/dist/host/update.js +286 -0
  45. package/dist/host/version.js +81 -0
  46. package/dist/kernel/card-page.js +54 -0
  47. package/dist/kernel/credentials.js +16 -0
  48. package/dist/kernel/cron.js +135 -0
  49. package/dist/kernel/decide.js +826 -0
  50. package/dist/kernel/due.js +73 -0
  51. package/dist/kernel/errors.js +9 -0
  52. package/dist/kernel/evolve.js +264 -0
  53. package/dist/kernel/http.js +357 -0
  54. package/dist/kernel/query.js +202 -0
  55. package/dist/kernel/rules.js +20 -0
  56. package/dist/kernel/store.js +52 -0
  57. package/dist/plugins/acp/agent.js +32 -0
  58. package/dist/plugins/acp/bridge.js +204 -0
  59. package/dist/plugins/claude/agent.js +113 -0
  60. package/dist/plugins/codex/agent.js +105 -0
  61. package/dist/plugins/command/agent.js +25 -0
  62. package/dist/plugins/delivery/action.js +25 -0
  63. package/dist/plugins/delivery/agent.js +13 -0
  64. package/dist/plugins/delivery/delivery.js +388 -0
  65. package/dist/plugins/delivery/github.js +201 -0
  66. package/dist/plugins/delivery/handoff.js +50 -0
  67. package/dist/plugins/delivery/input.js +47 -0
  68. package/dist/plugins/delivery/poll.js +72 -0
  69. package/dist/plugins/delivery/project.js +144 -0
  70. package/dist/plugins/delivery/review-ledger.js +82 -0
  71. package/dist/plugins/delivery/review-policy.js +99 -0
  72. package/dist/plugins/delivery/review.js +280 -0
  73. package/dist/plugins/delivery/run.js +204 -0
  74. package/dist/plugins/desktop/channel.js +94 -0
  75. package/dist/plugins/dsh/agent.js +116 -0
  76. package/dist/plugins/feishu/channel.js +245 -0
  77. package/dist/plugins/github/source.js +30 -0
  78. package/dist/plugins/gitlab/source.js +30 -0
  79. package/dist/plugins/omp/agent.js +3 -0
  80. package/dist/plugins/opencode/agent.js +88 -0
  81. package/dist/plugins/pi/agent.js +92 -0
  82. package/dist/plugins/webhook/channel.js +70 -0
  83. package/dist/plugins/webhook/templates.js +34 -0
  84. package/dist/plugins/xiaojiu/channel.js +410 -0
  85. package/dist/plugins/zcode/agent.js +102 -0
  86. package/dist/protocol/client.js +56 -0
  87. package/dist/protocol/conformance.js +184 -0
  88. package/dist/protocol/job.js +1 -0
  89. package/dist/protocol/rules.js +230 -0
  90. package/dist/protocol/schema.js +70 -0
  91. package/dist/protocol/schema.json +985 -0
  92. package/dist/protocol/validate.js +150 -0
  93. package/dist/worker/agents.js +47 -0
  94. package/dist/worker/contract.js +178 -0
  95. package/dist/worker/run.js +52 -0
  96. package/dist/worker/usage.js +45 -0
  97. package/dist/worker/windows.ps1 +48 -0
  98. package/dist/worker/worker.js +595 -0
  99. package/docs/architecture.en.svg +178 -0
  100. package/docs/architecture.svg +178 -0
  101. package/docs/auto-update.md +53 -0
  102. package/docs/plugins.md +139 -0
  103. package/docs/protocol.md +582 -0
  104. package/docs/state-machines.md +77 -0
  105. package/examples/channel.sh +15 -0
  106. package/examples/plugins/hello-agent/agent.ts +40 -0
  107. package/examples/plugins/hello-channel/channel.ts +54 -0
  108. package/examples/plugins/hello-source/source.ts +19 -0
  109. package/examples/recipes/browser-handoff/README.md +44 -0
  110. package/examples/recipes/browser-handoff/SKILL.md +22 -0
  111. package/examples/recipes/browser-handoff/browser.json +10 -0
  112. package/examples/recipes/browser-handoff/handoff.ts +44 -0
  113. package/examples/recipes/browser-handoff/rules.json +19 -0
  114. package/examples/recipes/feedback/README.md +34 -0
  115. package/examples/recipes/feedback/SKILL.md +22 -0
  116. package/examples/recipes/feedback/event.json +8 -0
  117. package/examples/recipes/feedback/rules.json +37 -0
  118. package/examples/recipes/feishu-brief/README.md +28 -0
  119. package/examples/recipes/feishu-brief/SKILL.md +16 -0
  120. package/examples/recipes/feishu-brief/collect.ts +164 -0
  121. package/examples/recipes/feishu-brief/rules.json +57 -0
  122. package/examples/recipes/feishu-brief/sources.json +13 -0
  123. package/examples/recipes/github-issues/README.md +17 -0
  124. package/examples/recipes/github-issues/SKILL.md +18 -0
  125. package/examples/recipes/github-issues/rules.json +30 -0
  126. package/examples/recipes/inbox-triage/README.md +36 -0
  127. package/examples/recipes/inbox-triage/SKILL.md +19 -0
  128. package/examples/recipes/inbox-triage/collect.ts +191 -0
  129. package/examples/recipes/inbox-triage/rules.json +57 -0
  130. package/examples/recipes/inbox-triage/sources.json +8 -0
  131. package/examples/recipes/morning-brief/README.md +17 -0
  132. package/examples/recipes/morning-brief/SKILL.md +17 -0
  133. package/examples/recipes/morning-brief/rules.json +16 -0
  134. package/examples/recipes/ops-alerts/README.md +18 -0
  135. package/examples/recipes/ops-alerts/SKILL.md +34 -0
  136. package/examples/recipes/ops-alerts/restart.sh +5 -0
  137. package/examples/recipes/ops-alerts/rules.json +29 -0
  138. package/examples/recipes/website-watch/README.md +26 -0
  139. package/examples/recipes/website-watch/SKILL.md +19 -0
  140. package/examples/recipes/website-watch/check.ts +86 -0
  141. package/examples/recipes/website-watch/rules.json +56 -0
  142. package/examples/recipes/website-watch/site.json +6 -0
  143. package/examples/skills/weekly-sweep/SKILL.md +17 -0
  144. package/examples/watch-github-issues.sh +23 -0
  145. package/examples/worker.sh +18 -0
  146. package/package.json +57 -4
  147. package/plugins/acp/agent.ts +31 -0
  148. package/plugins/acp/bridge.ts +190 -0
  149. package/plugins/claude/agent.ts +121 -0
  150. package/plugins/codex/agent.ts +103 -0
  151. package/plugins/command/agent.ts +25 -0
  152. package/plugins/delivery/action.ts +29 -0
  153. package/plugins/delivery/action.yml +28 -0
  154. package/plugins/delivery/agent.ts +15 -0
  155. package/plugins/delivery/delivery.ts +408 -0
  156. package/plugins/delivery/github.ts +207 -0
  157. package/plugins/delivery/handoff.ts +72 -0
  158. package/plugins/delivery/input.ts +55 -0
  159. package/plugins/delivery/poll.ts +65 -0
  160. package/plugins/delivery/project.ts +161 -0
  161. package/plugins/delivery/review-ledger.ts +90 -0
  162. package/plugins/delivery/review-policy.ts +126 -0
  163. package/plugins/delivery/review.ts +324 -0
  164. package/plugins/delivery/run.ts +219 -0
  165. package/plugins/desktop/channel.ts +100 -0
  166. package/plugins/dsh/agent.ts +100 -0
  167. package/plugins/feishu/channel.ts +243 -0
  168. package/plugins/github/source.ts +33 -0
  169. package/plugins/gitlab/source.ts +32 -0
  170. package/plugins/omp/agent.ts +4 -0
  171. package/plugins/opencode/agent.ts +78 -0
  172. package/plugins/pi/agent.ts +80 -0
  173. package/plugins/webhook/channel.ts +74 -0
  174. package/plugins/webhook/templates.ts +32 -0
  175. package/plugins/xiaojiu/channel.ts +426 -0
  176. package/plugins/zcode/agent.ts +96 -0
  177. package/skills/agent-autoloop/SKILL.md +260 -0
@@ -0,0 +1,135 @@
1
+ // Five-field cron in the rules' time zone: when the next one comes. As in cron, a day of the month and a day of the
2
+ // week both given take either, but when either starts with * a day must match both; 0 and 7 are Sunday. A wall-clock
3
+ // moment that doesn't exist (clocks going forward) is skipped, and one that happens twice (clocks going back) counts
4
+ // the first time, so a schedule fires at most once per wall-clock moment.
5
+ const MINUTE = 60_000;
6
+ const HOUR = 60 * MINUTE;
7
+ const DAY = 24 * HOUR;
8
+ /** Far enough for a leap day; a schedule with nothing in it never comes. */
9
+ const HORIZON = 8 * 366;
10
+ /** What each of the five fields may hold. */
11
+ const LOWEST = [0, 0, 1, 1, 0];
12
+ const HIGHEST = [59, 23, 31, 12, 7];
13
+ function field(text, min, max) {
14
+ const values = new Set();
15
+ for (const part of text.split(",")) {
16
+ const match = /^(\*|(\d+)(?:-(\d+))?)(?:\/(\d+))?$/.exec(part);
17
+ if (!match)
18
+ return null;
19
+ const [, all, from, to, step] = match;
20
+ const start = all === "*" ? min : Number(from);
21
+ const end = all === "*" ? max : to !== undefined ? Number(to) : step !== undefined ? max : start;
22
+ const by = step === undefined ? 1 : Number(step);
23
+ if (start < min || end > max || start > end || by < 1)
24
+ return null;
25
+ for (let value = start; value <= end; value += by)
26
+ values.add(value);
27
+ }
28
+ return values;
29
+ }
30
+ export function parseCron(text) {
31
+ const parts = text.trim().split(/\s+/);
32
+ if (parts.length !== 5)
33
+ return null;
34
+ const sets = parts.map((part, index) => field(part, LOWEST[index], HIGHEST[index]));
35
+ if (sets.some((set) => set === null))
36
+ return null;
37
+ const [minutes, hours, days, months, weekdays] = sets;
38
+ if (weekdays.delete(7))
39
+ weekdays.add(0);
40
+ const sorted = (set) => [...set].sort((a, b) => a - b);
41
+ const anyDay = parts[2].startsWith("*");
42
+ const anyWeekday = parts[4].startsWith("*");
43
+ return { minutes: sorted(minutes), hours: sorted(hours), days, months, weekdays, anyDay, anyWeekday };
44
+ }
45
+ const formats = new Map();
46
+ function wallClock(ms, zone) {
47
+ let format = formats.get(zone);
48
+ if (!format) {
49
+ const fields = { year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric" };
50
+ format = new Intl.DateTimeFormat("en-US", { timeZone: zone, hourCycle: "h23", ...fields });
51
+ formats.set(zone, format);
52
+ }
53
+ return Object.fromEntries(format.formatToParts(ms).map((part) => [part.type, Number(part.value)]));
54
+ }
55
+ /** How far the zone's clock is ahead of UTC at this moment. */
56
+ function offset(ms, zone) {
57
+ const clock = wallClock(ms, zone);
58
+ return Date.UTC(clock.year, clock.month - 1, clock.day, clock.hour, clock.minute) - Math.floor(ms / MINUTE) * MINUTE;
59
+ }
60
+ /** The first moment this zone's clock reads this, or null if it never does. */
61
+ function moment(year, month, day, hour, minute, zone) {
62
+ const guess = Date.UTC(year, month - 1, day, hour, minute);
63
+ const tries = [...new Set([guess - offset(guess - DAY, zone), guess - offset(guess + DAY, zone)])];
64
+ const matches = (at) => {
65
+ const clock = wallClock(at, zone);
66
+ return clock.day === day && clock.hour === hour && clock.minute === minute;
67
+ };
68
+ return tries.sort((a, b) => a - b).find(matches) ?? null;
69
+ }
70
+ /** The first minute strictly after `after` that the schedule names, or null if it never comes. */
71
+ export function nextRun(cron, after, zone) {
72
+ const start = wallClock(after, zone);
73
+ const first = Date.UTC(start.year, start.month - 1, start.day);
74
+ for (let dayIndex = 0; dayIndex < HORIZON; dayIndex++) {
75
+ const date = new Date(first + dayIndex * DAY);
76
+ if (!cron.months.has(date.getUTCMonth() + 1))
77
+ continue;
78
+ const onDay = cron.days.has(date.getUTCDate());
79
+ const onWeekday = cron.weekdays.has(date.getUTCDay());
80
+ if (!(cron.anyDay || cron.anyWeekday ? onDay && onWeekday : onDay || onWeekday))
81
+ continue;
82
+ for (const hour of cron.hours) {
83
+ if (dayIndex === 0 && hour < start.hour)
84
+ continue;
85
+ for (const minute of cron.minutes) {
86
+ // Earlier on the first day has already happened: a moment counts the first time the clock reads it.
87
+ if (dayIndex === 0 && hour === start.hour && minute < start.minute)
88
+ continue;
89
+ const at = moment(date.getUTCFullYear(), date.getUTCMonth() + 1, date.getUTCDate(), hour, minute, zone);
90
+ if (at !== null && at > after)
91
+ return at;
92
+ }
93
+ }
94
+ }
95
+ return null;
96
+ }
97
+ /** The last time due at or before `now`, given that `due` was due: what a hub asleep for a while still owes, once. */
98
+ export function latestBefore(cron, due, now, zone) {
99
+ // Looks back over ever wider windows, so a minutely schedule after a year asleep doesn't walk the year.
100
+ for (const window of [HOUR, DAY, 32 * DAY, 400 * DAY, Infinity]) {
101
+ let latest = null;
102
+ let at = window === Infinity || now - window < due ? due : nextRun(cron, now - window, zone);
103
+ while (at !== null && at <= now) {
104
+ latest = at;
105
+ at = nextRun(cron, at, zone);
106
+ }
107
+ if (latest !== null)
108
+ return latest;
109
+ }
110
+ return due;
111
+ }
112
+ export function dayStart(now, zone) {
113
+ const clock = wallClock(now, zone);
114
+ return moment(clock.year, clock.month, clock.day, 0, 0, zone) ?? now;
115
+ }
116
+ // With the rest of the cron and not with the status summary: a client's own page can import this file, which needs nothing
117
+ // of Node.
118
+ const WEEKDAYS = ["日", "一", "二", "三", "四", "五", "六", "日"];
119
+ /** A common schedule in plain words (每天 18:00, 每周日 06:00, 工作日 09:00, 每月 1 日 04:00); any other as written. */
120
+ export function describeCron(text) {
121
+ const [minute, hour, day, month, weekday] = text.trim().split(/\s+/);
122
+ const number = /^\d+$/;
123
+ if (!number.test(minute) || !number.test(hour) || month !== "*" || !parseCron(text))
124
+ return text;
125
+ const at = `${hour.padStart(2, "0")}:${minute.padStart(2, "0")}`;
126
+ if (day === "*" && weekday === "*")
127
+ return `每天 ${at}`;
128
+ if (day === "*" && number.test(weekday))
129
+ return `每周${WEEKDAYS[Number(weekday)]} ${at}`;
130
+ if (day === "*" && weekday === "1-5")
131
+ return `工作日 ${at}`;
132
+ if (number.test(day) && weekday === "*")
133
+ return `每月 ${Number(day)} 日 ${at}`;
134
+ return text;
135
+ }