@tidegap/core 0.19.0 → 0.21.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 +1 -1
- package/dist/agent-snippet.d.ts +30 -0
- package/dist/agent-snippet.js +120 -0
- 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 +138 -0
- package/dist/channels/format.js +425 -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 +84 -0
- package/dist/energy.js +138 -0
- package/dist/grid.d.ts +17 -2
- package/dist/grid.js +43 -4
- package/dist/handoff.d.ts +171 -0
- package/dist/handoff.js +482 -0
- package/dist/history.d.ts +17 -0
- package/dist/history.js +14 -1
- package/dist/index.d.ts +49 -15
- package/dist/index.js +36 -8
- package/dist/job-cadence.d.ts +76 -0
- package/dist/job-cadence.js +240 -0
- package/dist/jobs.d.ts +183 -0
- package/dist/jobs.js +917 -0
- package/dist/notify.d.ts +11 -0
- package/dist/notify.js +19 -4
- package/dist/postpone.d.ts +113 -0
- package/dist/postpone.js +185 -0
- package/dist/providers/anthropic.js +14 -8
- package/dist/providers/base.d.ts +145 -2
- package/dist/providers/base.js +41 -1
- 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 +4 -0
- package/dist/providers/gemini.js +34 -24
- package/dist/providers/http.d.ts +40 -0
- package/dist/providers/http.js +82 -0
- package/dist/providers/index.d.ts +2 -2
- package/dist/providers/index.js +1 -1
- package/dist/providers/ollama.d.ts +4 -0
- package/dist/providers/ollama.js +32 -25
- package/dist/providers/openai.js +11 -9
- package/dist/providers/timeout.d.ts +36 -0
- package/dist/providers/timeout.js +71 -0
- package/dist/quota.d.ts +7 -2
- package/dist/quota.js +12 -27
- package/dist/readiness.d.ts +205 -0
- package/dist/readiness.js +326 -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-limits.d.ts +268 -0
- package/dist/run-limits.js +572 -0
- package/dist/run-session.d.ts +36 -0
- package/dist/run-session.js +72 -0
- package/dist/schedule-job.d.ts +118 -0
- package/dist/schedule-job.js +243 -0
- package/dist/scheduler.d.ts +391 -18
- package/dist/scheduler.js +1341 -177
- package/dist/sign.d.ts +8 -1
- package/dist/sign.js +49 -3
- package/dist/storage/sqlite.d.ts +183 -5
- package/dist/storage/sqlite.js +720 -19
- package/dist/task-events.d.ts +117 -0
- package/dist/task-events.js +345 -0
- package/dist/tool-surface.d.ts +31 -2
- package/dist/tool-surface.js +390 -65
- package/dist/types.d.ts +267 -3
- package/dist/types.js +6 -1
- package/dist/zone-time.d.ts +22 -0
- package/dist/zone-time.js +76 -0
- package/package.json +13 -5
- 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,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Channel kind descriptors (KTD1): each registry kind as data (fields,
|
|
3
|
+
* where-to-get-it steps, default events, docs anchor) plus a pure
|
|
4
|
+
* `format(payload, values) → DeliverySpec`. The host page renders a card
|
|
5
|
+
* from a descriptor without `format`; the host driver performs the spec.
|
|
6
|
+
*/
|
|
7
|
+
import type { NotificationPayload } from "./payload.js";
|
|
8
|
+
import type { EventKind, RegistryKind } from "./types.js";
|
|
9
|
+
/** How a card renders a field. */
|
|
10
|
+
export type FieldInput = "text" | "password" | "url" | "select" | "number" | "textarea";
|
|
11
|
+
export interface FieldOption {
|
|
12
|
+
value: string;
|
|
13
|
+
label: string;
|
|
14
|
+
}
|
|
15
|
+
/** One input of a kind. */
|
|
16
|
+
export interface KindField {
|
|
17
|
+
name: string;
|
|
18
|
+
label: string;
|
|
19
|
+
input: FieldInput;
|
|
20
|
+
required?: boolean;
|
|
21
|
+
/** Stored in the host's secrets file, never in the ledger or an API answer. */
|
|
22
|
+
secret?: boolean;
|
|
23
|
+
default?: string | number;
|
|
24
|
+
placeholder?: string;
|
|
25
|
+
/** Regular expression source the whole value must match. */
|
|
26
|
+
pattern?: string;
|
|
27
|
+
/** `select` only: the allowed values. */
|
|
28
|
+
options?: readonly FieldOption[];
|
|
29
|
+
help?: string;
|
|
30
|
+
/** Shown under a collapsed disclosure on the card. */
|
|
31
|
+
advanced?: boolean;
|
|
32
|
+
/** `number` only: inclusive range. */
|
|
33
|
+
min?: number;
|
|
34
|
+
max?: number;
|
|
35
|
+
/** Overrides the default length cap of the input type. */
|
|
36
|
+
maxLength?: number;
|
|
37
|
+
/** `textarea` only: most non-empty lines. */
|
|
38
|
+
maxLines?: number;
|
|
39
|
+
/** The sentence shown when the value fails `pattern` or its type check (default: a generic one). */
|
|
40
|
+
invalid?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Secret fields only: the field this secret belongs to (the ntfy token to
|
|
43
|
+
* its server, a webhook's signing secret to its URL). An empty input reuses
|
|
44
|
+
* the stored secret only while that field's origin is unchanged, so a
|
|
45
|
+
* secret never follows a new address the user typed.
|
|
46
|
+
*/
|
|
47
|
+
keepWith?: string;
|
|
48
|
+
/**
|
|
49
|
+
* `url` owners of a `keepWith` secret: hosts where the path, not the
|
|
50
|
+
* origin, names the receiver (`hooks.zapier.com`, `*.logic.azure.com`).
|
|
51
|
+
* There the secret is kept only for the same origin and path; see
|
|
52
|
+
* {@link keepsSecret}.
|
|
53
|
+
*/
|
|
54
|
+
keepPathHosts?: readonly string[];
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* True when `host` matches one of `patterns`: an exact host, or `*.suffix` for any subdomain of `suffix`.
|
|
58
|
+
* One trailing dot is ignored: `hooks.zapier.com.` is the same host as `hooks.zapier.com`.
|
|
59
|
+
*/
|
|
60
|
+
export declare function hostMatches(host: string, patterns: readonly string[]): boolean;
|
|
61
|
+
/**
|
|
62
|
+
* The address a `keepWith` secret belongs to: the origin of the owner's
|
|
63
|
+
* value, plus the path on a {@link KindField.keepPathHosts} host. A value
|
|
64
|
+
* that is not a URL is its trimmed self.
|
|
65
|
+
*/
|
|
66
|
+
export declare function keepAddress(owner: KindField, value: unknown): string;
|
|
67
|
+
/**
|
|
68
|
+
* Whether a stored `keepWith` secret is kept when the owner field holds
|
|
69
|
+
* `typed` (undefined: left empty, so it keeps its stored value). The same
|
|
70
|
+
* rule runs in the card, which sees a secret owner only as its hint (host
|
|
71
|
+
* and last 4 characters): so a secret owner typed again on a path host
|
|
72
|
+
* drops the secret, as the card cannot compare the stored path.
|
|
73
|
+
*/
|
|
74
|
+
export declare function keepsSecret(owner: KindField, stored: unknown, typed: unknown): boolean;
|
|
75
|
+
/** A named set of field values (email providers). */
|
|
76
|
+
export interface KindPreset {
|
|
77
|
+
id: string;
|
|
78
|
+
label: string;
|
|
79
|
+
values: Readonly<Record<string, string | number>>;
|
|
80
|
+
note?: string;
|
|
81
|
+
}
|
|
82
|
+
/** One "where to get this" step: text plus an optional provider link. */
|
|
83
|
+
export interface KindStep {
|
|
84
|
+
text: string;
|
|
85
|
+
link?: string;
|
|
86
|
+
}
|
|
87
|
+
/** What a host driver sends. */
|
|
88
|
+
export type DeliverySpec = {
|
|
89
|
+
type: "http";
|
|
90
|
+
method: "POST";
|
|
91
|
+
url: string;
|
|
92
|
+
headers: Record<string, string>;
|
|
93
|
+
body: string;
|
|
94
|
+
/** Sign the body with HMAC-SHA256 (webhook, KTD13); the driver adds the timestamp and signature headers. */
|
|
95
|
+
sign?: {
|
|
96
|
+
secret: string;
|
|
97
|
+
};
|
|
98
|
+
} | {
|
|
99
|
+
type: "smtp";
|
|
100
|
+
host: string;
|
|
101
|
+
port: number;
|
|
102
|
+
/** `tls` = implicit TLS, `starttls` = upgrade before AUTH, `none` = loopback only (KTD5). */
|
|
103
|
+
security: SmtpSecurity;
|
|
104
|
+
/** Absent with `password`: no AUTH. */
|
|
105
|
+
user?: string;
|
|
106
|
+
password?: string;
|
|
107
|
+
from: string;
|
|
108
|
+
to: string;
|
|
109
|
+
subject: string;
|
|
110
|
+
text: string;
|
|
111
|
+
} | {
|
|
112
|
+
type: "shortcut";
|
|
113
|
+
name: string;
|
|
114
|
+
input: string;
|
|
115
|
+
}
|
|
116
|
+
/** A program on this computer: `path` with `args` (argv, no shell), `input` on its stdin. */
|
|
117
|
+
| {
|
|
118
|
+
type: "command";
|
|
119
|
+
path: string;
|
|
120
|
+
args: string[];
|
|
121
|
+
input: string;
|
|
122
|
+
};
|
|
123
|
+
export type SmtpSecurity = "tls" | "starttls" | "none";
|
|
124
|
+
/** Field values after validation: non-secret and secret together, as `format` reads them. */
|
|
125
|
+
export type FieldValues = Readonly<Record<string, string | number>>;
|
|
126
|
+
export interface KindDescriptor {
|
|
127
|
+
kind: RegistryKind;
|
|
128
|
+
label: string;
|
|
129
|
+
/** One line under the card title. */
|
|
130
|
+
description: string;
|
|
131
|
+
/** `process.platform` values the kind works on; absent = every platform. */
|
|
132
|
+
platforms?: readonly string[];
|
|
133
|
+
/** False while the kind's stream has not landed: the page and CLI hide it. */
|
|
134
|
+
ready: boolean;
|
|
135
|
+
fields: readonly KindField[];
|
|
136
|
+
presets?: readonly KindPreset[];
|
|
137
|
+
/** At most four. */
|
|
138
|
+
steps: readonly KindStep[];
|
|
139
|
+
defaultEvents: readonly EventKind[];
|
|
140
|
+
/** `notify-<kind>`, the anchor of the kind's docs section. */
|
|
141
|
+
docsAnchor: string;
|
|
142
|
+
/** Services the kind also covers (the webhook's Teams, Google Chat…). */
|
|
143
|
+
worksWith?: string;
|
|
144
|
+
/** Absent for Telegram, which sends through `TelegramClient` (KTD3), and for kinds not ready yet. */
|
|
145
|
+
format?: (payload: NotificationPayload, values: FieldValues) => DeliverySpec;
|
|
146
|
+
/**
|
|
147
|
+
* The card's one-line target ("#alerts", "you@example.com"). It must hold
|
|
148
|
+
* no secret value; `hints` are the secret fields' masked hints. Absent: the
|
|
149
|
+
* host derives one from the first URL hint or the first non-secret value.
|
|
150
|
+
*/
|
|
151
|
+
target?: (values: FieldValues, hints: Readonly<Record<string, string>>) => string;
|
|
152
|
+
/**
|
|
153
|
+
* Kind-specific sentences for delivery codes (`http_404`, `auth`…), each one
|
|
154
|
+
* sentence with the fix, and the field it is about. Absent codes take the
|
|
155
|
+
* host's generic sentence.
|
|
156
|
+
*/
|
|
157
|
+
errors?: Readonly<Record<string, KindErrorText>>;
|
|
158
|
+
/**
|
|
159
|
+
* Checks across or inside fields that a pattern cannot express (the
|
|
160
|
+
* webhook's header lines). Runs after {@link validateFields} on the merged
|
|
161
|
+
* values; the answer names the field and one sentence with the fix.
|
|
162
|
+
*/
|
|
163
|
+
validate?: (values: FieldValues) => KindErrorText & {
|
|
164
|
+
field: string;
|
|
165
|
+
} | undefined;
|
|
166
|
+
}
|
|
167
|
+
/** A kind's sentence for one delivery code. */
|
|
168
|
+
export interface KindErrorText {
|
|
169
|
+
text: string;
|
|
170
|
+
field?: string;
|
|
171
|
+
}
|
|
172
|
+
/** A descriptor as the page receives it: data only, no functions and no host error texts. */
|
|
173
|
+
export type KindDescriptorData = Omit<KindDescriptor, "format" | "target" | "errors" | "validate">;
|
|
174
|
+
export declare function descriptorData(descriptor: KindDescriptor): KindDescriptorData;
|
|
175
|
+
export type FieldErrorCode = "required" | "invalid" | "too_long";
|
|
176
|
+
export type FieldValidation = {
|
|
177
|
+
ok: true;
|
|
178
|
+
values: Record<string, string | number>;
|
|
179
|
+
secrets: Record<string, string>;
|
|
180
|
+
} | {
|
|
181
|
+
ok: false;
|
|
182
|
+
field: string;
|
|
183
|
+
code: FieldErrorCode;
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* Check `input` against a descriptor's fields, in field order; the first
|
|
187
|
+
* failing field is the answer. Unknown keys are ignored, an empty value
|
|
188
|
+
* takes the field's default, and secret fields come back in `secrets` only.
|
|
189
|
+
*/
|
|
190
|
+
export declare function validateFields(descriptor: KindDescriptor, input: Readonly<Record<string, unknown>>): FieldValidation;
|
|
191
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Channel kind descriptors (KTD1): each registry kind as data (fields,
|
|
3
|
+
* where-to-get-it steps, default events, docs anchor) plus a pure
|
|
4
|
+
* `format(payload, values) → DeliverySpec`. The host page renders a card
|
|
5
|
+
* from a descriptor without `format`; the host driver performs the spec.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* True when `host` matches one of `patterns`: an exact host, or `*.suffix` for any subdomain of `suffix`.
|
|
9
|
+
* One trailing dot is ignored: `hooks.zapier.com.` is the same host as `hooks.zapier.com`.
|
|
10
|
+
*/
|
|
11
|
+
export function hostMatches(host, patterns) {
|
|
12
|
+
const h = host.toLowerCase().replace(/\.$/, "");
|
|
13
|
+
return patterns.some((p) => (p.startsWith("*.") ? h.endsWith(p.slice(1)) : h === p));
|
|
14
|
+
}
|
|
15
|
+
function parseUrl(v) {
|
|
16
|
+
try {
|
|
17
|
+
return new URL(String(v).trim());
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The address a `keepWith` secret belongs to: the origin of the owner's
|
|
25
|
+
* value, plus the path on a {@link KindField.keepPathHosts} host. A value
|
|
26
|
+
* that is not a URL is its trimmed self.
|
|
27
|
+
*/
|
|
28
|
+
export function keepAddress(owner, value) {
|
|
29
|
+
const url = parseUrl(value);
|
|
30
|
+
if (!url)
|
|
31
|
+
return String(value ?? "").trim();
|
|
32
|
+
return owner.keepPathHosts && hostMatches(url.hostname, owner.keepPathHosts) ? `${url.origin}${url.pathname}` : url.origin;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Whether a stored `keepWith` secret is kept when the owner field holds
|
|
36
|
+
* `typed` (undefined: left empty, so it keeps its stored value). The same
|
|
37
|
+
* rule runs in the card, which sees a secret owner only as its hint (host
|
|
38
|
+
* and last 4 characters): so a secret owner typed again on a path host
|
|
39
|
+
* drops the secret, as the card cannot compare the stored path.
|
|
40
|
+
*/
|
|
41
|
+
export function keepsSecret(owner, stored, typed) {
|
|
42
|
+
if (typed === undefined)
|
|
43
|
+
return true;
|
|
44
|
+
if (owner.secret && owner.keepPathHosts) {
|
|
45
|
+
const url = parseUrl(typed);
|
|
46
|
+
if (url && hostMatches(url.hostname, owner.keepPathHosts))
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
return keepAddress(owner, typed ?? owner.default) === keepAddress(owner, stored ?? owner.default);
|
|
50
|
+
}
|
|
51
|
+
export function descriptorData(descriptor) {
|
|
52
|
+
const { format: _format, target: _target, errors: _errors, validate: _validate, ...data } = descriptor;
|
|
53
|
+
return data;
|
|
54
|
+
}
|
|
55
|
+
/** Default length caps per input type. */
|
|
56
|
+
const MAX_LENGTH = { url: 2048, text: 256, password: 256, select: 256, number: 32, textarea: 4096 };
|
|
57
|
+
/** Lines of a textarea field when it sets no `maxLines`. */
|
|
58
|
+
const DEFAULT_MAX_LINES = 10;
|
|
59
|
+
/** C0, DEL, C1 and the Unicode line and paragraph separators: none may reach a header, subject or log line. */
|
|
60
|
+
const CONTROL = /[\u0000-\u001f\u007f-\u009f\u2028\u2029]/;
|
|
61
|
+
const CONTROL_BUT_NEWLINE = /[\u0000-\u0009\u000b-\u001f\u007f-\u009f\u2028\u2029]/;
|
|
62
|
+
/** Plain decimal numbers only: no hex, binary or exponent forms. */
|
|
63
|
+
const DECIMAL = /^-?\d+(?:\.\d+)?$/;
|
|
64
|
+
/** A string or number input as trimmed text (CRLF → LF in a textarea); anything else unchanged. */
|
|
65
|
+
function normalize(field, raw) {
|
|
66
|
+
if (typeof raw === "number")
|
|
67
|
+
return String(raw);
|
|
68
|
+
if (typeof raw !== "string")
|
|
69
|
+
return raw;
|
|
70
|
+
return (field.input === "textarea" ? raw.replace(/\r\n/g, "\n") : raw).trim();
|
|
71
|
+
}
|
|
72
|
+
function checkField(field, raw) {
|
|
73
|
+
const given = normalize(field, raw);
|
|
74
|
+
if (given === undefined || given === null || given === "") {
|
|
75
|
+
if (field.default !== undefined)
|
|
76
|
+
return { value: field.default };
|
|
77
|
+
return field.required ? { code: "required" } : {};
|
|
78
|
+
}
|
|
79
|
+
if (typeof given !== "string")
|
|
80
|
+
return { code: "invalid" };
|
|
81
|
+
if (given.length > (field.maxLength ?? MAX_LENGTH[field.input]))
|
|
82
|
+
return { code: "too_long" };
|
|
83
|
+
if ((field.input === "textarea" ? CONTROL_BUT_NEWLINE : CONTROL).test(given))
|
|
84
|
+
return { code: "invalid" };
|
|
85
|
+
if (field.pattern && !new RegExp(`^(?:${field.pattern})$`).test(given))
|
|
86
|
+
return { code: "invalid" };
|
|
87
|
+
switch (field.input) {
|
|
88
|
+
case "select":
|
|
89
|
+
return field.options?.some((o) => o.value === given) ? { value: given } : { code: "invalid" };
|
|
90
|
+
case "number": {
|
|
91
|
+
const n = DECIMAL.test(given) ? Number(given) : NaN;
|
|
92
|
+
if (!Number.isFinite(n) || (field.min !== undefined && n < field.min) || (field.max !== undefined && n > field.max))
|
|
93
|
+
return { code: "invalid" };
|
|
94
|
+
return { value: n };
|
|
95
|
+
}
|
|
96
|
+
case "url":
|
|
97
|
+
return URL.canParse(given) ? { value: given } : { code: "invalid" };
|
|
98
|
+
case "textarea":
|
|
99
|
+
return given.split("\n").filter((l) => l.trim()).length > (field.maxLines ?? DEFAULT_MAX_LINES) ? { code: "too_long" } : { value: given };
|
|
100
|
+
default:
|
|
101
|
+
return { value: given };
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Check `input` against a descriptor's fields, in field order; the first
|
|
106
|
+
* failing field is the answer. Unknown keys are ignored, an empty value
|
|
107
|
+
* takes the field's default, and secret fields come back in `secrets` only.
|
|
108
|
+
*/
|
|
109
|
+
export function validateFields(descriptor, input) {
|
|
110
|
+
const values = {};
|
|
111
|
+
const secrets = {};
|
|
112
|
+
for (const field of descriptor.fields) {
|
|
113
|
+
const { value, code } = checkField(field, Object.hasOwn(input, field.name) ? input[field.name] : undefined);
|
|
114
|
+
if (code)
|
|
115
|
+
return { ok: false, field: field.name, code };
|
|
116
|
+
if (value === undefined)
|
|
117
|
+
continue;
|
|
118
|
+
if (field.secret)
|
|
119
|
+
secrets[field.name] = String(value);
|
|
120
|
+
else
|
|
121
|
+
values[field.name] = value;
|
|
122
|
+
}
|
|
123
|
+
return { ok: true, values, secrets };
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Telegram Bot API client (KTD10, KTD13). Takes the token, the API base URL
|
|
3
|
+
* and `fetch` as arguments; reads no environment. Messages are plain text
|
|
4
|
+
* (no `parse_mode`), so titles and model output cannot inject markup.
|
|
5
|
+
*
|
|
6
|
+
* The request URL holds the token, so no error message or log line built
|
|
7
|
+
* here contains the URL, the token, or a transport error's own text: only
|
|
8
|
+
* the Bot API method, the HTTP status and Telegram's error description.
|
|
9
|
+
*/
|
|
10
|
+
/** The public Bot API. */
|
|
11
|
+
export declare const TELEGRAM_API_BASE = "https://api.telegram.org";
|
|
12
|
+
/** Telegram's cap on one message's text. */
|
|
13
|
+
export declare const TELEGRAM_MESSAGE_MAX = 4096;
|
|
14
|
+
/** Longest wait for one Bot API call beyond its long-poll `timeout`, in ms. */
|
|
15
|
+
export declare const TELEGRAM_CALL_TIMEOUT_MS = 30000;
|
|
16
|
+
/** Telegram's cap on a button's `callback_data`, in bytes. */
|
|
17
|
+
export declare const TELEGRAM_CALLBACK_MAX_BYTES = 64;
|
|
18
|
+
export interface TelegramChat {
|
|
19
|
+
id: number;
|
|
20
|
+
type: "private" | "group" | "supergroup" | "channel";
|
|
21
|
+
username?: string;
|
|
22
|
+
first_name?: string;
|
|
23
|
+
}
|
|
24
|
+
export interface TelegramUser {
|
|
25
|
+
id: number;
|
|
26
|
+
is_bot?: boolean;
|
|
27
|
+
username?: string;
|
|
28
|
+
first_name?: string;
|
|
29
|
+
}
|
|
30
|
+
export interface TelegramMessage {
|
|
31
|
+
message_id: number;
|
|
32
|
+
chat: TelegramChat;
|
|
33
|
+
from?: TelegramUser;
|
|
34
|
+
date?: number;
|
|
35
|
+
text?: string;
|
|
36
|
+
}
|
|
37
|
+
export interface TelegramCallbackQuery {
|
|
38
|
+
id: string;
|
|
39
|
+
from: TelegramUser;
|
|
40
|
+
message?: TelegramMessage;
|
|
41
|
+
data?: string;
|
|
42
|
+
}
|
|
43
|
+
export interface TelegramUpdate {
|
|
44
|
+
update_id: number;
|
|
45
|
+
message?: TelegramMessage;
|
|
46
|
+
callback_query?: TelegramCallbackQuery;
|
|
47
|
+
}
|
|
48
|
+
/** One inline button: its label and its `callback_data`. */
|
|
49
|
+
export interface InlineButton {
|
|
50
|
+
text: string;
|
|
51
|
+
data: string;
|
|
52
|
+
}
|
|
53
|
+
/** Rows of inline buttons. */
|
|
54
|
+
export type InlineKeyboard = InlineButton[][];
|
|
55
|
+
/** `unauthorized` = 401 (bad token); `conflict` = 409 (another `getUpdates` consumer or a webhook). */
|
|
56
|
+
export type TelegramErrorKind = "unauthorized" | "conflict" | "rate_limited" | "http" | "network" | "api";
|
|
57
|
+
export declare class TelegramError extends Error {
|
|
58
|
+
readonly kind: TelegramErrorKind;
|
|
59
|
+
readonly method: string;
|
|
60
|
+
readonly status?: number | undefined;
|
|
61
|
+
/** Seconds Telegram asked to wait (429). */
|
|
62
|
+
readonly retryAfterS?: number | undefined;
|
|
63
|
+
name: string;
|
|
64
|
+
constructor(message: string, kind: TelegramErrorKind, method: string, status?: number | undefined,
|
|
65
|
+
/** Seconds Telegram asked to wait (429). */
|
|
66
|
+
retryAfterS?: number | undefined);
|
|
67
|
+
}
|
|
68
|
+
export interface TelegramClientOptions {
|
|
69
|
+
token: string;
|
|
70
|
+
/** Default {@link TELEGRAM_API_BASE}. Hosts allow an override only on loopback ({@link isLoopbackApiBase}). */
|
|
71
|
+
baseUrl?: string;
|
|
72
|
+
/** Default: the global `fetch`, looked up at call time. */
|
|
73
|
+
fetch?: typeof fetch;
|
|
74
|
+
}
|
|
75
|
+
/** True for an `http://127.0.0.1[:port]` or `http://localhost[:port]` base (KTD13: tests and QA only). */
|
|
76
|
+
export declare function isLoopbackApiBase(url: string): boolean;
|
|
77
|
+
export declare class TelegramClient {
|
|
78
|
+
private readonly token;
|
|
79
|
+
private readonly base;
|
|
80
|
+
private readonly fetchFn;
|
|
81
|
+
constructor(opts: TelegramClientOptions);
|
|
82
|
+
/**
|
|
83
|
+
* Call one Bot API method with a JSON body; throws {@link TelegramError}.
|
|
84
|
+
* The call gives up after its long-poll `timeout` plus
|
|
85
|
+
* {@link TELEGRAM_CALL_TIMEOUT_MS}, so a stalled connection cannot hold
|
|
86
|
+
* the tick or the listener.
|
|
87
|
+
*/
|
|
88
|
+
call<T>(method: string, body?: Record<string, unknown>, signal?: AbortSignal): Promise<T>;
|
|
89
|
+
getMe(): Promise<TelegramUser>;
|
|
90
|
+
getWebhookInfo(): Promise<{
|
|
91
|
+
url: string;
|
|
92
|
+
}>;
|
|
93
|
+
/** Long poll; `timeoutS` is Telegram's server-side wait. */
|
|
94
|
+
getUpdates(opts?: {
|
|
95
|
+
offset?: number;
|
|
96
|
+
timeoutS?: number;
|
|
97
|
+
signal?: AbortSignal;
|
|
98
|
+
}): Promise<TelegramUpdate[]>;
|
|
99
|
+
/** Send plain text, optionally with inline buttons. */
|
|
100
|
+
sendMessage(chatId: number | string, text: string, opts?: {
|
|
101
|
+
buttons?: InlineKeyboard;
|
|
102
|
+
}): Promise<TelegramMessage>;
|
|
103
|
+
answerCallbackQuery(id: string, opts?: {
|
|
104
|
+
text?: string;
|
|
105
|
+
showAlert?: boolean;
|
|
106
|
+
}): Promise<boolean>;
|
|
107
|
+
/** Replace a message's buttons; an empty or missing keyboard removes them. */
|
|
108
|
+
editMessageReplyMarkup(chatId: number | string, messageId: number, buttons?: InlineKeyboard): Promise<unknown>;
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=telegram.d.ts.map
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Telegram Bot API client (KTD10, KTD13). Takes the token, the API base URL
|
|
3
|
+
* and `fetch` as arguments; reads no environment. Messages are plain text
|
|
4
|
+
* (no `parse_mode`), so titles and model output cannot inject markup.
|
|
5
|
+
*
|
|
6
|
+
* The request URL holds the token, so no error message or log line built
|
|
7
|
+
* here contains the URL, the token, or a transport error's own text: only
|
|
8
|
+
* the Bot API method, the HTTP status and Telegram's error description.
|
|
9
|
+
*/
|
|
10
|
+
/** The public Bot API. */
|
|
11
|
+
export const TELEGRAM_API_BASE = "https://api.telegram.org";
|
|
12
|
+
/** Telegram's cap on one message's text. */
|
|
13
|
+
export const TELEGRAM_MESSAGE_MAX = 4096;
|
|
14
|
+
/** Longest wait for one Bot API call beyond its long-poll `timeout`, in ms. */
|
|
15
|
+
export const TELEGRAM_CALL_TIMEOUT_MS = 30_000;
|
|
16
|
+
/** Telegram's cap on a button's `callback_data`, in bytes. */
|
|
17
|
+
export const TELEGRAM_CALLBACK_MAX_BYTES = 64;
|
|
18
|
+
export class TelegramError extends Error {
|
|
19
|
+
kind;
|
|
20
|
+
method;
|
|
21
|
+
status;
|
|
22
|
+
retryAfterS;
|
|
23
|
+
name = "TelegramError";
|
|
24
|
+
constructor(message, kind, method, status,
|
|
25
|
+
/** Seconds Telegram asked to wait (429). */
|
|
26
|
+
retryAfterS) {
|
|
27
|
+
super(message);
|
|
28
|
+
this.kind = kind;
|
|
29
|
+
this.method = method;
|
|
30
|
+
this.status = status;
|
|
31
|
+
this.retryAfterS = retryAfterS;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/** True for an `http://127.0.0.1[:port]` or `http://localhost[:port]` base (KTD13: tests and QA only). */
|
|
35
|
+
export function isLoopbackApiBase(url) {
|
|
36
|
+
try {
|
|
37
|
+
const u = new URL(url);
|
|
38
|
+
return u.protocol === "http:" && (u.hostname === "127.0.0.1" || u.hostname === "localhost") && u.username === "" && u.password === "";
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** Keep Telegram's description short and free of the token. */
|
|
45
|
+
function cleanDescription(text, token) {
|
|
46
|
+
if (!text)
|
|
47
|
+
return "";
|
|
48
|
+
return text.split(token).join("<token>").slice(0, 200);
|
|
49
|
+
}
|
|
50
|
+
export class TelegramClient {
|
|
51
|
+
token;
|
|
52
|
+
base;
|
|
53
|
+
fetchFn;
|
|
54
|
+
constructor(opts) {
|
|
55
|
+
this.token = opts.token;
|
|
56
|
+
let base = opts.baseUrl ?? TELEGRAM_API_BASE;
|
|
57
|
+
while (base.endsWith("/"))
|
|
58
|
+
base = base.slice(0, -1);
|
|
59
|
+
this.base = base;
|
|
60
|
+
this.fetchFn = opts.fetch;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Call one Bot API method with a JSON body; throws {@link TelegramError}.
|
|
64
|
+
* The call gives up after its long-poll `timeout` plus
|
|
65
|
+
* {@link TELEGRAM_CALL_TIMEOUT_MS}, so a stalled connection cannot hold
|
|
66
|
+
* the tick or the listener.
|
|
67
|
+
*/
|
|
68
|
+
async call(method, body = {}, signal) {
|
|
69
|
+
const f = this.fetchFn ?? globalThis.fetch;
|
|
70
|
+
const pollS = typeof body.timeout === "number" ? body.timeout : 0;
|
|
71
|
+
const controller = new AbortController();
|
|
72
|
+
let timedOut = false;
|
|
73
|
+
const timer = setTimeout(() => {
|
|
74
|
+
timedOut = true;
|
|
75
|
+
controller.abort();
|
|
76
|
+
}, pollS * 1000 + TELEGRAM_CALL_TIMEOUT_MS);
|
|
77
|
+
const forward = () => controller.abort();
|
|
78
|
+
if (signal?.aborted)
|
|
79
|
+
controller.abort();
|
|
80
|
+
else
|
|
81
|
+
signal?.addEventListener("abort", forward, { once: true });
|
|
82
|
+
let res;
|
|
83
|
+
let env = {};
|
|
84
|
+
try {
|
|
85
|
+
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
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
const code = err?.cause?.code;
|
|
95
|
+
const why = typeof code === "string" && /^[A-Z_]+$/.test(code) ? ` (${code})` : "";
|
|
96
|
+
if (timedOut)
|
|
97
|
+
throw new TelegramError(`Telegram ${method}: timed out`, "network", method);
|
|
98
|
+
if (err?.name === "AbortError") {
|
|
99
|
+
throw new TelegramError(`Telegram ${method}: aborted`, "network", method);
|
|
100
|
+
}
|
|
101
|
+
throw new TelegramError(`Telegram ${method}: network error${why}`, "network", method);
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
env = (await res.json());
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
// A non-JSON body (a proxy error page), or a body cut off by the timeout: the status speaks for it.
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
finally {
|
|
111
|
+
clearTimeout(timer);
|
|
112
|
+
signal?.removeEventListener("abort", forward);
|
|
113
|
+
}
|
|
114
|
+
if (!res.ok || env.ok !== true) {
|
|
115
|
+
const status = res.status;
|
|
116
|
+
const desc = cleanDescription(env.description, this.token);
|
|
117
|
+
const msg = `Telegram ${method}: HTTP ${status}${desc ? `: ${desc}` : ""}`;
|
|
118
|
+
if (status === 401)
|
|
119
|
+
throw new TelegramError(msg, "unauthorized", method, status);
|
|
120
|
+
if (status === 409)
|
|
121
|
+
throw new TelegramError(msg, "conflict", method, status);
|
|
122
|
+
if (status === 429)
|
|
123
|
+
throw new TelegramError(msg, "rate_limited", method, status, env.parameters?.retry_after);
|
|
124
|
+
throw new TelegramError(msg, res.ok ? "api" : "http", method, status);
|
|
125
|
+
}
|
|
126
|
+
return env.result;
|
|
127
|
+
}
|
|
128
|
+
getMe() {
|
|
129
|
+
return this.call("getMe");
|
|
130
|
+
}
|
|
131
|
+
getWebhookInfo() {
|
|
132
|
+
return this.call("getWebhookInfo");
|
|
133
|
+
}
|
|
134
|
+
/** Long poll; `timeoutS` is Telegram's server-side wait. */
|
|
135
|
+
getUpdates(opts = {}) {
|
|
136
|
+
const body = { timeout: opts.timeoutS ?? 0, allowed_updates: ["message", "callback_query"] };
|
|
137
|
+
if (opts.offset !== undefined)
|
|
138
|
+
body.offset = opts.offset;
|
|
139
|
+
return this.call("getUpdates", body, opts.signal);
|
|
140
|
+
}
|
|
141
|
+
/** Send plain text, optionally with inline buttons. */
|
|
142
|
+
sendMessage(chatId, text, opts = {}) {
|
|
143
|
+
const body = { chat_id: chatId, text, disable_web_page_preview: true };
|
|
144
|
+
if (opts.buttons && opts.buttons.length > 0)
|
|
145
|
+
body.reply_markup = keyboardMarkup(opts.buttons);
|
|
146
|
+
return this.call("sendMessage", body);
|
|
147
|
+
}
|
|
148
|
+
answerCallbackQuery(id, opts = {}) {
|
|
149
|
+
const body = { callback_query_id: id };
|
|
150
|
+
if (opts.text)
|
|
151
|
+
body.text = opts.text;
|
|
152
|
+
if (opts.showAlert)
|
|
153
|
+
body.show_alert = true;
|
|
154
|
+
return this.call("answerCallbackQuery", body);
|
|
155
|
+
}
|
|
156
|
+
/** Replace a message's buttons; an empty or missing keyboard removes them. */
|
|
157
|
+
editMessageReplyMarkup(chatId, messageId, buttons) {
|
|
158
|
+
return this.call("editMessageReplyMarkup", {
|
|
159
|
+
chat_id: chatId,
|
|
160
|
+
message_id: messageId,
|
|
161
|
+
reply_markup: keyboardMarkup(buttons ?? []),
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
function keyboardMarkup(buttons) {
|
|
166
|
+
return { inline_keyboard: buttons.map((row) => row.map((b) => ({ text: b.text, callback_data: b.data }))) };
|
|
167
|
+
}
|
|
168
|
+
//# sourceMappingURL=telegram.js.map
|