@tidegap/core 0.21.0 → 0.23.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/dist/channels/digest.d.ts +4 -1
- package/dist/channels/digest.js +7 -3
- package/dist/channels/format.d.ts +61 -19
- package/dist/channels/format.js +181 -51
- package/dist/channels/index.d.ts +1 -0
- package/dist/channels/index.js +1 -0
- package/dist/channels/kinds/discord.js +2 -2
- package/dist/channels/kinds/email.js +2 -1
- package/dist/channels/kinds/ntfy.js +9 -1
- package/dist/channels/kinds/pushover.js +4 -3
- package/dist/channels/kinds/shared.d.ts +4 -1
- package/dist/channels/kinds/shared.js +6 -1
- package/dist/channels/kinds/slack.js +2 -2
- package/dist/channels/notice.d.ts +42 -0
- package/dist/channels/notice.js +142 -0
- package/dist/channels/payload.d.ts +3 -0
- package/dist/channels/payload.js +19 -6
- package/dist/channels/plan.js +3 -2
- package/dist/channels/telegram.d.ts +18 -0
- package/dist/channels/telegram.js +23 -9
- package/dist/channels/types.d.ts +17 -1
- package/dist/grid.d.ts +10 -2
- package/dist/grid.js +14 -5
- package/dist/handoff.d.ts +66 -3
- package/dist/handoff.js +108 -3
- package/dist/history.d.ts +105 -0
- package/dist/history.js +108 -1
- package/dist/index.d.ts +15 -11
- package/dist/index.js +10 -6
- package/dist/job-cadence.d.ts +10 -0
- package/dist/job-cadence.js +34 -0
- package/dist/job-wake.d.ts +55 -0
- package/dist/job-wake.js +91 -0
- package/dist/jobs.d.ts +54 -2
- package/dist/jobs.js +258 -14
- package/dist/notice-language.d.ts +37 -0
- package/dist/notice-language.js +67 -0
- package/dist/providers/base.d.ts +14 -0
- package/dist/schedule-job.d.ts +18 -2
- package/dist/schedule-job.js +22 -2
- package/dist/scheduler.d.ts +2 -0
- package/dist/scheduler.js +5 -1
- package/dist/storage/sqlite.d.ts +24 -3
- package/dist/storage/sqlite.js +135 -28
- package/dist/task-events.d.ts +1 -1
- package/dist/task-events.js +9 -0
- package/dist/tool-surface.js +25 -1
- package/dist/types.d.ts +41 -1
- package/package.json +2 -2
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
/** Helpers the chat kinds share: the loopback URL the test sinks use, escaping of untrusted text (KTD2). */
|
|
1
|
+
/** Helpers the chat kinds share: the loopback URL the test sinks use, escaping of untrusted text (KTD2), the status title. */
|
|
2
|
+
import { NOTICE_EMOJI } from "../notice.js";
|
|
3
|
+
/** The payload title with the notice status emoji before it, for a job's task events; the title as is otherwise. */
|
|
4
|
+
export function statusTitle(payload) {
|
|
5
|
+
return payload.status ? `${NOTICE_EMOJI[payload.status]} ${payload.title}` : payload.title;
|
|
6
|
+
}
|
|
2
7
|
/**
|
|
3
8
|
* Pattern source of a loopback http(s) URL. Kinds with a fixed official host
|
|
4
9
|
* (Slack, Discord) also accept it, so the fake sinks of the tests and the QA
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/** Slack (R12): an Incoming Webhook of a Slack app. `text` plus one mrkdwn section block; `&<>` escaped (KTD2). */
|
|
2
|
-
import { clipEscaped, escapeAngle, LOOPBACK_URL } from "./shared.js";
|
|
2
|
+
import { clipEscaped, escapeAngle, LOOPBACK_URL, statusTitle } from "./shared.js";
|
|
3
3
|
/** Slack's cap on a section block's text. */
|
|
4
4
|
export const SLACK_SECTION_MAX = 3000;
|
|
5
5
|
export function slackBody(payload) {
|
|
6
|
-
const title = escapeAngle(payload
|
|
6
|
+
const title = escapeAngle(statusTitle(payload));
|
|
7
7
|
const text = escapeAngle(payload.text);
|
|
8
8
|
return JSON.stringify({
|
|
9
9
|
text,
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The status of a job notice and the shape of its summary. The status
|
|
3
|
+
* emoji comes from one fixed map, never from model output: a summary's
|
|
4
|
+
* status is a word from a closed set, and pictographs a model writes at the
|
|
5
|
+
* start of a line are removed. `structureSummary` takes whatever the model
|
|
6
|
+
* returned (a paragraph, markdown, a numbered list, the requested shape) and
|
|
7
|
+
* gives one shape: a verdict and at most four short lines. Pure: the
|
|
8
|
+
* run-level helpers (`runSummary`, `noticeStatus`) live in `format.ts`.
|
|
9
|
+
*/
|
|
10
|
+
/** The status a job notice shows: judged fine, judged to need a look, completed without a judgment, failed, or ended. */
|
|
11
|
+
export type NoticeStatus = "ok" | "attention" | "done" | "failed" | "ended";
|
|
12
|
+
/** The emoji line 1 of a job notice starts with, per status. */
|
|
13
|
+
export declare const NOTICE_EMOJI: Readonly<Record<NoticeStatus, string>>;
|
|
14
|
+
/** Most characters (code points) of a summary's verdict. */
|
|
15
|
+
export declare const NOTICE_VERDICT_CHARS = 120;
|
|
16
|
+
/** Most lines below the verdict. */
|
|
17
|
+
export declare const NOTICE_BULLETS_MAX = 4;
|
|
18
|
+
/** Most characters (code points) of one line below the verdict. */
|
|
19
|
+
export declare const NOTICE_BULLET_CHARS = 200;
|
|
20
|
+
/** A summary in the notice shape. `status` only when the reply led with a status word. */
|
|
21
|
+
export interface StructuredSummary {
|
|
22
|
+
status?: "ok" | "attention";
|
|
23
|
+
/** "" when the reply had no usable text: the notice then shows the raw output. */
|
|
24
|
+
verdict: string;
|
|
25
|
+
bullets: string[];
|
|
26
|
+
}
|
|
27
|
+
/** `text` cut to `maxChars - 1` code points plus "…" when longer, so it never exceeds `maxChars` (KTD6). */
|
|
28
|
+
export declare function cutPreview(text: string, maxChars: number): string;
|
|
29
|
+
/** `text` without bidirectional controls, for a line shown beside the status emoji. */
|
|
30
|
+
export declare function stripBidiControls(text: string): string;
|
|
31
|
+
/**
|
|
32
|
+
* A summary in the notice shape. Never fails and never trusts the shape:
|
|
33
|
+
* the first line with text is the verdict, a leading `OK` or `ATTENTION`
|
|
34
|
+
* is its status word, later marked lines are bullets (an unmarked line
|
|
35
|
+
* joins the bullet before it), and a paragraph without marks gives one
|
|
36
|
+
* bullet per sentence. The verdict is cut at {@link NOTICE_VERDICT_CHARS}
|
|
37
|
+
* and the rest of its line becomes the first bullet; bullets are cut at
|
|
38
|
+
* {@link NOTICE_BULLET_CHARS}, and past {@link NOTICE_BULLETS_MAX} the last
|
|
39
|
+
* line kept is `…`.
|
|
40
|
+
*/
|
|
41
|
+
export declare function structureSummary(text: string): StructuredSummary;
|
|
42
|
+
//# sourceMappingURL=notice.d.ts.map
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The status of a job notice and the shape of its summary. The status
|
|
3
|
+
* emoji comes from one fixed map, never from model output: a summary's
|
|
4
|
+
* status is a word from a closed set, and pictographs a model writes at the
|
|
5
|
+
* start of a line are removed. `structureSummary` takes whatever the model
|
|
6
|
+
* returned (a paragraph, markdown, a numbered list, the requested shape) and
|
|
7
|
+
* gives one shape: a verdict and at most four short lines. Pure: the
|
|
8
|
+
* run-level helpers (`runSummary`, `noticeStatus`) live in `format.ts`.
|
|
9
|
+
*/
|
|
10
|
+
/** The emoji line 1 of a job notice starts with, per status. */
|
|
11
|
+
export const NOTICE_EMOJI = {
|
|
12
|
+
ok: "✅",
|
|
13
|
+
attention: "⚠️",
|
|
14
|
+
done: "☑️",
|
|
15
|
+
failed: "❌",
|
|
16
|
+
ended: "🏁",
|
|
17
|
+
};
|
|
18
|
+
/** Most characters (code points) of a summary's verdict. */
|
|
19
|
+
export const NOTICE_VERDICT_CHARS = 120;
|
|
20
|
+
/** Most lines below the verdict. */
|
|
21
|
+
export const NOTICE_BULLETS_MAX = 4;
|
|
22
|
+
/** Most characters (code points) of one line below the verdict. */
|
|
23
|
+
export const NOTICE_BULLET_CHARS = 200;
|
|
24
|
+
/** `text` cut to `maxChars - 1` code points plus "…" when longer, so it never exceeds `maxChars` (KTD6). */
|
|
25
|
+
export function cutPreview(text, maxChars) {
|
|
26
|
+
const chars = Array.from(text);
|
|
27
|
+
return chars.length > maxChars ? `${chars.slice(0, maxChars - 1).join("")}…` : text;
|
|
28
|
+
}
|
|
29
|
+
/** Most characters (code points) of a reply `structureSummary` reads, and of one line of it: the caps need far less, and the regexes stay linear in practice. */
|
|
30
|
+
const SUMMARY_INPUT_CHARS = 8000;
|
|
31
|
+
const SUMMARY_LINE_CHARS = 2000;
|
|
32
|
+
/** Bidirectional controls: they can reorder the rest of a line on screen. */
|
|
33
|
+
const BIDI_CONTROLS = /[\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/g;
|
|
34
|
+
/** Whitespace that is a control character: a space, so words stay apart. */
|
|
35
|
+
const CONTROL_SPACE = /[\t\v\f\r\u0085]/g;
|
|
36
|
+
/** Control and format characters, and the blank fillers (Hangul, braille) that look like nothing. */
|
|
37
|
+
const INVISIBLE = /[\p{Cc}\p{Cf}\u115F\u1160\u2800\u3164\uFFA0]/gu;
|
|
38
|
+
const LEADING_PICTOGRAPHS = /^(?:[\p{Extended_Pictographic}\p{Emoji_Modifier}\p{Regional_Indicator}⃣︎️]|\s)+/u;
|
|
39
|
+
const FENCE = /^\s*(```|~~~)/;
|
|
40
|
+
const RULE = /^\s*([-*_]\s*){3,}$/;
|
|
41
|
+
const TABLE_RULE = /^\s*\|?\s*:?-{2,}:?\s*(\|\s*:?-{2,}:?\s*)*\|?\s*$/;
|
|
42
|
+
const HEADING = /^\s*#{1,6}(\s+|$)/;
|
|
43
|
+
const LIST_MARK = /^\s*(?:[-*•–]|\d{1,3}[.)])(?:\s+|$)/;
|
|
44
|
+
// A hyphen or a period after the word needs a space after it: `OK-ish` and `Attention-grabbing` are words, not a status.
|
|
45
|
+
const STATUS_WORD = /^\[?(ok|attention)\]?(?:\s*[:—–]|\s*[-.](?=\s|$)|\s*$)\s*/i;
|
|
46
|
+
const SENTENCE_END = /(?<=[.!?…])\s+|(?<=[。!?])/;
|
|
47
|
+
/** `text` without bidirectional controls, for a line shown beside the status emoji. */
|
|
48
|
+
export function stripBidiControls(text) {
|
|
49
|
+
return text.replace(BIDI_CONTROLS, "");
|
|
50
|
+
}
|
|
51
|
+
/** The first `max` code points of `text`, without an ellipsis. */
|
|
52
|
+
function headChars(text, max) {
|
|
53
|
+
return text.length <= max ? text : Array.from(text.slice(0, max * 2)).slice(0, max).join("");
|
|
54
|
+
}
|
|
55
|
+
/** One line without control and format characters, markdown emphasis, quote marks or leading pictographs; links as `text (url)`. */
|
|
56
|
+
function cleanLine(line) {
|
|
57
|
+
return line
|
|
58
|
+
.replace(CONTROL_SPACE, " ")
|
|
59
|
+
.replace(INVISIBLE, "")
|
|
60
|
+
.replace(/\*\*|__|`/g, "")
|
|
61
|
+
.replace(/^\s*(>\s*)+/, "")
|
|
62
|
+
.replace(/\[([^\]\n]+)\]\(([^)\s]+)\)/g, "$1 ($2)")
|
|
63
|
+
.replace(LEADING_PICTOGRAPHS, "")
|
|
64
|
+
.replace(/\s+/g, " ")
|
|
65
|
+
.trim();
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* A summary in the notice shape. Never fails and never trusts the shape:
|
|
69
|
+
* the first line with text is the verdict, a leading `OK` or `ATTENTION`
|
|
70
|
+
* is its status word, later marked lines are bullets (an unmarked line
|
|
71
|
+
* joins the bullet before it), and a paragraph without marks gives one
|
|
72
|
+
* bullet per sentence. The verdict is cut at {@link NOTICE_VERDICT_CHARS}
|
|
73
|
+
* and the rest of its line becomes the first bullet; bullets are cut at
|
|
74
|
+
* {@link NOTICE_BULLET_CHARS}, and past {@link NOTICE_BULLETS_MAX} the last
|
|
75
|
+
* line kept is `…`.
|
|
76
|
+
*/
|
|
77
|
+
export function structureSummary(text) {
|
|
78
|
+
let status;
|
|
79
|
+
let verdictLine;
|
|
80
|
+
let statusSeen = false;
|
|
81
|
+
const parts = [];
|
|
82
|
+
let open;
|
|
83
|
+
for (const full of headChars(text, SUMMARY_INPUT_CHARS).split(/\r?\n/)) {
|
|
84
|
+
const raw = headChars(full, SUMMARY_LINE_CHARS);
|
|
85
|
+
if (FENCE.test(raw) || TABLE_RULE.test(raw) || RULE.test(raw))
|
|
86
|
+
continue;
|
|
87
|
+
const heading = HEADING.test(raw);
|
|
88
|
+
if (heading && verdictLine !== undefined)
|
|
89
|
+
continue;
|
|
90
|
+
let line = cleanLine(raw.replace(HEADING, ""));
|
|
91
|
+
if (!line) {
|
|
92
|
+
open = undefined;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const marked = LIST_MARK.test(line);
|
|
96
|
+
if (marked)
|
|
97
|
+
line = cleanLine(line.replace(LIST_MARK, ""));
|
|
98
|
+
if (!line)
|
|
99
|
+
continue;
|
|
100
|
+
if (verdictLine === undefined) {
|
|
101
|
+
const word = statusSeen ? null : STATUS_WORD.exec(line);
|
|
102
|
+
if (word) {
|
|
103
|
+
statusSeen = true;
|
|
104
|
+
status = word[1].toLowerCase();
|
|
105
|
+
line = cleanLine(line.slice(word[0].length));
|
|
106
|
+
if (!line)
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
verdictLine = line;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (marked || !open) {
|
|
113
|
+
open = { text: line, marked };
|
|
114
|
+
parts.push(open);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
open.text = `${open.text} ${line}`;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (verdictLine === undefined)
|
|
121
|
+
return { verdict: "", bullets: [] };
|
|
122
|
+
const verdictChars = Array.from(verdictLine);
|
|
123
|
+
const verdict = cutPreview(verdictLine, NOTICE_VERDICT_CHARS);
|
|
124
|
+
const lines = [];
|
|
125
|
+
if (verdictChars.length > NOTICE_VERDICT_CHARS)
|
|
126
|
+
lines.push(verdictChars.slice(NOTICE_VERDICT_CHARS - 1).join("").trim());
|
|
127
|
+
for (const part of parts) {
|
|
128
|
+
if (lines.length > NOTICE_BULLETS_MAX)
|
|
129
|
+
break;
|
|
130
|
+
if (part.marked)
|
|
131
|
+
lines.push(part.text);
|
|
132
|
+
else
|
|
133
|
+
for (const sentence of part.text.split(SENTENCE_END))
|
|
134
|
+
if (sentence.trim())
|
|
135
|
+
lines.push(sentence.trim());
|
|
136
|
+
}
|
|
137
|
+
const bullets = lines.map((l) => cutPreview(l, NOTICE_BULLET_CHARS));
|
|
138
|
+
if (bullets.length > NOTICE_BULLETS_MAX)
|
|
139
|
+
bullets.splice(NOTICE_BULLETS_MAX - 1, Infinity, "…");
|
|
140
|
+
return { ...(status ? { status } : {}), verdict, bullets };
|
|
141
|
+
}
|
|
142
|
+
//# sourceMappingURL=notice.js.map
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* plain text: a `format` for a receiver that renders markup escapes them.
|
|
6
6
|
*/
|
|
7
7
|
import type { TaskStatus } from "../types.js";
|
|
8
|
+
import type { NoticeStatus } from "./notice.js";
|
|
8
9
|
import type { ChannelEvent, EventKind } from "./types.js";
|
|
9
10
|
export type NotificationLevel = "info" | "success" | "warning" | "error";
|
|
10
11
|
/** A payload's event: an {@link EventKind}, or `test` for the message a Connect or Send test sends. */
|
|
@@ -24,6 +25,8 @@ export interface NotificationPayload {
|
|
|
24
25
|
status: TaskStatus;
|
|
25
26
|
grams?: number;
|
|
26
27
|
};
|
|
28
|
+
/** On a job's task events only: the notice status; a kind shows its emoji beside the title. */
|
|
29
|
+
status?: NoticeStatus;
|
|
27
30
|
/** On `not_ready` only: the tick daemon runs again after a sent alert ("running again"). */
|
|
28
31
|
recovered?: true;
|
|
29
32
|
}
|
package/dist/channels/payload.js
CHANGED
|
@@ -4,7 +4,14 @@
|
|
|
4
4
|
* redaction and the 600-character preview. Title and text are untrusted
|
|
5
5
|
* plain text: a `format` for a receiver that renders markup escapes them.
|
|
6
6
|
*/
|
|
7
|
-
import { eventMessage, eventTitle, jobPausedTitle } from "./format.js";
|
|
7
|
+
import { eventMessage, eventTitle, jobEndedTitle, jobPausedTitle, noticeStatus } from "./format.js";
|
|
8
|
+
/** The word a job run's payload title ends with, per status. */
|
|
9
|
+
const STATUS_WORD = {
|
|
10
|
+
ok: "ok",
|
|
11
|
+
attention: "needs a look",
|
|
12
|
+
done: "done",
|
|
13
|
+
failed: "failed",
|
|
14
|
+
};
|
|
8
15
|
const LEVEL = {
|
|
9
16
|
task_done: "success",
|
|
10
17
|
task_failed: "error",
|
|
@@ -38,14 +45,20 @@ export function payloadFor(event, now = new Date()) {
|
|
|
38
45
|
const title = eventTitle(record, event.jobTitle);
|
|
39
46
|
const grams = record.receipt?.actualCarbonGCo2 ?? record.receipt?.estimatedCarbonGCo2;
|
|
40
47
|
const done = event.kind === "task_done";
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
48
|
+
const paused = event.kind === "task_failed" ? event.jobPaused : undefined;
|
|
49
|
+
const job = event.jobEnded !== undefined || paused !== undefined || event.jobTitle !== undefined;
|
|
50
|
+
const status = event.jobEnded ? "ended" : paused ? "failed" : job ? noticeStatus(record) : undefined;
|
|
51
|
+
const head = event.jobEnded
|
|
52
|
+
? jobEndedTitle(event.jobEnded)
|
|
53
|
+
: paused
|
|
54
|
+
? jobPausedTitle(paused)
|
|
55
|
+
: status && status !== "ended"
|
|
56
|
+
? `${title}: ${STATUS_WORD[status]}`
|
|
57
|
+
: `${done ? "Done" : "Failed"}: ${title}`;
|
|
46
58
|
return {
|
|
47
59
|
...base,
|
|
48
60
|
title: head,
|
|
61
|
+
...(status ? { status } : {}),
|
|
49
62
|
at: validIso(record.completedAt) ?? now.toISOString(),
|
|
50
63
|
task: { id: record.taskId, title, status: record.status, ...(typeof grams === "number" && Number.isFinite(grams) ? { grams } : {}) },
|
|
51
64
|
};
|
package/dist/channels/plan.js
CHANGED
|
@@ -29,10 +29,11 @@ export function planNotifications(events, channels, now = new Date()) {
|
|
|
29
29
|
function coveredBy(event, covers) {
|
|
30
30
|
switch (event.kind) {
|
|
31
31
|
case "task_done":
|
|
32
|
-
|
|
32
|
+
// A job's final notice is sent on its own, even when the summary lists its last run.
|
|
33
|
+
return !event.jobEnded && covers.taskIds.includes(event.task.taskId);
|
|
33
34
|
case "task_failed":
|
|
34
35
|
// An auto-pause notice is sent on its own, even when the summary lists the failed run.
|
|
35
|
-
return !event.jobPaused && covers.taskIds.includes(event.task.taskId);
|
|
36
|
+
return !event.jobPaused && !event.jobEnded && covers.taskIds.includes(event.task.taskId);
|
|
36
37
|
case "not_ready":
|
|
37
38
|
// The summary does not say the daemon runs again, so a recovery is never folded.
|
|
38
39
|
return covers.notReady && !event.recovered;
|
|
@@ -13,6 +13,8 @@ export declare const TELEGRAM_API_BASE = "https://api.telegram.org";
|
|
|
13
13
|
export declare const TELEGRAM_MESSAGE_MAX = 4096;
|
|
14
14
|
/** Longest wait for one Bot API call beyond its long-poll `timeout`, in ms. */
|
|
15
15
|
export declare const TELEGRAM_CALL_TIMEOUT_MS = 30000;
|
|
16
|
+
/** Longest one file upload may take: a 10 MB file on a slow link needs more than a call. */
|
|
17
|
+
export declare const TELEGRAM_UPLOAD_TIMEOUT_MS = 120000;
|
|
16
18
|
/** Telegram's cap on a button's `callback_data`, in bytes. */
|
|
17
19
|
export declare const TELEGRAM_CALLBACK_MAX_BYTES = 64;
|
|
18
20
|
export interface TelegramChat {
|
|
@@ -65,6 +67,14 @@ export declare class TelegramError extends Error {
|
|
|
65
67
|
/** Seconds Telegram asked to wait (429). */
|
|
66
68
|
retryAfterS?: number | undefined);
|
|
67
69
|
}
|
|
70
|
+
/** The Bot API methods that upload a file. */
|
|
71
|
+
export type TelegramFileMethod = "sendPhoto" | "sendDocument";
|
|
72
|
+
/** A file to upload: its bytes, its type, and the file name Telegram shows. */
|
|
73
|
+
export interface TelegramUpload {
|
|
74
|
+
bytes: Uint8Array;
|
|
75
|
+
type: string;
|
|
76
|
+
filename: string;
|
|
77
|
+
}
|
|
68
78
|
export interface TelegramClientOptions {
|
|
69
79
|
token: string;
|
|
70
80
|
/** Default {@link TELEGRAM_API_BASE}. Hosts allow an override only on loopback ({@link isLoopbackApiBase}). */
|
|
@@ -86,6 +96,8 @@ export declare class TelegramClient {
|
|
|
86
96
|
* the tick or the listener.
|
|
87
97
|
*/
|
|
88
98
|
call<T>(method: string, body?: Record<string, unknown>, signal?: AbortSignal): Promise<T>;
|
|
99
|
+
/** One POST to a Bot API method, with the timeout, abort and error mapping every call shares. */
|
|
100
|
+
private post;
|
|
89
101
|
getMe(): Promise<TelegramUser>;
|
|
90
102
|
getWebhookInfo(): Promise<{
|
|
91
103
|
url: string;
|
|
@@ -100,6 +112,12 @@ export declare class TelegramClient {
|
|
|
100
112
|
sendMessage(chatId: number | string, text: string, opts?: {
|
|
101
113
|
buttons?: InlineKeyboard;
|
|
102
114
|
}): Promise<TelegramMessage>;
|
|
115
|
+
/**
|
|
116
|
+
* Upload one file as a photo (`sendPhoto`) or a document (`sendDocument`),
|
|
117
|
+
* multipart, with a plain caption (no `parse_mode`). The same host and
|
|
118
|
+
* errors as {@link call}; the time limit is {@link TELEGRAM_UPLOAD_TIMEOUT_MS}.
|
|
119
|
+
*/
|
|
120
|
+
sendFile(method: TelegramFileMethod, chatId: number | string, file: TelegramUpload, caption: string): Promise<TelegramMessage>;
|
|
103
121
|
answerCallbackQuery(id: string, opts?: {
|
|
104
122
|
text?: string;
|
|
105
123
|
showAlert?: boolean;
|
|
@@ -13,6 +13,8 @@ export const TELEGRAM_API_BASE = "https://api.telegram.org";
|
|
|
13
13
|
export const TELEGRAM_MESSAGE_MAX = 4096;
|
|
14
14
|
/** Longest wait for one Bot API call beyond its long-poll `timeout`, in ms. */
|
|
15
15
|
export const TELEGRAM_CALL_TIMEOUT_MS = 30_000;
|
|
16
|
+
/** Longest one file upload may take: a 10 MB file on a slow link needs more than a call. */
|
|
17
|
+
export const TELEGRAM_UPLOAD_TIMEOUT_MS = 120_000;
|
|
16
18
|
/** Telegram's cap on a button's `callback_data`, in bytes. */
|
|
17
19
|
export const TELEGRAM_CALLBACK_MAX_BYTES = 64;
|
|
18
20
|
export class TelegramError extends Error {
|
|
@@ -65,15 +67,19 @@ export class TelegramClient {
|
|
|
65
67
|
* {@link TELEGRAM_CALL_TIMEOUT_MS}, so a stalled connection cannot hold
|
|
66
68
|
* the tick or the listener.
|
|
67
69
|
*/
|
|
68
|
-
|
|
69
|
-
const f = this.fetchFn ?? globalThis.fetch;
|
|
70
|
+
call(method, body = {}, signal) {
|
|
70
71
|
const pollS = typeof body.timeout === "number" ? body.timeout : 0;
|
|
72
|
+
return this.post(method, { headers: { "content-type": "application/json" }, body: JSON.stringify(body) }, pollS * 1000 + TELEGRAM_CALL_TIMEOUT_MS, signal);
|
|
73
|
+
}
|
|
74
|
+
/** One POST to a Bot API method, with the timeout, abort and error mapping every call shares. */
|
|
75
|
+
async post(method, init, timeoutMs, signal) {
|
|
76
|
+
const f = this.fetchFn ?? globalThis.fetch;
|
|
71
77
|
const controller = new AbortController();
|
|
72
78
|
let timedOut = false;
|
|
73
79
|
const timer = setTimeout(() => {
|
|
74
80
|
timedOut = true;
|
|
75
81
|
controller.abort();
|
|
76
|
-
},
|
|
82
|
+
}, timeoutMs);
|
|
77
83
|
const forward = () => controller.abort();
|
|
78
84
|
if (signal?.aborted)
|
|
79
85
|
controller.abort();
|
|
@@ -83,12 +89,7 @@ export class TelegramClient {
|
|
|
83
89
|
let env = {};
|
|
84
90
|
try {
|
|
85
91
|
try {
|
|
86
|
-
res = await f(`${this.base}/bot${this.token}/${method}`, {
|
|
87
|
-
method: "POST",
|
|
88
|
-
headers: { "content-type": "application/json" },
|
|
89
|
-
body: JSON.stringify(body),
|
|
90
|
-
signal: controller.signal,
|
|
91
|
-
});
|
|
92
|
+
res = await f(`${this.base}/bot${this.token}/${method}`, { method: "POST", ...init, signal: controller.signal });
|
|
92
93
|
}
|
|
93
94
|
catch (err) {
|
|
94
95
|
const code = err?.cause?.code;
|
|
@@ -145,6 +146,19 @@ export class TelegramClient {
|
|
|
145
146
|
body.reply_markup = keyboardMarkup(opts.buttons);
|
|
146
147
|
return this.call("sendMessage", body);
|
|
147
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* Upload one file as a photo (`sendPhoto`) or a document (`sendDocument`),
|
|
151
|
+
* multipart, with a plain caption (no `parse_mode`). The same host and
|
|
152
|
+
* errors as {@link call}; the time limit is {@link TELEGRAM_UPLOAD_TIMEOUT_MS}.
|
|
153
|
+
*/
|
|
154
|
+
sendFile(method, chatId, file, caption) {
|
|
155
|
+
const form = new FormData();
|
|
156
|
+
form.append("chat_id", String(chatId));
|
|
157
|
+
form.append("caption", caption);
|
|
158
|
+
const field = method === "sendPhoto" ? "photo" : "document";
|
|
159
|
+
form.append(field, new Blob([file.bytes], { type: file.type }), file.filename);
|
|
160
|
+
return this.post(method, { body: form }, TELEGRAM_UPLOAD_TIMEOUT_MS);
|
|
161
|
+
}
|
|
148
162
|
answerCallbackQuery(id, opts = {}) {
|
|
149
163
|
const body = { callback_query_id: id };
|
|
150
164
|
if (opts.text)
|
package/dist/channels/types.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* every side effect (spawning the toast, calling the Bot API, sending HTTP).
|
|
7
7
|
*/
|
|
8
8
|
import type { ReadinessProblem } from "../readiness.js";
|
|
9
|
-
import type { TaskRecord } from "../types.js";
|
|
9
|
+
import type { JobEndReason, TaskRecord } from "../types.js";
|
|
10
10
|
/** Every kind a channel row can have: `os` plus the registry kinds (`kinds/index.ts` lists one descriptor per registry kind). */
|
|
11
11
|
export declare const CHANNEL_KINDS: readonly ["os", "telegram", "webhook", "slack", "discord", "ntfy", "pushover", "email", "shortcuts", "command"];
|
|
12
12
|
export type ChannelKind = (typeof CHANNEL_KINDS)[number];
|
|
@@ -52,6 +52,17 @@ export interface JobPausedNotice {
|
|
|
52
52
|
title: string;
|
|
53
53
|
reason: string;
|
|
54
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* What a job's final notice names: the job, why it ended, its end date for
|
|
57
|
+
* `until`, and the outcome of its last run (`task` of the event).
|
|
58
|
+
*/
|
|
59
|
+
export interface JobEndedNotice {
|
|
60
|
+
jobId: string;
|
|
61
|
+
title: string;
|
|
62
|
+
reason: JobEndReason;
|
|
63
|
+
until?: string;
|
|
64
|
+
lastOutcome: "ok" | "failed";
|
|
65
|
+
}
|
|
55
66
|
/** A task event a channel can receive. A `digest` carries its finished text; the host builds it from the ledger. */
|
|
56
67
|
export type ChannelEvent =
|
|
57
68
|
/** `jobTitle`: the task is an occurrence of this job; messages lead with the job's current title. */
|
|
@@ -59,6 +70,10 @@ export type ChannelEvent =
|
|
|
59
70
|
kind: "task_done";
|
|
60
71
|
task: TaskRecord<unknown>;
|
|
61
72
|
jobTitle?: string;
|
|
73
|
+
/** The job's next run as a label (`Sun 10:05`, `~Sun 10:05`), set when the event was built. */
|
|
74
|
+
jobNext?: string;
|
|
75
|
+
/** The job ended after this run; the message is its final notice. */
|
|
76
|
+
jobEnded?: JobEndedNotice;
|
|
62
77
|
}
|
|
63
78
|
/**
|
|
64
79
|
* `jobPaused`: the job paused itself after its third failed slot (jobs
|
|
@@ -69,6 +84,7 @@ export type ChannelEvent =
|
|
|
69
84
|
task: TaskRecord<unknown>;
|
|
70
85
|
jobTitle?: string;
|
|
71
86
|
jobPaused?: JobPausedNotice;
|
|
87
|
+
jobEnded?: JobEndedNotice;
|
|
72
88
|
} | {
|
|
73
89
|
kind: "clean_window";
|
|
74
90
|
window: CleanWindowEvent;
|
package/dist/grid.d.ts
CHANGED
|
@@ -75,6 +75,8 @@ export declare function electricityMapsFeed(apiKey?: string): GridFeed;
|
|
|
75
75
|
* Docs: https://carbon-intensity.github.io/api-definitions/#carbon-intensity
|
|
76
76
|
*/
|
|
77
77
|
export declare function ukCarbonIntensityFeed(): GridFeed;
|
|
78
|
+
/** The line a keyless EIA feed prints, at most once per process. */
|
|
79
|
+
export declare const EIA_NO_KEY_WARNING = "[tidegap/grid] no EIA API key configured \u2014 using mock data for EIA-eligible zones";
|
|
78
80
|
/**
|
|
79
81
|
* Carbon-intensity feed backed by the US Energy Information Administration's
|
|
80
82
|
* v2 Open Data API. Returns a synthesized hourly forecast from the most
|
|
@@ -83,7 +85,9 @@ export declare function ukCarbonIntensityFeed(): GridFeed;
|
|
|
83
85
|
* The key is always passed in by the host (CLI/MCP read `TIDEGAP_EIA_API_KEY`;
|
|
84
86
|
* the OpenClaw plugin reads `eiaApiKey` from plugin config).
|
|
85
87
|
*/
|
|
86
|
-
export declare function eiaFeed(apiKey?: string
|
|
88
|
+
export declare function eiaFeed(apiKey?: string, opts?: {
|
|
89
|
+
quiet?: boolean;
|
|
90
|
+
}): GridFeed;
|
|
87
91
|
/** One `<Period>` inside an ENTSO-E TimeSeries. */
|
|
88
92
|
export interface EntsoePeriod {
|
|
89
93
|
/** ISO-8601 start of the period's time interval. */
|
|
@@ -245,5 +249,9 @@ export declare function regionDataSource(zone: string, credentials?: GridFeedCre
|
|
|
245
249
|
* `buildDefaultGridFeed()` with no argument is exactly the "no keys
|
|
246
250
|
* configured" case: free UK feed for GB, deterministic mock everywhere else.
|
|
247
251
|
*/
|
|
248
|
-
export declare function buildDefaultGridFeed(credentials?: GridFeedCredentials
|
|
252
|
+
export declare function buildDefaultGridFeed(credentials?: GridFeedCredentials,
|
|
253
|
+
/** `quiet`: no "no EIA API key" line (a one-off CLI command; a receipt still says mock). */
|
|
254
|
+
opts?: {
|
|
255
|
+
quiet?: boolean;
|
|
256
|
+
}): GridFeed;
|
|
249
257
|
//# sourceMappingURL=grid.d.ts.map
|
package/dist/grid.js
CHANGED
|
@@ -380,6 +380,9 @@ const EIA_FUEL_FACTORS = {
|
|
|
380
380
|
* window left 11–15 hours-of-day and sent every US ISO to the mock.
|
|
381
381
|
*/
|
|
382
382
|
const EIA_HISTORY_HOURS = 72;
|
|
383
|
+
/** The line a keyless EIA feed prints, at most once per process. */
|
|
384
|
+
export const EIA_NO_KEY_WARNING = "[tidegap/grid] no EIA API key configured — using mock data for EIA-eligible zones";
|
|
385
|
+
let eiaKeyWarned = false;
|
|
383
386
|
/**
|
|
384
387
|
* Carbon-intensity feed backed by the US Energy Information Administration's
|
|
385
388
|
* v2 Open Data API. Returns a synthesized hourly forecast from the most
|
|
@@ -388,15 +391,19 @@ const EIA_HISTORY_HOURS = 72;
|
|
|
388
391
|
* The key is always passed in by the host (CLI/MCP read `TIDEGAP_EIA_API_KEY`;
|
|
389
392
|
* the OpenClaw plugin reads `eiaApiKey` from plugin config).
|
|
390
393
|
*/
|
|
391
|
-
export function eiaFeed(apiKey) {
|
|
394
|
+
export function eiaFeed(apiKey, opts = {}) {
|
|
392
395
|
const key = apiKey;
|
|
393
396
|
const mock = mockGridFeed();
|
|
394
397
|
if (!key) {
|
|
395
398
|
return {
|
|
396
399
|
source: "mock",
|
|
397
400
|
async fetchForecast(region, hours) {
|
|
398
|
-
//
|
|
399
|
-
|
|
401
|
+
// Once per process, across every keyless feed; a quiet feed (a one-off CLI command) never prints it.
|
|
402
|
+
if (!opts.quiet && !eiaKeyWarned) {
|
|
403
|
+
eiaKeyWarned = true;
|
|
404
|
+
// eslint-disable-next-line no-console
|
|
405
|
+
console.warn(EIA_NO_KEY_WARNING);
|
|
406
|
+
}
|
|
400
407
|
return mock.fetchForecast(region, hours);
|
|
401
408
|
},
|
|
402
409
|
};
|
|
@@ -1060,7 +1067,9 @@ export function regionDataSource(zone, credentials = {}) {
|
|
|
1060
1067
|
* `buildDefaultGridFeed()` with no argument is exactly the "no keys
|
|
1061
1068
|
* configured" case: free UK feed for GB, deterministic mock everywhere else.
|
|
1062
1069
|
*/
|
|
1063
|
-
export function buildDefaultGridFeed(credentials = {}
|
|
1070
|
+
export function buildDefaultGridFeed(credentials = {},
|
|
1071
|
+
/** `quiet`: no "no EIA API key" line (a one-off CLI command; a receipt still says mock). */
|
|
1072
|
+
opts = {}) {
|
|
1064
1073
|
const feeds = {
|
|
1065
1074
|
GB: ukCarbonIntensityFeed(),
|
|
1066
1075
|
};
|
|
@@ -1074,7 +1083,7 @@ export function buildDefaultGridFeed(credentials = {}) {
|
|
|
1074
1083
|
feeds[zone] = wattTimeFeed({
|
|
1075
1084
|
username: credentials.wattTimeUsername,
|
|
1076
1085
|
password: credentials.wattTimePassword,
|
|
1077
|
-
fallback: eiaFeed(credentials.eiaApiKey),
|
|
1086
|
+
fallback: eiaFeed(credentials.eiaApiKey, { quiet: opts.quiet }),
|
|
1078
1087
|
});
|
|
1079
1088
|
}
|
|
1080
1089
|
for (const zone of Object.keys(ENTSOE_BIDDING_ZONE_BY_REGION)) {
|
package/dist/handoff.d.ts
CHANGED
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
* and only Tidegap writes a result file, as a new file, never through a
|
|
10
10
|
* link.
|
|
11
11
|
*/
|
|
12
|
+
import { type SessionNotice, type TaskStore } from "./storage/sqlite.js";
|
|
13
|
+
import type { TaskRecord } from "./types.js";
|
|
12
14
|
/** A tool profile: a name for one of the existing tool sets. */
|
|
13
15
|
export type HandoffProfile = "read" | "web" | "none";
|
|
14
16
|
export declare const HANDOFF_PROFILES: readonly HandoffProfile[];
|
|
@@ -110,8 +112,13 @@ export interface TaskWorkShape {
|
|
|
110
112
|
profile?: string;
|
|
111
113
|
tools?: string[];
|
|
112
114
|
resultPath?: string;
|
|
113
|
-
/**
|
|
115
|
+
/**
|
|
116
|
+
* True when the task wakes its idle origin session when it finishes; on
|
|
117
|
+
* a run of a bound job, when the job sweep's decision says so.
|
|
118
|
+
*/
|
|
114
119
|
wake?: boolean;
|
|
120
|
+
/** On a run of a bound job: the job's wake rule, which decides whether this run wakes. */
|
|
121
|
+
wakeRule?: "failures" | "match";
|
|
115
122
|
/** The shell command of a one-off command task. */
|
|
116
123
|
command?: string;
|
|
117
124
|
}
|
|
@@ -142,6 +149,12 @@ export declare function wakeError(wake: unknown, origin: {
|
|
|
142
149
|
* fallback; it creates that file at start, so the two agree from then on.
|
|
143
150
|
*/
|
|
144
151
|
export declare function defaultDbPath(): string;
|
|
152
|
+
/**
|
|
153
|
+
* The `wake:` line of a task's work shape, or undefined when it does not
|
|
154
|
+
* wake: a one-off wake task wakes when it finishes; a run of a bound job
|
|
155
|
+
* says the job's rule, since most of its runs do not wake.
|
|
156
|
+
*/
|
|
157
|
+
export declare function wakeShapeText(shape: Pick<TaskWorkShape, "wake" | "wakeRule">): string | undefined;
|
|
145
158
|
/**
|
|
146
159
|
* True when `dbPath` is the default ledger. The session notice and wake
|
|
147
160
|
* markers under `~/.tidegap/sessions` belong to it: the plugin's hooks
|
|
@@ -151,8 +164,6 @@ export declare function defaultDbPath(): string;
|
|
|
151
164
|
* its folder's real path and its name.
|
|
152
165
|
*/
|
|
153
166
|
export declare function isDefaultLedger(dbPath: string): boolean;
|
|
154
|
-
/** A wake marker with no wake task left to wait for goes this long after its last refresh (the waiter refreshes it at every Stop). */
|
|
155
|
-
export declare const WAKE_MARKER_MAX_AGE_MS: number;
|
|
156
167
|
/** `~/.tidegap/sessions/wake`: one marker per session with a task to wake it for. */
|
|
157
168
|
export declare function wakeMarkersDir(home?: string): string;
|
|
158
169
|
/** The wake marker of one session, or undefined for an id that fails the id rule. */
|
|
@@ -168,4 +179,56 @@ export declare function wakeMarkerPath(sessionId: string, dir?: string): string
|
|
|
168
179
|
export declare function writeWakeMarker(sessionId: string, dir?: string, now?: Date): string | undefined;
|
|
169
180
|
/** Remove a session's wake marker; a missing one is fine. */
|
|
170
181
|
export declare function removeWakeMarker(sessionId: string, dir?: string): void;
|
|
182
|
+
/** True for a task queued with wake, and for a run of a job bound to a session. */
|
|
183
|
+
export declare function isWakeTask(record: Pick<TaskRecord<unknown>, "bodyJson">): boolean;
|
|
184
|
+
/**
|
|
185
|
+
* True when a session notice wakes its session: a run of a bound job when
|
|
186
|
+
* the job sweep's decision says so, any other completed or failed wake
|
|
187
|
+
* task. A cancel is someone's own act: it never spends a turn.
|
|
188
|
+
*/
|
|
189
|
+
export declare function noticeWakes(n: SessionNotice): boolean;
|
|
190
|
+
/**
|
|
191
|
+
* The wake tasks still to come of one session, or of every session when
|
|
192
|
+
* `sessionId` is undefined: not finished yet, or completed and waiting for
|
|
193
|
+
* its result file (its notice comes once the file is). A row that completed
|
|
194
|
+
* more than {@link SESSION_NOTICE_MAX_AGE_MS} ago no longer counts: it can
|
|
195
|
+
* never be a notice.
|
|
196
|
+
*/
|
|
197
|
+
export declare function wakeTasksToCome(store: TaskStore, sessionId: string | undefined, now: Date): TaskRecord<unknown>[];
|
|
198
|
+
/**
|
|
199
|
+
* True when a session has something left to wait for (KTD11): an open wake
|
|
200
|
+
* task, a wake task waiting for its result file, an unclaimed notice that
|
|
201
|
+
* wakes, or an active job bound to it. The bound job counts so a waiter
|
|
202
|
+
* does not stop between a run's finish and the sweep's decision. With
|
|
203
|
+
* `since`, only a task queued after it and a job bound after it count (a
|
|
204
|
+
* second look after a removal); a run of a bound job counts through its
|
|
205
|
+
* job's bind time only, since every run is a fresh row and would keep the
|
|
206
|
+
* 7-day rule from ever ending the marker. An unclaimed wake notice counts
|
|
207
|
+
* whatever its time. The reads go against the job sweep's write order
|
|
208
|
+
* (run, then notice, then job): the job first, so a run that finishes
|
|
209
|
+
* between two reads is seen by one of them. `notices` is the session's
|
|
210
|
+
* notice list when the caller has just read it. Throws when the ledger
|
|
211
|
+
* cannot be read.
|
|
212
|
+
*/
|
|
213
|
+
export declare function sessionHasWaitWork(store: TaskStore, sessionId: string, now?: Date, opts?: {
|
|
214
|
+
since?: number;
|
|
215
|
+
notices?: readonly SessionNotice[];
|
|
216
|
+
}): boolean;
|
|
217
|
+
/**
|
|
218
|
+
* Remove a session's wake marker, then look once more
|
|
219
|
+
* ({@link sessionHasWaitWork}, from `since` when given), and write the
|
|
220
|
+
* marker again when the session has something left or the read fails. A
|
|
221
|
+
* writer commits its row (or job) before it writes the marker, so work
|
|
222
|
+
* added while the caller decided is either seen here or writes its marker
|
|
223
|
+
* after the removal. Returns "removed", "kept" or "unread" (the read
|
|
224
|
+
* failed). Never throws.
|
|
225
|
+
*/
|
|
226
|
+
export declare function removeWakeMarkerUnlessPending(store: TaskStore, sessionId: string, dir?: string, now?: Date, since?: number): "removed" | "kept" | "unread";
|
|
227
|
+
/**
|
|
228
|
+
* The agent's own act ended something a session waited for (a cancel, a
|
|
229
|
+
* job removed, paused or unbound): drop the session's marker now unless
|
|
230
|
+
* it has something left (KTD12). Only on the default ledger, whose markers
|
|
231
|
+
* these are; `sessionId` undefined does nothing. Never throws.
|
|
232
|
+
*/
|
|
233
|
+
export declare function releaseWakeMarker(store: TaskStore | undefined, dbPath: string | undefined, sessionId: string | undefined): void;
|
|
171
234
|
//# sourceMappingURL=handoff.d.ts.map
|