@tidegap/core 0.18.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/agent-snippet.d.ts +30 -0
- package/dist/agent-snippet.js +120 -0
- package/dist/budget.d.ts +0 -12
- package/dist/budget.js +2 -24
- package/dist/channels/commands.d.ts +137 -0
- package/dist/channels/commands.js +377 -0
- package/dist/channels/delivery-codes.d.ts +9 -0
- package/dist/channels/delivery-codes.js +53 -0
- package/dist/channels/digest.d.ts +102 -0
- package/dist/channels/digest.js +146 -0
- package/dist/channels/format.d.ts +127 -0
- package/dist/channels/format.js +407 -0
- package/dist/channels/index.d.ts +15 -0
- package/dist/channels/index.js +15 -0
- package/dist/channels/kinds/command.d.ts +13 -0
- package/dist/channels/kinds/command.js +65 -0
- package/dist/channels/kinds/discord.d.ts +16 -0
- package/dist/channels/kinds/discord.js +67 -0
- package/dist/channels/kinds/email.d.ts +20 -0
- package/dist/channels/kinds/email.js +162 -0
- package/dist/channels/kinds/index.d.ts +8 -0
- package/dist/channels/kinds/index.js +17 -0
- package/dist/channels/kinds/ntfy.d.ts +25 -0
- package/dist/channels/kinds/ntfy.js +163 -0
- package/dist/channels/kinds/pushover.d.ts +11 -0
- package/dist/channels/kinds/pushover.js +77 -0
- package/dist/channels/kinds/shared.d.ts +25 -0
- package/dist/channels/kinds/shared.js +39 -0
- package/dist/channels/kinds/shortcuts.d.ts +9 -0
- package/dist/channels/kinds/shortcuts.js +45 -0
- package/dist/channels/kinds/slack.d.ts +8 -0
- package/dist/channels/kinds/slack.js +57 -0
- package/dist/channels/kinds/telegram.d.ts +4 -0
- package/dist/channels/kinds/telegram.js +26 -0
- package/dist/channels/kinds/webhook.d.ts +30 -0
- package/dist/channels/kinds/webhook.js +177 -0
- package/dist/channels/payload.d.ts +36 -0
- package/dist/channels/payload.js +61 -0
- package/dist/channels/plan.d.ts +24 -0
- package/dist/channels/plan.js +66 -0
- package/dist/channels/registry.d.ts +191 -0
- package/dist/channels/registry.js +125 -0
- package/dist/channels/telegram.d.ts +110 -0
- package/dist/channels/telegram.js +168 -0
- package/dist/channels/types.d.ts +107 -0
- package/dist/channels/types.js +26 -0
- package/dist/claude-tools.d.ts +29 -0
- package/dist/claude-tools.js +49 -0
- package/dist/data/tables.generated.d.ts +6 -0
- package/dist/data/tables.generated.js +37 -0
- package/dist/energy.d.ts +77 -0
- package/dist/energy.js +131 -0
- package/dist/grid.d.ts +19 -2
- package/dist/grid.js +84 -28
- package/dist/handoff.d.ts +152 -0
- package/dist/handoff.js +430 -0
- package/dist/history.d.ts +2 -0
- package/dist/history.js +3 -1
- package/dist/index.d.ts +46 -16
- package/dist/index.js +34 -8
- package/dist/job-cadence.d.ts +76 -0
- package/dist/job-cadence.js +240 -0
- package/dist/jobs.d.ts +173 -0
- package/dist/jobs.js +794 -0
- package/dist/notify.d.ts +2 -0
- package/dist/notify.js +2 -1
- package/dist/paths.d.ts +13 -46
- package/dist/paths.js +51 -219
- package/dist/postpone.d.ts +113 -0
- package/dist/postpone.js +185 -0
- package/dist/providers/anthropic.js +7 -3
- package/dist/providers/base.d.ts +113 -1
- package/dist/providers/base.js +15 -0
- package/dist/providers/budget.d.ts +25 -0
- package/dist/providers/budget.js +57 -0
- package/dist/providers/defaults.js +4 -0
- package/dist/providers/defer.d.ts +16 -0
- package/dist/providers/defer.js +31 -0
- package/dist/providers/gemini.d.ts +1 -0
- package/dist/providers/gemini.js +27 -20
- package/dist/providers/index.d.ts +2 -2
- package/dist/providers/index.js +1 -1
- package/dist/providers/ollama.d.ts +1 -0
- package/dist/providers/ollama.js +26 -22
- package/dist/providers/openai.js +5 -5
- package/dist/providers/timeout.d.ts +18 -0
- package/dist/providers/timeout.js +33 -0
- package/dist/quota.d.ts +7 -2
- package/dist/quota.js +13 -29
- package/dist/readiness.d.ts +169 -0
- package/dist/readiness.js +254 -0
- package/dist/redact.d.ts +42 -0
- package/dist/redact.js +110 -0
- package/dist/region-host.d.ts +30 -0
- package/dist/region-host.js +69 -0
- package/dist/region.d.ts +102 -20
- package/dist/region.js +175 -23
- package/dist/run-session.d.ts +36 -0
- package/dist/run-session.js +72 -0
- package/dist/schedule-job.d.ts +95 -0
- package/dist/schedule-job.js +195 -0
- package/dist/scheduler.d.ts +245 -14
- package/dist/scheduler.js +925 -110
- package/dist/sign.d.ts +8 -1
- package/dist/sign.js +49 -3
- package/dist/storage/sqlite.d.ts +157 -5
- package/dist/storage/sqlite.js +661 -20
- package/dist/task-events.d.ts +102 -0
- package/dist/task-events.js +308 -0
- package/dist/tool-surface.d.ts +31 -2
- package/dist/tool-surface.js +359 -65
- package/dist/types.d.ts +246 -3
- package/dist/types.js +8 -1
- package/dist/zone-time.d.ts +22 -0
- package/dist/zone-time.js +76 -0
- package/package.json +11 -6
- package/dist/aggregator.d.ts.map +0 -1
- package/dist/aggregator.js.map +0 -1
- package/dist/budget.d.ts.map +0 -1
- package/dist/budget.js.map +0 -1
- package/dist/data/tables.generated.d.ts.map +0 -1
- package/dist/data/tables.generated.js.map +0 -1
- package/dist/energy.d.ts.map +0 -1
- package/dist/energy.js.map +0 -1
- package/dist/grid.d.ts.map +0 -1
- package/dist/grid.js.map +0 -1
- package/dist/history.d.ts.map +0 -1
- package/dist/history.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/notify.d.ts.map +0 -1
- package/dist/notify.js.map +0 -1
- package/dist/paths.d.ts.map +0 -1
- package/dist/paths.js.map +0 -1
- package/dist/providers/anthropic.d.ts.map +0 -1
- package/dist/providers/anthropic.js.map +0 -1
- package/dist/providers/base.d.ts.map +0 -1
- package/dist/providers/base.js.map +0 -1
- package/dist/providers/defaults.d.ts.map +0 -1
- package/dist/providers/defaults.js.map +0 -1
- package/dist/providers/gemini.d.ts.map +0 -1
- package/dist/providers/gemini.js.map +0 -1
- package/dist/providers/index.d.ts.map +0 -1
- package/dist/providers/index.js.map +0 -1
- package/dist/providers/ollama.d.ts.map +0 -1
- package/dist/providers/ollama.js.map +0 -1
- package/dist/providers/openai.d.ts.map +0 -1
- package/dist/providers/openai.js.map +0 -1
- package/dist/quota.d.ts.map +0 -1
- package/dist/quota.js.map +0 -1
- package/dist/recommend.d.ts.map +0 -1
- package/dist/recommend.js.map +0 -1
- package/dist/redact.d.ts.map +0 -1
- package/dist/redact.js.map +0 -1
- package/dist/region.d.ts.map +0 -1
- package/dist/region.js.map +0 -1
- package/dist/routing.d.ts.map +0 -1
- package/dist/routing.js.map +0 -1
- package/dist/scheduler.d.ts.map +0 -1
- package/dist/scheduler.js.map +0 -1
- package/dist/select-window.d.ts.map +0 -1
- package/dist/select-window.js.map +0 -1
- package/dist/sign.d.ts.map +0 -1
- package/dist/sign.js.map +0 -1
- package/dist/storage/sqlite.d.ts.map +0 -1
- package/dist/storage/sqlite.js.map +0 -1
- package/dist/tool-surface.d.ts.map +0 -1
- package/dist/tool-surface.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bot commands and button presses (KTD15, R27): parse text and callback
|
|
3
|
+
* data into typed commands, then run them through the same `Scheduler`
|
|
4
|
+
* methods the page uses. Every write re-reads the task and checks it is
|
|
5
|
+
* still in a state that allows the action; a `/queue` number resolves only
|
|
6
|
+
* against the last listing and expires after {@link LISTING_TTL_MS}.
|
|
7
|
+
* `/jobs` lists recurring jobs and pauses, resumes or runs one through the
|
|
8
|
+
* core job actions; its numbers come from the last `/jobs` only.
|
|
9
|
+
*/
|
|
10
|
+
import { taskActions } from "../history.js";
|
|
11
|
+
import { getJob, JobActionError, listJobs, pauseJob, resumeJob, runJobNow } from "../jobs.js";
|
|
12
|
+
import { localTimeLabel, NEEDS_RESET_TIME, } from "../postpone.js";
|
|
13
|
+
import { CALLBACK_ACTIONS, CALLBACK_PRESETS, HELP_TEXT, JOB_RUNNING_NOW, jobNextLabel, JOBS_GRAMMAR, jobsMessage, jobTitle, LATER_GRAMMAR, laterMenuMessage, messageTitle, QUEUE_LIST_MAX, queueMessage, resultMessages, } from "./format.js";
|
|
14
|
+
/** `/queue` numbers expire after this long (R27). */
|
|
15
|
+
export const LISTING_TTL_MS = 10 * 60_000;
|
|
16
|
+
const JOB_ACTIONS = new Set(["pause", "resume", "run"]);
|
|
17
|
+
const CLOCK = /^([01]?\d|2[0-3]):([0-5]\d)$/;
|
|
18
|
+
function parseRef(word) {
|
|
19
|
+
if (!word)
|
|
20
|
+
return undefined;
|
|
21
|
+
if (/^\d{1,3}$/.test(word))
|
|
22
|
+
return { n: Number(word) };
|
|
23
|
+
return /^[A-Za-z0-9_~.-]{1,100}$/.test(word) ? { id: word } : undefined;
|
|
24
|
+
}
|
|
25
|
+
function refLabel(ref) {
|
|
26
|
+
return "n" in ref ? String(ref.n) : ref.id;
|
|
27
|
+
}
|
|
28
|
+
function parseLaterArg(words) {
|
|
29
|
+
const [w, extra] = words.map((x) => x.toLowerCase());
|
|
30
|
+
if (w === "1h" && extra === undefined)
|
|
31
|
+
return { preset: "1h" };
|
|
32
|
+
if (w === "tonight" && extra === undefined)
|
|
33
|
+
return { preset: "tonight" };
|
|
34
|
+
if (w === "tomorrow" && extra === undefined)
|
|
35
|
+
return { preset: "tomorrow_morning" };
|
|
36
|
+
if (w === "reset") {
|
|
37
|
+
if (extra === undefined)
|
|
38
|
+
return { preset: "after_limit_reset" };
|
|
39
|
+
return CLOCK.test(extra) && words.length === 2 ? { preset: "after_limit_reset", resetClock: extra } : undefined;
|
|
40
|
+
}
|
|
41
|
+
if (w !== undefined && CLOCK.test(w) && extra === undefined)
|
|
42
|
+
return { clock: w };
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
/** Parse a message text. A text that is not a command is `unknown`. */
|
|
46
|
+
export function parseCommand(text) {
|
|
47
|
+
const words = text.trim().split(/\s+/);
|
|
48
|
+
const head = words.shift() ?? "";
|
|
49
|
+
if (!head.startsWith("/"))
|
|
50
|
+
return { kind: "unknown" };
|
|
51
|
+
const name = head.slice(1).split("@")[0].toLowerCase();
|
|
52
|
+
switch (name) {
|
|
53
|
+
case "help":
|
|
54
|
+
return { kind: "help" };
|
|
55
|
+
case "queue":
|
|
56
|
+
return { kind: "queue" };
|
|
57
|
+
case "start":
|
|
58
|
+
case "pair":
|
|
59
|
+
return words[0] ? { kind: "pair", code: words[0] } : { kind: "pair" };
|
|
60
|
+
case "later": {
|
|
61
|
+
const ref = parseRef(words[0]);
|
|
62
|
+
const arg = parseLaterArg(words.slice(1));
|
|
63
|
+
return ref && arg ? { kind: "later", ref, arg } : { kind: "invalid", reply: `Usage: ${LATER_GRAMMAR}` };
|
|
64
|
+
}
|
|
65
|
+
case "jobs": {
|
|
66
|
+
if (words.length === 0)
|
|
67
|
+
return { kind: "jobs" };
|
|
68
|
+
const action = words[0].toLowerCase();
|
|
69
|
+
const ref = parseRef(words[1]);
|
|
70
|
+
if (!JOB_ACTIONS.has(action) || !ref || words.length > 2)
|
|
71
|
+
return { kind: "invalid", reply: `Usage: ${JOBS_GRAMMAR}` };
|
|
72
|
+
return { kind: "job", action: action, ref };
|
|
73
|
+
}
|
|
74
|
+
case "cancel":
|
|
75
|
+
case "run":
|
|
76
|
+
case "result": {
|
|
77
|
+
const ref = parseRef(words[0]);
|
|
78
|
+
if (!ref || words.length > 1)
|
|
79
|
+
return { kind: "invalid", reply: `Usage: /${name} <n|id>` };
|
|
80
|
+
return { kind: name, ref };
|
|
81
|
+
}
|
|
82
|
+
default:
|
|
83
|
+
return { kind: "unknown" };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** Parse `callback_data` (`<action>:<task_id>[:<preset code>]`); null when malformed. */
|
|
87
|
+
export function parseCallback(data) {
|
|
88
|
+
const m = /^([a-z]):([A-Za-z0-9_~.-]{1,100}?)(?::([a-z]))?$/.exec(data);
|
|
89
|
+
if (!m)
|
|
90
|
+
return null;
|
|
91
|
+
const action = CALLBACK_ACTIONS[m[1]];
|
|
92
|
+
if (!action)
|
|
93
|
+
return null;
|
|
94
|
+
if (m[3] === undefined)
|
|
95
|
+
return { action, taskId: m[2] };
|
|
96
|
+
const preset = CALLBACK_PRESETS[m[3]];
|
|
97
|
+
if (action !== "later" || !preset)
|
|
98
|
+
return null;
|
|
99
|
+
return { action, taskId: m[2], preset };
|
|
100
|
+
}
|
|
101
|
+
/** The next local `HH:MM` after `now` (today, else tomorrow). */
|
|
102
|
+
export function nextClockTime(clock, now) {
|
|
103
|
+
const m = CLOCK.exec(clock);
|
|
104
|
+
if (!m)
|
|
105
|
+
throw new Error(`not HH:MM: ${clock}`);
|
|
106
|
+
const at = new Date(now.getFullYear(), now.getMonth(), now.getDate(), Number(m[1]), Number(m[2]), 0, 0);
|
|
107
|
+
if (at.getTime() <= now.getTime())
|
|
108
|
+
at.setDate(at.getDate() + 1);
|
|
109
|
+
return at;
|
|
110
|
+
}
|
|
111
|
+
/** The core postpone target of a `/later` argument. */
|
|
112
|
+
export function laterTarget(arg, now) {
|
|
113
|
+
if ("clock" in arg)
|
|
114
|
+
return { until: nextClockTime(arg.clock, now) };
|
|
115
|
+
if (arg.preset === "after_limit_reset") {
|
|
116
|
+
return arg.resetClock ? { preset: "after_limit_reset", resetAt: nextClockTime(arg.resetClock, now) } : { preset: "after_limit_reset" };
|
|
117
|
+
}
|
|
118
|
+
return { preset: arg.preset };
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* The last `/queue` and `/jobs` listings of one chat: item n of a kind is
|
|
122
|
+
* `ids[n-1]` of that kind's last listing. In memory only, so a restart drops it.
|
|
123
|
+
*/
|
|
124
|
+
export class QueueListing {
|
|
125
|
+
ttlMs;
|
|
126
|
+
lists = new Map();
|
|
127
|
+
constructor(ttlMs = LISTING_TTL_MS) {
|
|
128
|
+
this.ttlMs = ttlMs;
|
|
129
|
+
}
|
|
130
|
+
set(ids, now, kind = "tasks") {
|
|
131
|
+
this.lists.set(kind, { ids: [...ids], at: now.getTime() });
|
|
132
|
+
}
|
|
133
|
+
/** The id of item `n`, or null with no valid listing of that kind (none yet, or expired). */
|
|
134
|
+
resolve(n, now, kind = "tasks") {
|
|
135
|
+
const list = this.lists.get(kind);
|
|
136
|
+
if (!list || now.getTime() - list.at > this.ttlMs)
|
|
137
|
+
return null;
|
|
138
|
+
return list.ids[n - 1];
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const PENDING = ["running", "submitted", "scheduled", "queued"];
|
|
142
|
+
function plannedMs(r) {
|
|
143
|
+
const t = Date.parse(r.scheduledFor ?? r.notBefore ?? r.enqueuedAt);
|
|
144
|
+
return Number.isFinite(t) ? t : Number.MAX_SAFE_INTEGER;
|
|
145
|
+
}
|
|
146
|
+
/** A {@link CommandContext} over a core `Scheduler` and its `TaskStore`. */
|
|
147
|
+
export function commandContextFor(scheduler, store) {
|
|
148
|
+
return {
|
|
149
|
+
getTask: (id) => store.get(id),
|
|
150
|
+
pendingTasks: () => PENDING.flatMap((status) => store.list({ status }))
|
|
151
|
+
.sort((a, b) => plannedMs(a) - plannedMs(b))
|
|
152
|
+
.slice(0, QUEUE_LIST_MAX),
|
|
153
|
+
postpone: (id, target) => scheduler.postponeTask(id, target, { actor: "telegram" }),
|
|
154
|
+
cancel: (id) => scheduler.cancelTask(id, { actor: "telegram" }),
|
|
155
|
+
runNow: (id) => scheduler.runNow(id, { actor: "telegram" }),
|
|
156
|
+
laterOptions: (record, now) => scheduler.postponeOptionsFor(record, now),
|
|
157
|
+
listJobs: () => listJobs(scheduler),
|
|
158
|
+
getJob: (id) => {
|
|
159
|
+
const job = getJob(scheduler, id);
|
|
160
|
+
return job && job.status !== "removed" ? job : undefined;
|
|
161
|
+
},
|
|
162
|
+
pauseJob: (id) => pauseJob(scheduler, id, { actor: "telegram" }),
|
|
163
|
+
resumeJob: (id) => resumeJob(scheduler, id, { actor: "telegram" }),
|
|
164
|
+
runJobNow: (id) => runJobNow(scheduler, id, { actor: "telegram" }),
|
|
165
|
+
now: () => new Date(),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
/** The refusal text of a scheduler error, without its `action: task <id>:` prefix. */
|
|
169
|
+
function refusalText(err) {
|
|
170
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
171
|
+
return msg.replace(/^[a-zA-Z]+: task \S+?:? /, "");
|
|
172
|
+
}
|
|
173
|
+
/** The id a ref names: a number resolves against the last listing of `kind`, an id passes through. */
|
|
174
|
+
function resolveId(ref, ctx, listing, kind) {
|
|
175
|
+
if (!("n" in ref))
|
|
176
|
+
return { ok: true, id: ref.id };
|
|
177
|
+
const [command, noun] = kind === "jobs" ? ["/jobs", "job"] : ["/queue", "task"];
|
|
178
|
+
const hit = listing.resolve(ref.n, ctx.now(), kind);
|
|
179
|
+
if (hit === null)
|
|
180
|
+
return { ok: false, reply: `Send ${command} first: ${noun} numbers come from the last ${command} and expire after 10 minutes.` };
|
|
181
|
+
if (hit === undefined)
|
|
182
|
+
return { ok: false, reply: `The last ${command} has no ${noun} ${ref.n}.` };
|
|
183
|
+
return { ok: true, id: hit };
|
|
184
|
+
}
|
|
185
|
+
function resolveTask(ref, ctx, listing) {
|
|
186
|
+
const resolved = resolveId(ref, ctx, listing, "tasks");
|
|
187
|
+
if (!resolved.ok)
|
|
188
|
+
return resolved;
|
|
189
|
+
const record = ctx.getTask(resolved.id);
|
|
190
|
+
return record ? { ok: true, record } : { ok: false, reply: `There is no task ${refLabel(ref)}.` };
|
|
191
|
+
}
|
|
192
|
+
function resolveJob(ref, ctx, listing) {
|
|
193
|
+
const resolved = resolveId(ref, ctx, listing, "jobs");
|
|
194
|
+
if (!resolved.ok)
|
|
195
|
+
return resolved;
|
|
196
|
+
const job = ctx.getJob(resolved.id);
|
|
197
|
+
return job ? { ok: true, job } : { ok: false, reply: `There is no job ${refLabel(ref)}.` };
|
|
198
|
+
}
|
|
199
|
+
const JOB_REFUSED = { pause: "Not paused", resume: "Not resumed", run: "Not started" };
|
|
200
|
+
/** Pause, resume or run one job. The state is checked first; a job action refusal is quoted, any other error is not. */
|
|
201
|
+
async function runJobAction(action, ref, ctx, listing) {
|
|
202
|
+
const res = resolveJob(ref, ctx, listing);
|
|
203
|
+
if (!res.ok)
|
|
204
|
+
return res.reply;
|
|
205
|
+
const { job } = res;
|
|
206
|
+
const title = jobTitle(job);
|
|
207
|
+
if (action === "pause" && job.status === "paused")
|
|
208
|
+
return `${title} is already paused.`;
|
|
209
|
+
if (action === "resume" && job.status !== "paused")
|
|
210
|
+
return `${title} is not paused.`;
|
|
211
|
+
if (action === "run" && job.status === "paused")
|
|
212
|
+
return `${title} is paused. Resume it first: /jobs resume ${refLabel(ref)}`;
|
|
213
|
+
try {
|
|
214
|
+
if (action === "pause")
|
|
215
|
+
await ctx.pauseJob(job.jobId);
|
|
216
|
+
else if (action === "resume")
|
|
217
|
+
await ctx.resumeJob(job.jobId);
|
|
218
|
+
else
|
|
219
|
+
await ctx.runJobNow(job.jobId);
|
|
220
|
+
}
|
|
221
|
+
catch (err) {
|
|
222
|
+
// Other exception text can hold paths or SQL: only the fixed job action refusals reach the chat.
|
|
223
|
+
const why = err instanceof JobActionError ? err.message : "unexpected error. Check the job on the page: tidegap ui";
|
|
224
|
+
return `${JOB_REFUSED[action]}: ${title}: ${why}`;
|
|
225
|
+
}
|
|
226
|
+
if (action === "pause")
|
|
227
|
+
return `Paused: ${title}.`;
|
|
228
|
+
if (action === "run")
|
|
229
|
+
return `Runs at the next tick: ${title}.`;
|
|
230
|
+
const after = ctx.getJob(job.jobId);
|
|
231
|
+
const next = after?.pendingTaskId ? jobNextLabel(after, ctx.getTask(after.pendingTaskId), ctx.now()) : undefined;
|
|
232
|
+
if (next === JOB_RUNNING_NOW)
|
|
233
|
+
return `Resumed: ${title}. A run is in progress.`;
|
|
234
|
+
return next ? `Resumed: ${title}. Next ${next}.` : `Resumed: ${title}.`;
|
|
235
|
+
}
|
|
236
|
+
/** "<title> is already completed." */
|
|
237
|
+
function notAllowed(record) {
|
|
238
|
+
return `${messageTitle(record)} is already ${record.status}.`;
|
|
239
|
+
}
|
|
240
|
+
/** The confirmation of a move: the title and the new earliest start, read back from the ledger. */
|
|
241
|
+
function movedText(id, ctx, now) {
|
|
242
|
+
const after = ctx.getTask(id);
|
|
243
|
+
const nb = after?.notBefore ?? after?.scheduledFor;
|
|
244
|
+
const at = nb ? localTimeLabel(new Date(nb), now) : "later";
|
|
245
|
+
return `Moved: ${after ? messageTitle(after) : id}. Not before ${at}.`;
|
|
246
|
+
}
|
|
247
|
+
/** Run a parsed command for the paired chat. `pair` and `unknown` answer with help; the caller handles pairing. */
|
|
248
|
+
export async function runCommand(cmd, ctx, listing) {
|
|
249
|
+
const now = ctx.now();
|
|
250
|
+
switch (cmd.kind) {
|
|
251
|
+
case "help":
|
|
252
|
+
case "unknown":
|
|
253
|
+
return [{ text: HELP_TEXT }];
|
|
254
|
+
case "pair":
|
|
255
|
+
return [{ text: "This chat is already paired." }];
|
|
256
|
+
case "invalid":
|
|
257
|
+
return [{ text: cmd.reply }];
|
|
258
|
+
case "queue": {
|
|
259
|
+
const msg = queueMessage(ctx.pendingTasks(), now);
|
|
260
|
+
listing.set(msg.ids, now);
|
|
261
|
+
return [{ text: msg.text, buttons: msg.buttons }];
|
|
262
|
+
}
|
|
263
|
+
case "jobs": {
|
|
264
|
+
const msg = jobsMessage(ctx.listJobs(), (id) => ctx.getTask(id), now);
|
|
265
|
+
listing.set(msg.ids, now, "jobs");
|
|
266
|
+
return [{ text: msg.text }];
|
|
267
|
+
}
|
|
268
|
+
case "job":
|
|
269
|
+
return [{ text: await runJobAction(cmd.action, cmd.ref, ctx, listing) }];
|
|
270
|
+
case "result": {
|
|
271
|
+
const res = resolveTask(cmd.ref, ctx, listing);
|
|
272
|
+
return res.ok ? resultMessages(res.record) : [{ text: res.reply }];
|
|
273
|
+
}
|
|
274
|
+
case "cancel": {
|
|
275
|
+
const res = resolveTask(cmd.ref, ctx, listing);
|
|
276
|
+
if (!res.ok)
|
|
277
|
+
return [{ text: res.reply }];
|
|
278
|
+
if (!taskActions(res.record).cancel)
|
|
279
|
+
return [{ text: notAllowed(res.record) }];
|
|
280
|
+
try {
|
|
281
|
+
ctx.cancel(res.record.taskId);
|
|
282
|
+
}
|
|
283
|
+
catch (err) {
|
|
284
|
+
return [{ text: `Not cancelled: ${refusalText(err)}` }];
|
|
285
|
+
}
|
|
286
|
+
return [{ text: `Cancelled: ${messageTitle(res.record)}.` }];
|
|
287
|
+
}
|
|
288
|
+
case "run": {
|
|
289
|
+
const res = resolveTask(cmd.ref, ctx, listing);
|
|
290
|
+
if (!res.ok)
|
|
291
|
+
return [{ text: res.reply }];
|
|
292
|
+
if (!taskActions(res.record).runNow)
|
|
293
|
+
return [{ text: `${messageTitle(res.record)} is ${res.record.status}; it cannot run now.` }];
|
|
294
|
+
try {
|
|
295
|
+
ctx.runNow(res.record.taskId);
|
|
296
|
+
}
|
|
297
|
+
catch (err) {
|
|
298
|
+
return [{ text: `Not started: ${refusalText(err)}` }];
|
|
299
|
+
}
|
|
300
|
+
return [{ text: `Runs at the next tick: ${messageTitle(res.record)}.` }];
|
|
301
|
+
}
|
|
302
|
+
case "later": {
|
|
303
|
+
const res = resolveTask(cmd.ref, ctx, listing);
|
|
304
|
+
if (!res.ok)
|
|
305
|
+
return [{ text: res.reply }];
|
|
306
|
+
if (!taskActions(res.record).later)
|
|
307
|
+
return [{ text: `${messageTitle(res.record)} is ${res.record.status}; it cannot move.` }];
|
|
308
|
+
try {
|
|
309
|
+
await ctx.postpone(res.record.taskId, laterTarget(cmd.arg, now));
|
|
310
|
+
}
|
|
311
|
+
catch (err) {
|
|
312
|
+
const why = refusalText(err);
|
|
313
|
+
if (why.includes(NEEDS_RESET_TIME)) {
|
|
314
|
+
return [{ text: `${NEEDS_RESET_TIME}: /later ${refLabel(cmd.ref)} reset HH:MM (for example /later ${refLabel(cmd.ref)} reset 14:05)` }];
|
|
315
|
+
}
|
|
316
|
+
return [{ text: `Not moved: ${messageTitle(res.record)}: ${why}` }];
|
|
317
|
+
}
|
|
318
|
+
return [{ text: movedText(res.record.taskId, ctx, now) }];
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Run a button press. A press on a task whose state no longer allows the
|
|
324
|
+
* action answers with an alert naming the state, removes the buttons and
|
|
325
|
+
* writes nothing.
|
|
326
|
+
*/
|
|
327
|
+
export async function runCallback(data, ctx) {
|
|
328
|
+
const action = parseCallback(data);
|
|
329
|
+
if (!action)
|
|
330
|
+
return { answer: { text: "This button is not valid any more." }, removeButtons: true, messages: [] };
|
|
331
|
+
const now = ctx.now();
|
|
332
|
+
const record = ctx.getTask(action.taskId);
|
|
333
|
+
if (!record)
|
|
334
|
+
return { answer: { text: "This task no longer exists.", showAlert: true }, removeButtons: true, messages: [] };
|
|
335
|
+
const allowed = taskActions(record);
|
|
336
|
+
const gone = () => ({ answer: { text: notAllowed(record), showAlert: true }, removeButtons: true, messages: [] });
|
|
337
|
+
const title = messageTitle(record);
|
|
338
|
+
switch (action.action) {
|
|
339
|
+
case "result":
|
|
340
|
+
return { answer: {}, removeButtons: false, messages: resultMessages(record) };
|
|
341
|
+
case "cancel":
|
|
342
|
+
if (!allowed.cancel)
|
|
343
|
+
return gone();
|
|
344
|
+
try {
|
|
345
|
+
ctx.cancel(record.taskId);
|
|
346
|
+
}
|
|
347
|
+
catch (err) {
|
|
348
|
+
return { answer: { text: refusalText(err), showAlert: true }, removeButtons: false, messages: [] };
|
|
349
|
+
}
|
|
350
|
+
return { answer: { text: "Cancelled" }, removeButtons: true, messages: [{ text: `Cancelled: ${title}.` }] };
|
|
351
|
+
case "run":
|
|
352
|
+
if (!allowed.runNow)
|
|
353
|
+
return gone();
|
|
354
|
+
try {
|
|
355
|
+
ctx.runNow(record.taskId);
|
|
356
|
+
}
|
|
357
|
+
catch (err) {
|
|
358
|
+
return { answer: { text: refusalText(err), showAlert: true }, removeButtons: false, messages: [] };
|
|
359
|
+
}
|
|
360
|
+
return { answer: { text: "Runs at the next tick" }, removeButtons: true, messages: [{ text: `Runs at the next tick: ${title}.` }] };
|
|
361
|
+
case "later": {
|
|
362
|
+
if (!allowed.later)
|
|
363
|
+
return gone();
|
|
364
|
+
if (!action.preset) {
|
|
365
|
+
return { answer: {}, removeButtons: false, messages: [laterMenuMessage(record, await ctx.laterOptions(record, now), now)] };
|
|
366
|
+
}
|
|
367
|
+
try {
|
|
368
|
+
await ctx.postpone(record.taskId, { preset: action.preset });
|
|
369
|
+
}
|
|
370
|
+
catch (err) {
|
|
371
|
+
return { answer: { text: refusalText(err), showAlert: true }, removeButtons: false, messages: [] };
|
|
372
|
+
}
|
|
373
|
+
return { answer: { text: "Moved" }, removeButtons: true, messages: [{ text: movedText(record.taskId, ctx, now) }] };
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
//# sourceMappingURL=commands.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every delivery code a host driver can report, besides the open families
|
|
3
|
+
* `http_<status>` and `smtp_<reply>`. The CLI's `DeliveryError` accepts only
|
|
4
|
+
* these, so a new code fails the type check until it is listed here; the
|
|
5
|
+
* docs-drift check (K13) reads this list for the codes the docs may quote.
|
|
6
|
+
*/
|
|
7
|
+
export declare const DELIVERY_CODES: readonly ["auth", "rate_limited", "redirect", "timeout", "network", "tls", "invalid_url", "scheme", "userinfo", "blocked_address", "ui_port", "not_configured", "not_ready", "invalid_config", "invalid_spec", "error", "auth_required", "auth_unsupported", "cleartext", "smtp_policy", "smtp_protocol", "smtp_recipient", "smtp_sender", "starttls_unsupported", "command_failed", "no_shortcuts", "not_executable", "not_found", "shortcut_failed", "unsafe_path", "ntfy_token_needed", "pushover_token", "pushover_user", "slack_action_prohibited", "slack_channel_archived", "slack_channel_not_found", "slack_payload", "slack_webhook_gone"];
|
|
8
|
+
export type DeliveryCode = (typeof DELIVERY_CODES)[number] | `http_${number}` | `smtp_${number}`;
|
|
9
|
+
//# sourceMappingURL=delivery-codes.d.ts.map
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every delivery code a host driver can report, besides the open families
|
|
3
|
+
* `http_<status>` and `smtp_<reply>`. The CLI's `DeliveryError` accepts only
|
|
4
|
+
* these, so a new code fails the type check until it is listed here; the
|
|
5
|
+
* docs-drift check (K13) reads this list for the codes the docs may quote.
|
|
6
|
+
*/
|
|
7
|
+
export const DELIVERY_CODES = [
|
|
8
|
+
// HTTP transport
|
|
9
|
+
"auth",
|
|
10
|
+
"rate_limited",
|
|
11
|
+
"redirect",
|
|
12
|
+
"timeout",
|
|
13
|
+
"network",
|
|
14
|
+
"tls",
|
|
15
|
+
// URL policy
|
|
16
|
+
"invalid_url",
|
|
17
|
+
"scheme",
|
|
18
|
+
"userinfo",
|
|
19
|
+
"blocked_address",
|
|
20
|
+
"ui_port",
|
|
21
|
+
// host checks before a send
|
|
22
|
+
"not_configured",
|
|
23
|
+
"not_ready",
|
|
24
|
+
"invalid_config",
|
|
25
|
+
"invalid_spec",
|
|
26
|
+
"error",
|
|
27
|
+
// SMTP
|
|
28
|
+
"auth_required",
|
|
29
|
+
"auth_unsupported",
|
|
30
|
+
"cleartext",
|
|
31
|
+
"smtp_policy",
|
|
32
|
+
"smtp_protocol",
|
|
33
|
+
"smtp_recipient",
|
|
34
|
+
"smtp_sender",
|
|
35
|
+
"starttls_unsupported",
|
|
36
|
+
// Command and Shortcuts
|
|
37
|
+
"command_failed",
|
|
38
|
+
"no_shortcuts",
|
|
39
|
+
"not_executable",
|
|
40
|
+
"not_found",
|
|
41
|
+
"shortcut_failed",
|
|
42
|
+
"unsafe_path",
|
|
43
|
+
// kind drivers that read the answer body
|
|
44
|
+
"ntfy_token_needed",
|
|
45
|
+
"pushover_token",
|
|
46
|
+
"pushover_user",
|
|
47
|
+
"slack_action_prohibited",
|
|
48
|
+
"slack_channel_archived",
|
|
49
|
+
"slack_channel_not_found",
|
|
50
|
+
"slack_payload",
|
|
51
|
+
"slack_webhook_gone",
|
|
52
|
+
];
|
|
53
|
+
//# sourceMappingURL=delivery-codes.js.map
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The daily summary (`digest`, integrations plan U15): when it is due and
|
|
3
|
+
* what it says. Pure: the host reads the ledger, calls {@link digestDue} and
|
|
4
|
+
* {@link buildDigest}, and sends the event through the normal channel path.
|
|
5
|
+
*
|
|
6
|
+
* Rules: one summary per local day at `digest_at` (default 09:00, the end of
|
|
7
|
+
* the quiet hours); a day with nothing to report sends nothing; the window
|
|
8
|
+
* starts at the previous summary and covers at most 48 hours.
|
|
9
|
+
*/
|
|
10
|
+
import { type ReadinessProblem } from "../readiness.js";
|
|
11
|
+
import type { TaskRecord } from "../types.js";
|
|
12
|
+
import type { ChannelEvent, CleanWindowEvent } from "./types.js";
|
|
13
|
+
/**
|
|
14
|
+
* Ledger meta key: ISO-8601 end of the last summary window. An empty day
|
|
15
|
+
* moves it too, so it is not proof of a send; see {@link DIGEST_SENT_META_KEY}.
|
|
16
|
+
*/
|
|
17
|
+
export declare const DIGEST_LAST_META_KEY = "notified:digest";
|
|
18
|
+
/** Ledger meta key: ISO-8601 time a summary last reached at least one channel. */
|
|
19
|
+
export declare const DIGEST_SENT_META_KEY = "digest_sent";
|
|
20
|
+
/** Ledger meta key: the local send time `HH:MM` (absent = {@link DEFAULT_DIGEST_AT}). */
|
|
21
|
+
export declare const DIGEST_AT_META_KEY = "digest_at";
|
|
22
|
+
/** The default send time: the end of the quiet hours. */
|
|
23
|
+
export declare const DEFAULT_DIGEST_AT: string;
|
|
24
|
+
/** The longest window one summary covers (a machine asleep for days sends one summary, not several). */
|
|
25
|
+
export declare const DIGEST_MAX_WINDOW_MS: number;
|
|
26
|
+
/** The window of the first summary, when no summary was sent before. */
|
|
27
|
+
export declare const DIGEST_FIRST_WINDOW_MS: number;
|
|
28
|
+
/** Titles listed per section before "and N more". */
|
|
29
|
+
export declare const DIGEST_LIST_MAX = 10;
|
|
30
|
+
/** `{h, m}` of a valid `HH:MM` (00:00–23:59), else null. */
|
|
31
|
+
export declare function parseDigestAt(value: unknown): {
|
|
32
|
+
h: number;
|
|
33
|
+
m: number;
|
|
34
|
+
} | null;
|
|
35
|
+
/** The stored send time, or the default when it is absent or invalid. */
|
|
36
|
+
export declare function digestAt(stored: string | undefined): string;
|
|
37
|
+
/**
|
|
38
|
+
* Whether a summary is due at `now` and the window it covers. Due when the
|
|
39
|
+
* local time is at or after today's send time and no summary went out
|
|
40
|
+
* today (local midnight on), so a send time moved later the same day does
|
|
41
|
+
* not send a second one. A spring-forward gap moves the send time to the
|
|
42
|
+
* first valid local minute after it (the `Date` constructor's rule).
|
|
43
|
+
*/
|
|
44
|
+
export declare function digestDue(now: Date, lastIso: string | undefined, at?: string): {
|
|
45
|
+
due: boolean;
|
|
46
|
+
since: Date;
|
|
47
|
+
};
|
|
48
|
+
export interface DigestInput {
|
|
49
|
+
since: Date;
|
|
50
|
+
now: Date;
|
|
51
|
+
/** Tasks that completed in the window. */
|
|
52
|
+
done: readonly TaskRecord<unknown>[];
|
|
53
|
+
/** Tasks that failed in the window. */
|
|
54
|
+
failed: readonly TaskRecord<unknown>[];
|
|
55
|
+
/** Tasks postponed in the window (once each). */
|
|
56
|
+
postponed: readonly TaskRecord<unknown>[];
|
|
57
|
+
/** Readiness problems still open at send time. */
|
|
58
|
+
problems: readonly ReadinessProblem[];
|
|
59
|
+
/** Tasks still waiting to run (queued or scheduled). */
|
|
60
|
+
queued: number;
|
|
61
|
+
/** The next clean window, when the host knows it. */
|
|
62
|
+
window?: CleanWindowEvent | null;
|
|
63
|
+
/**
|
|
64
|
+
* One line per job with runs in the window (jobs KTD11). Their occurrences
|
|
65
|
+
* are not in `done` or `failed`: the host splits them out.
|
|
66
|
+
*/
|
|
67
|
+
jobs?: readonly JobDigestLine[];
|
|
68
|
+
}
|
|
69
|
+
/** One job in the daily summary; the host builds it from the job and its finished runs in the window. */
|
|
70
|
+
export interface JobDigestLine {
|
|
71
|
+
title: string;
|
|
72
|
+
/** Finished runs in the window. */
|
|
73
|
+
runs: number;
|
|
74
|
+
/** Failed runs among them. */
|
|
75
|
+
failed: number;
|
|
76
|
+
/** The newest finished run: its outcome, its finish time as a label, whether it finished after its deadline, a one-line preview. */
|
|
77
|
+
last: {
|
|
78
|
+
outcome: "ok" | "failed";
|
|
79
|
+
at: string;
|
|
80
|
+
late: boolean;
|
|
81
|
+
preview?: string;
|
|
82
|
+
};
|
|
83
|
+
/** The next run as a label (`Fri 02:00`, `~Thu 22:00`), when the job has one. */
|
|
84
|
+
next?: string;
|
|
85
|
+
/** Carbon of the ok runs, grams CO2e. */
|
|
86
|
+
grams?: number;
|
|
87
|
+
/** The runs this line reports; their alerts fold into the summary only when the line is printed. */
|
|
88
|
+
coveredTaskIds: readonly string[];
|
|
89
|
+
}
|
|
90
|
+
/** `Mail triage: ok 03:12, "3 messages need a reply..." - next Fri 02:00` (KTD11). */
|
|
91
|
+
export declare function jobDigestText(job: JobDigestLine): string;
|
|
92
|
+
/** The digest event, with what it covers so per-task alerts can fold into it. */
|
|
93
|
+
export type DigestEvent = Extract<ChannelEvent, {
|
|
94
|
+
kind: "digest";
|
|
95
|
+
}>;
|
|
96
|
+
/**
|
|
97
|
+
* The summary event, or null for an empty day: nothing finished, failed or
|
|
98
|
+
* postponed in the window and no open problem. Queued work alone does not
|
|
99
|
+
* make a summary.
|
|
100
|
+
*/
|
|
101
|
+
export declare function buildDigest(input: DigestInput): DigestEvent | null;
|
|
102
|
+
//# sourceMappingURL=digest.d.ts.map
|