@wirecat/cli-core 0.613.0 → 0.615.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/CHANGELOG.md +41 -0
- package/dist/background/report/render.d.ts +18 -4
- package/dist/background/report/render.js +59 -18
- package/dist/background/report/stored-templates.d.ts +64 -0
- package/dist/background/report/stored-templates.js +80 -0
- package/dist/background/report/templates/morning-digest.sample.json +150 -0
- package/dist/background/schedules.d.ts +4 -2
- package/dist/background/schedules.js +2 -1
- package/dist/calendar/mcp/index.d.ts +12 -12
- package/dist/cli/account-remove.d.ts +67 -0
- package/dist/cli/account-remove.js +285 -0
- package/dist/cli/index.d.ts +3 -1
- package/dist/cli/index.js +2 -1
- package/dist/cli/messenger/account-command.js +6 -0
- package/dist/cli/messenger/context.d.ts +2 -0
- package/dist/cli/messenger/serve-relay.d.ts +1 -0
- package/dist/cli/profile-files.d.ts +7 -0
- package/dist/cli/profile-files.js +43 -0
- package/dist/cli/schedule/schedule-command.d.ts +2 -0
- package/dist/cli/schedule/schedule-command.js +20 -2
- package/dist/cli/shared-command-exports.d.ts +1 -0
- package/dist/cli/shared-command-exports.js +2 -0
- package/dist/cli/templates-command.d.ts +16 -0
- package/dist/cli/templates-command.js +243 -0
- package/dist/entities/account-change.d.ts +20 -0
- package/dist/entities/account-change.js +12 -1
- package/dist/entities/index.d.ts +20 -0
- package/dist/entities/index.js +2 -1
- package/dist/errors/reasons.js +17 -0
- package/dist/services/metadata.d.ts +1 -1
- package/dist/store/accounts.d.ts +2 -0
- package/dist/store/accounts.js +2 -1
- package/dist/store/exports.d.ts +1 -0
- package/dist/store/exports.js +1 -0
- package/dist/store/index.d.ts +1 -0
- package/dist/store/sqlite/account-groups.d.ts +1 -0
- package/dist/store/sqlite/account-groups.js +14 -0
- package/dist/store/sqlite/manifest.js +1 -0
- package/dist/store/sqlite/migrations.generated.js +8 -0
- package/dist/store/sqlite/schema/templates.d.ts +242 -0
- package/dist/store/sqlite/schema/templates.js +23 -0
- package/dist/store/sqlite/schema.d.ts +1 -0
- package/dist/store/sqlite/schema.js +1 -0
- package/dist/store/sqlite/templates.d.ts +38 -0
- package/dist/store/sqlite/templates.js +69 -0
- package/dist/store/store.d.ts +3 -0
- package/dist/store/store.js +5 -0
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,47 @@ Notable changes to `@wirecat/cli-core` (`@wirecat/cli-messaging` up to 0.261.0,
|
|
|
5
5
|
minor version may break callers, and says how under "Changed — may break callers". `pnpm docs:check` checks the
|
|
6
6
|
shape of this file.
|
|
7
7
|
|
|
8
|
+
## 0.614.0 — 08.11.2026
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- **The owner's own message templates, in the shared store (store version 59).** `templatesCommand(app, { env,
|
|
13
|
+
output, diagnostic })` gives a tool `templates list | show | edit | preview | reset <key> --channel
|
|
14
|
+
telegram|email|text [--language <code>]`. Keys: `report/morning-digest`; `report/weekly-gaps` and
|
|
15
|
+
`notice/schedule-failed` are listed, but nothing renders them yet, so they cannot be edited. `edit` opens
|
|
16
|
+
`$VISUAL`/`$EDITOR` on the template that applies, or takes `--file`; a save is checked first against a synthetic
|
|
17
|
+
report and the tool's last real `<run>.report.json`: it must render, the channel's rules may remove nothing (a tag
|
|
18
|
+
outside the list, an `href` that is not a link of the report, a `style` with `url(`), and a Telegram text must fit
|
|
19
|
+
4 096 characters once the size fit dropped what it could. A failed check saves nothing and says why, and keeps an
|
|
20
|
+
editor draft for `--file`. Every save and reset keeps the replaced body in `templates_history`; `edit --undo` puts
|
|
21
|
+
back the last one. `reset` deletes the row, so the shipped template applies again. `edit` and `reset` change what
|
|
22
|
+
later runs send (`grantsAuthority`), and an agent is refused (`template.owner-only`). A scheduled report renders
|
|
23
|
+
with the stored templates of the owner's language; a stored body that fails, or a store that cannot be read,
|
|
24
|
+
falls back to the shipped template for that channel and records `report.template-failed`. The email template is
|
|
25
|
+
stored and previewed, but mail still sends the plain-text rendering. `store.templates` and
|
|
26
|
+
`storedTemplateBodies(database, key, language)` from `@wirecat/cli-core/store`.
|
|
27
|
+
- **`account remove`, built once for every tool.** `<tool> <profile> account remove --keep-data|--delete-data` signs
|
|
28
|
+
the profile out with the tool's own `signOut` hook, then forgets it here: its keyring entries, its
|
|
29
|
+
`state/accounts/<profile>.json`, the tool's own state files and its sections in the config file; other profiles stay.
|
|
30
|
+
`--delete-data` also purges the account from the shared store (`store.accounts.purge`) and deletes the tool's data
|
|
31
|
+
files; `--keep-data` leaves its messages in the store, still searchable, and says so under `kept`. It asks first, or
|
|
32
|
+
`--yes`; under `--json` without `--yes` it answers `confirmation_required`; `--dry-run` lists every entry, section and
|
|
33
|
+
file. It refuses while a serve or a sync holds the profile, a path outside the tool's own folders, and runs neither
|
|
34
|
+
over MCP nor under `<PREFIX>_READ_ONLY`. The answer is the entity `account_removal`; reasons `account-remove.*`.
|
|
35
|
+
A messenger opts in with `Messenger.accountRemoval`; any other CLI mounts `accountRemoveCommand(app, hooks)`
|
|
36
|
+
([guide](docs/dev/ACCOUNT-REMOVAL.md)). `store.accounts.forgetProfile` forgets which account a profile was.
|
|
37
|
+
It also removes what cli-core keeps for the profile, so no tool lists it: its flood, pace, recipients, moderation,
|
|
38
|
+
provider log, MCP HTTP tokens, replies state and rules, `--new` points and checkpoints, its serve unit (by its exact
|
|
39
|
+
name) and the schedules whose environment names it. The sends journal stays, under `kept`. A tool whose files live
|
|
40
|
+
outside its cli-core folders names them with the `ownFolders` hook; each must be absolute and inside the home folder
|
|
41
|
+
(`account-remove.folder-outside`).
|
|
42
|
+
|
|
43
|
+
### Fixed
|
|
44
|
+
|
|
45
|
+
- **cli-core accepts any `@wirecat/cli-base` 0.x from 0.80.7 (`>=0.80.7 <1.0.0`).** Its peer range stopped at
|
|
46
|
+
`<0.86.0`, so a CLI could not take cli-base 0.86.0 or 0.87.0 beside it. The range is pinned again at 1.0. Nothing
|
|
47
|
+
else changes for a caller.
|
|
48
|
+
|
|
8
49
|
## 0.612.0 — 08.11.2026
|
|
9
50
|
|
|
10
51
|
### Changed — may break callers
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type LinkConfig } from "../../domain/links.js";
|
|
2
|
+
import { type ReportKind } from "./schema.js";
|
|
2
3
|
import { type ReportView } from "./view.js";
|
|
3
4
|
export declare const REPORT_CHANNELS: readonly ["telegram", "email", "text"];
|
|
4
5
|
export type ReportChannel = (typeof REPORT_CHANNELS)[number];
|
|
@@ -11,12 +12,13 @@ export declare const REPORT_REASONS: {
|
|
|
11
12
|
export declare const defaultTemplate: (kind: string, channel: ReportChannel, language: string) => string;
|
|
12
13
|
/**
|
|
13
14
|
* What a template's HTML may keep, enforced after it rendered: the channel's tags and attributes only, a `style`
|
|
14
|
-
* with no `url(`, and an `href` only to a URL of the view model. Anything else goes, its text kept; `
|
|
15
|
-
* what went.
|
|
15
|
+
* with no `url(`, and an `href` only to a URL of the view model. Anything else goes, its text kept; `removed` names
|
|
16
|
+
* what went, once per occurrence, and `changed` counts it.
|
|
16
17
|
*/
|
|
17
18
|
export declare const sanitizeHtml: (channel: Exclude<ReportChannel, "text">, html: string, urls: ReadonlySet<string>) => {
|
|
18
19
|
html: string;
|
|
19
20
|
changed: number;
|
|
21
|
+
removed: string[];
|
|
20
22
|
};
|
|
21
23
|
export interface Rendering {
|
|
22
24
|
text: string;
|
|
@@ -32,6 +34,17 @@ export interface Rendering {
|
|
|
32
34
|
export declare const renderReport: (view: ReportView, channel: ReportChannel, { template }?: {
|
|
33
35
|
template?: string;
|
|
34
36
|
}) => Promise<Rendering>;
|
|
37
|
+
/**
|
|
38
|
+
* Why `source` may not be saved for this view, empty when it may, and what it renders: it must render, the platform
|
|
39
|
+
* rules may take nothing out, and a Telegram text must fit once the size fit dropped what it could. Unlike
|
|
40
|
+
* `renderReport` it never falls back to the shipped template.
|
|
41
|
+
*/
|
|
42
|
+
export declare const checkTemplate: (view: ReportView, channel: ReportChannel, source: string) => Promise<{
|
|
43
|
+
problems: string[];
|
|
44
|
+
text: string;
|
|
45
|
+
}>;
|
|
46
|
+
/** The owner's stored templates of one report kind and language, by channel; a missing channel uses the shipped one. */
|
|
47
|
+
export type StoredTemplates = (kind: ReportKind, language: string) => Promise<Partial<Record<ReportChannel, string>>>;
|
|
35
48
|
/** What `last.json` keeps of a report run, beside the deliveries. */
|
|
36
49
|
export interface ReportRun {
|
|
37
50
|
language: string;
|
|
@@ -59,11 +72,12 @@ export interface PreparedReport {
|
|
|
59
72
|
* A run's answer rendered for every route, or `undefined` when it is not a report. An invalid report becomes one
|
|
60
73
|
* line in the owner's language and is never shown raw.
|
|
61
74
|
*/
|
|
62
|
-
export declare const prepareReport: (answer: string, { language, now, timeZone, templates, links, env, }: {
|
|
75
|
+
export declare const prepareReport: (answer: string, { language, now, timeZone, templates: storedTemplates, links, env, }: {
|
|
63
76
|
language: string;
|
|
64
77
|
now: number;
|
|
65
78
|
timeZone?: string;
|
|
66
|
-
templates
|
|
79
|
+
/** A lookup that fails delivers with the shipped templates and records `report.template-failed`. */
|
|
80
|
+
templates?: StoredTemplates;
|
|
67
81
|
links?: LinkConfig;
|
|
68
82
|
/** Read for `WIRECAT_LINKS=off`. */
|
|
69
83
|
env?: NodeJS.ProcessEnv;
|
|
@@ -48,20 +48,20 @@ const linkUrls = (view) => new Set([
|
|
|
48
48
|
].map((link) => link.url));
|
|
49
49
|
/**
|
|
50
50
|
* What a template's HTML may keep, enforced after it rendered: the channel's tags and attributes only, a `style`
|
|
51
|
-
* with no `url(`, and an `href` only to a URL of the view model. Anything else goes, its text kept; `
|
|
52
|
-
* what went.
|
|
51
|
+
* with no `url(`, and an `href` only to a URL of the view model. Anything else goes, its text kept; `removed` names
|
|
52
|
+
* what went, once per occurrence, and `changed` counts it.
|
|
53
53
|
*/
|
|
54
54
|
export const sanitizeHtml = (channel, html, urls) => {
|
|
55
55
|
const allowed = ALLOWED[channel];
|
|
56
|
-
|
|
56
|
+
const removed = [];
|
|
57
57
|
const dropped = [];
|
|
58
|
-
const withoutBlocks = html.replace(/<(script|style|iframe|object)\b[\s\S]*?(<\/\1\s*>|$)/gi, () => {
|
|
59
|
-
|
|
58
|
+
const withoutBlocks = html.replace(/<(script|style|iframe|object)\b[\s\S]*?(<\/\1\s*>|$)/gi, (_, name) => {
|
|
59
|
+
removed.push(`<${String(name).toLowerCase()}> and its content`);
|
|
60
60
|
return "";
|
|
61
61
|
});
|
|
62
62
|
const out = withoutBlocks.replace(/<(\/?)([a-zA-Z][a-zA-Z0-9]*)\b([^<>]*)>|[<>]/g, (tag, close, name, rest) => {
|
|
63
63
|
if (name === undefined) {
|
|
64
|
-
|
|
64
|
+
removed.push(`a bare ${tag}`);
|
|
65
65
|
return escapeHtml(tag);
|
|
66
66
|
}
|
|
67
67
|
const lower = String(name).toLowerCase();
|
|
@@ -70,26 +70,26 @@ export const sanitizeHtml = (channel, html, urls) => {
|
|
|
70
70
|
if (lower === "a" && dropped.pop())
|
|
71
71
|
return "";
|
|
72
72
|
if (!attributes)
|
|
73
|
-
|
|
73
|
+
removed.push(`</${lower}>`);
|
|
74
74
|
return attributes ? `</${lower}>` : "";
|
|
75
75
|
}
|
|
76
76
|
if (!attributes) {
|
|
77
|
-
|
|
77
|
+
removed.push(`<${lower}>`);
|
|
78
78
|
return "";
|
|
79
79
|
}
|
|
80
80
|
const kept = [];
|
|
81
81
|
const all = [...String(rest).matchAll(/([a-zA-Z-]+)\s*=\s*"([^"]*)"/g)];
|
|
82
82
|
if (all.length !== (String(rest).match(/=/g)?.length ?? 0))
|
|
83
|
-
|
|
83
|
+
removed.push(`an unquoted attribute on <${lower}>`);
|
|
84
84
|
for (const [, key = "", value = ""] of all) {
|
|
85
85
|
const attribute = key.toLowerCase();
|
|
86
86
|
const unescaped = value.replace(/&/g, "&");
|
|
87
87
|
if (!attributes.includes(attribute))
|
|
88
|
-
|
|
88
|
+
removed.push(`${attribute} on <${lower}>`);
|
|
89
89
|
else if (attribute === "href" && !urls.has(unescaped))
|
|
90
|
-
|
|
90
|
+
removed.push(`an href that is not a link of the report`);
|
|
91
91
|
else if (attribute === "style" && /url\(|expression\(|javascript:/i.test(value))
|
|
92
|
-
|
|
92
|
+
removed.push(`a style with url(), expression() or javascript:`);
|
|
93
93
|
else
|
|
94
94
|
kept.push(`${attribute}="${value}"`);
|
|
95
95
|
}
|
|
@@ -101,7 +101,7 @@ export const sanitizeHtml = (channel, html, urls) => {
|
|
|
101
101
|
}
|
|
102
102
|
return `<${lower}${kept.map((one) => ` ${one}`).join("")}>`;
|
|
103
103
|
});
|
|
104
|
-
return { html: out, changed };
|
|
104
|
+
return { html: out, changed: removed.length, removed };
|
|
105
105
|
};
|
|
106
106
|
const renderOnce = async (view, channel, source) => {
|
|
107
107
|
const engine = safeLiquid(LIMITS);
|
|
@@ -114,9 +114,9 @@ const renderOnce = async (view, channel, source) => {
|
|
|
114
114
|
if (!rendered)
|
|
115
115
|
throw new Error("empty");
|
|
116
116
|
if (channel === "text")
|
|
117
|
-
return { text: rendered, templateTags: 0 };
|
|
118
|
-
const { html, changed } = sanitizeHtml(channel, rendered, linkUrls(view));
|
|
119
|
-
return { text: html, templateTags: changed };
|
|
117
|
+
return { text: rendered, templateTags: 0, removed: [] };
|
|
118
|
+
const { html, changed, removed } = sanitizeHtml(channel, rendered, linkUrls(view));
|
|
119
|
+
return { text: html, templateTags: changed, removed };
|
|
120
120
|
};
|
|
121
121
|
const renderWithFallback = async (view, channel, template) => {
|
|
122
122
|
const shipped = () => renderOnce(view, channel, defaultTemplate(view.kind, channel, view.language));
|
|
@@ -163,6 +163,40 @@ export const renderReport = async (view, channel, { template } = {}) => {
|
|
|
163
163
|
current = smaller;
|
|
164
164
|
}
|
|
165
165
|
};
|
|
166
|
+
/**
|
|
167
|
+
* Why `source` may not be saved for this view, empty when it may, and what it renders: it must render, the platform
|
|
168
|
+
* rules may take nothing out, and a Telegram text must fit once the size fit dropped what it could. Unlike
|
|
169
|
+
* `renderReport` it never falls back to the shipped template.
|
|
170
|
+
*/
|
|
171
|
+
export const checkTemplate = async (view, channel, source) => {
|
|
172
|
+
let current = view;
|
|
173
|
+
for (;;) {
|
|
174
|
+
let rendering;
|
|
175
|
+
try {
|
|
176
|
+
rendering = await renderOnce(current, channel, source);
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
180
|
+
return { problems: [message === "empty" ? "it renders nothing" : `it does not render: ${message}`], text: "" };
|
|
181
|
+
}
|
|
182
|
+
if (rendering.removed.length > 0)
|
|
183
|
+
return {
|
|
184
|
+
problems: [...new Set(rendering.removed)].map((what) => `the ${channel} rules would remove ${what}`),
|
|
185
|
+
text: rendering.text,
|
|
186
|
+
};
|
|
187
|
+
if (channel !== "telegram" || rendering.text.length <= TELEGRAM_TEXT_CHARS)
|
|
188
|
+
return { problems: [], text: rendering.text };
|
|
189
|
+
const smaller = dropOne(current);
|
|
190
|
+
if (!smaller)
|
|
191
|
+
return {
|
|
192
|
+
problems: [
|
|
193
|
+
`it is ${rendering.text.length} characters with every item left out; Telegram takes ${TELEGRAM_TEXT_CHARS}`,
|
|
194
|
+
],
|
|
195
|
+
text: rendering.text,
|
|
196
|
+
};
|
|
197
|
+
current = smaller;
|
|
198
|
+
}
|
|
199
|
+
};
|
|
166
200
|
const INVALID_LINES = {
|
|
167
201
|
ru: "Сводка не собралась: ошибка формата. Подробности в WireCat.",
|
|
168
202
|
en: "The report could not be built: format error. Details in WireCat.",
|
|
@@ -171,7 +205,7 @@ const INVALID_LINES = {
|
|
|
171
205
|
* A run's answer rendered for every route, or `undefined` when it is not a report. An invalid report becomes one
|
|
172
206
|
* line in the owner's language and is never shown raw.
|
|
173
207
|
*/
|
|
174
|
-
export const prepareReport = async (answer, { language, now, timeZone, templates
|
|
208
|
+
export const prepareReport = async (answer, { language, now, timeZone, templates: storedTemplates, links = DEFAULT_LINK_CONFIG, env = process.env, }) => {
|
|
175
209
|
const parsed = parseReport(answer);
|
|
176
210
|
if (!parsed)
|
|
177
211
|
return undefined;
|
|
@@ -194,13 +228,20 @@ export const prepareReport = async (answer, { language, now, timeZone, templates
|
|
|
194
228
|
...(timeZone ? { timeZone } : {}),
|
|
195
229
|
linkOf: (ref) => linkFor(ref, { form, config: links, env }),
|
|
196
230
|
});
|
|
231
|
+
let lookupFailed = false;
|
|
232
|
+
const templates = storedTemplates
|
|
233
|
+
? await storedTemplates(parsed.report.kind, shipped).catch(() => {
|
|
234
|
+
lookupFailed = true;
|
|
235
|
+
return {};
|
|
236
|
+
})
|
|
237
|
+
: {};
|
|
197
238
|
const web = viewFor("web");
|
|
198
239
|
const telegram = await renderReport(web, "telegram", templateOf(templates, "telegram"));
|
|
199
240
|
const text = await renderReport(web, "text", templateOf(templates, "text"));
|
|
200
241
|
const terminal = await renderReport(viewFor("app"), "text", templateOf(templates, "text"));
|
|
201
242
|
const renderings = [telegram, text, terminal];
|
|
202
243
|
const reasons = [
|
|
203
|
-
...(renderings.some((one) => one.templateFailed) ? [REPORT_REASONS.templateFailed] : []),
|
|
244
|
+
...(lookupFailed || renderings.some((one) => one.templateFailed) ? [REPORT_REASONS.templateFailed] : []),
|
|
204
245
|
...(renderings.some((one) => one.templateTags > 0) ? [REPORT_REASONS.templateTags] : []),
|
|
205
246
|
];
|
|
206
247
|
const title = text.text.split("\n")[0] ?? "";
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { type ReportChannel } from "./render.js";
|
|
2
|
+
import { type ReportKind } from "./schema.js";
|
|
3
|
+
import { type ReportView } from "./view.js";
|
|
4
|
+
/** What an owner's template renders. A key with no `kind` has storage and commands, but nothing renders it yet. */
|
|
5
|
+
export declare const TEMPLATE_KEYS: readonly [{
|
|
6
|
+
readonly key: "report/morning-digest";
|
|
7
|
+
readonly kind: "morning-digest";
|
|
8
|
+
readonly about: "the morning digest";
|
|
9
|
+
}, {
|
|
10
|
+
readonly key: "report/weekly-gaps";
|
|
11
|
+
readonly kind: null;
|
|
12
|
+
readonly about: "the weekly list of what the digest could not check";
|
|
13
|
+
}, {
|
|
14
|
+
readonly key: "notice/schedule-failed";
|
|
15
|
+
readonly kind: null;
|
|
16
|
+
readonly about: "the message sent when a scheduled run fails";
|
|
17
|
+
}];
|
|
18
|
+
export type TemplateKey = (typeof TEMPLATE_KEYS)[number]["key"];
|
|
19
|
+
export declare const templateKeyOf: (key: string) => {
|
|
20
|
+
readonly key: "report/morning-digest";
|
|
21
|
+
readonly kind: "morning-digest";
|
|
22
|
+
readonly about: "the morning digest";
|
|
23
|
+
} | {
|
|
24
|
+
readonly key: "report/weekly-gaps";
|
|
25
|
+
readonly kind: null;
|
|
26
|
+
readonly about: "the weekly list of what the digest could not check";
|
|
27
|
+
} | {
|
|
28
|
+
readonly key: "notice/schedule-failed";
|
|
29
|
+
readonly kind: null;
|
|
30
|
+
readonly about: "the message sent when a scheduled run fails";
|
|
31
|
+
} | undefined;
|
|
32
|
+
export declare const reportKeyOf: (kind: ReportKind) => TemplateKey;
|
|
33
|
+
/** The languages cli-core ships a template in for `kind`: a stored template in another one would never be used. */
|
|
34
|
+
export declare const shippedLanguages: (kind: ReportKind) => string[];
|
|
35
|
+
/** The synthetic report shipped beside the templates: every section, item type and health reason once. */
|
|
36
|
+
export declare const sampleView: (kind: ReportKind, language: string, now: number) => ReportView;
|
|
37
|
+
/** The newest `<run>.report.json` of `kind` the tool's schedules wrote, or undefined. */
|
|
38
|
+
export declare const lastReport: (state: string, kind: ReportKind) => {
|
|
39
|
+
path: string;
|
|
40
|
+
document: string;
|
|
41
|
+
} | undefined;
|
|
42
|
+
export interface TemplateCheck {
|
|
43
|
+
/** `sample`, or the path of the last real report. */
|
|
44
|
+
against: string;
|
|
45
|
+
problems: string[];
|
|
46
|
+
text: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Whether `body` may be saved for one channel: it is rendered against the synthetic sample and against the last real
|
|
50
|
+
* report, when the tool has one, with the platform rules that run at send time. Any problem refuses the save.
|
|
51
|
+
*/
|
|
52
|
+
export declare const checkTemplateSave: ({ kind, channel, language, body, state, now, }: {
|
|
53
|
+
kind: ReportKind;
|
|
54
|
+
channel: ReportChannel;
|
|
55
|
+
language: string;
|
|
56
|
+
body: string;
|
|
57
|
+
state: string;
|
|
58
|
+
now: number;
|
|
59
|
+
}) => Promise<TemplateCheck[]>;
|
|
60
|
+
/** The view a preview renders: the last real report when there is one, else the sample. */
|
|
61
|
+
export declare const previewView: (state: string, kind: ReportKind, language: string, now: number) => {
|
|
62
|
+
against: string;
|
|
63
|
+
view: ReportView;
|
|
64
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { linkFor } from "../../domain/links.js";
|
|
5
|
+
import { schedulesDir } from "../schedules.js";
|
|
6
|
+
import { checkTemplate, REPORT_CHANNELS } from "./render.js";
|
|
7
|
+
import { parseReport } from "./schema.js";
|
|
8
|
+
import { reportView } from "./view.js";
|
|
9
|
+
/** What an owner's template renders. A key with no `kind` has storage and commands, but nothing renders it yet. */
|
|
10
|
+
export const TEMPLATE_KEYS = [
|
|
11
|
+
{ key: "report/morning-digest", kind: "morning-digest", about: "the morning digest" },
|
|
12
|
+
{ key: "report/weekly-gaps", kind: null, about: "the weekly list of what the digest could not check" },
|
|
13
|
+
{ key: "notice/schedule-failed", kind: null, about: "the message sent when a scheduled run fails" },
|
|
14
|
+
];
|
|
15
|
+
export const templateKeyOf = (key) => TEMPLATE_KEYS.find((one) => one.key === key);
|
|
16
|
+
export const reportKeyOf = (kind) => `report/${kind}`;
|
|
17
|
+
const TEMPLATES = fileURLToPath(new URL("./templates/", import.meta.url));
|
|
18
|
+
/** The languages cli-core ships a template in for `kind`: a stored template in another one would never be used. */
|
|
19
|
+
export const shippedLanguages = (kind) => [
|
|
20
|
+
...new Set(readdirSync(TEMPLATES)
|
|
21
|
+
.map((file) => file.match(new RegExp(`^${kind}\\.(?:${REPORT_CHANNELS.join("|")})\\.([a-z]{2})\\.liquid$`))?.[1])
|
|
22
|
+
.filter((one) => one !== undefined)),
|
|
23
|
+
].sort();
|
|
24
|
+
const viewOf = (document, language, now, timeZone) => {
|
|
25
|
+
const parsed = parseReport(document);
|
|
26
|
+
if (!parsed || "invalid" in parsed)
|
|
27
|
+
return undefined;
|
|
28
|
+
return reportView(parsed.report, {
|
|
29
|
+
language,
|
|
30
|
+
now,
|
|
31
|
+
...(timeZone ? { timeZone } : {}),
|
|
32
|
+
linkOf: (ref) => linkFor(ref, { form: "web", env: {} }),
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
/** The synthetic report shipped beside the templates: every section, item type and health reason once. */
|
|
36
|
+
export const sampleView = (kind, language, now) => {
|
|
37
|
+
const view = viewOf(readFileSync(join(TEMPLATES, `${kind}.sample.json`), "utf8"), language, now);
|
|
38
|
+
if (!view)
|
|
39
|
+
throw new Error(`the shipped ${kind} sample is not a report`);
|
|
40
|
+
return view;
|
|
41
|
+
};
|
|
42
|
+
/** The newest `<run>.report.json` of `kind` the tool's schedules wrote, or undefined. */
|
|
43
|
+
export const lastReport = (state, kind) => {
|
|
44
|
+
const folder = schedulesDir(state);
|
|
45
|
+
if (!existsSync(folder))
|
|
46
|
+
return undefined;
|
|
47
|
+
const files = readdirSync(folder).flatMap((name) => {
|
|
48
|
+
const runs = join(folder, name, "runs");
|
|
49
|
+
return existsSync(runs)
|
|
50
|
+
? readdirSync(runs)
|
|
51
|
+
.filter((file) => file.endsWith(".report.json"))
|
|
52
|
+
.map((file) => join(runs, file))
|
|
53
|
+
: [];
|
|
54
|
+
});
|
|
55
|
+
for (const path of files.sort((a, b) => statSync(b).mtimeMs - statSync(a).mtimeMs)) {
|
|
56
|
+
const document = readFileSync(path, "utf8");
|
|
57
|
+
const parsed = parseReport(document);
|
|
58
|
+
if (parsed && !("invalid" in parsed) && parsed.report.kind === kind)
|
|
59
|
+
return { path, document };
|
|
60
|
+
}
|
|
61
|
+
return undefined;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Whether `body` may be saved for one channel: it is rendered against the synthetic sample and against the last real
|
|
65
|
+
* report, when the tool has one, with the platform rules that run at send time. Any problem refuses the save.
|
|
66
|
+
*/
|
|
67
|
+
export const checkTemplateSave = async ({ kind, channel, language, body, state, now, }) => {
|
|
68
|
+
const last = lastReport(state, kind);
|
|
69
|
+
const views = [
|
|
70
|
+
["sample", sampleView(kind, language, now)],
|
|
71
|
+
...(last ? [[last.path, viewOf(last.document, language, now)]] : []),
|
|
72
|
+
];
|
|
73
|
+
return Promise.all(views.flatMap(([against, view]) => view ? [checkTemplate(view, channel, body).then((check) => ({ against, ...check }))] : []));
|
|
74
|
+
};
|
|
75
|
+
/** The view a preview renders: the last real report when there is one, else the sample. */
|
|
76
|
+
export const previewView = (state, kind, language, now) => {
|
|
77
|
+
const last = lastReport(state, kind);
|
|
78
|
+
const view = last && viewOf(last.document, language, now);
|
|
79
|
+
return view ? { against: last.path, view } : { against: "sample", view: sampleView(kind, language, now) };
|
|
80
|
+
};
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
{
|
|
2
|
+
"format": "wirecat.report/1",
|
|
3
|
+
"kind": "morning-digest",
|
|
4
|
+
"date": "2026-11-06",
|
|
5
|
+
"summary": "Two replies are waiting, one payment is overdue.",
|
|
6
|
+
"sections": [
|
|
7
|
+
{
|
|
8
|
+
"kind": "today",
|
|
9
|
+
"items": [
|
|
10
|
+
{
|
|
11
|
+
"type": "reply",
|
|
12
|
+
"who": "Alice Example",
|
|
13
|
+
"topic": "warehouse lease",
|
|
14
|
+
"action": "Say whether signing on Friday works",
|
|
15
|
+
"quote": "Can we sign on Friday?",
|
|
16
|
+
"since": "2026-11-04T14:20:00+03:00",
|
|
17
|
+
"channel": "mail",
|
|
18
|
+
"refs": ["msg:email/alice%40example.com/5005/6006"]
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"type": "question",
|
|
22
|
+
"who": "Bob Sample",
|
|
23
|
+
"topic": "tile delivery",
|
|
24
|
+
"action": "can the delivery move to the 20th?",
|
|
25
|
+
"since": "2026-11-05T19:00:00+03:00",
|
|
26
|
+
"channel": "telegram",
|
|
27
|
+
"refs": ["chat:telegram/1001/4004"]
|
|
28
|
+
},
|
|
29
|
+
{ "type": "task", "topic": "Reconciliation for Demo Ltd", "due": "2026-11-04", "refs": ["task:80"] },
|
|
30
|
+
{ "type": "task", "topic": "Office repair estimate", "due": "2026-11-06T18:00:00+03:00", "who": "Carol Test" },
|
|
31
|
+
{
|
|
32
|
+
"type": "task",
|
|
33
|
+
"topic": "Contractor agreement",
|
|
34
|
+
"who": "Dan Fixture",
|
|
35
|
+
"priority": "high",
|
|
36
|
+
"refs": ["task:81"]
|
|
37
|
+
}
|
|
38
|
+
],
|
|
39
|
+
"more": 1
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"kind": "decisions",
|
|
43
|
+
"items": [
|
|
44
|
+
{
|
|
45
|
+
"type": "decision",
|
|
46
|
+
"topic": "Findings from mail",
|
|
47
|
+
"action": "waiting for review",
|
|
48
|
+
"count": 12,
|
|
49
|
+
"refs": ["proposal:fact%3A512"]
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"kind": "money",
|
|
55
|
+
"items": [
|
|
56
|
+
{
|
|
57
|
+
"type": "money",
|
|
58
|
+
"who": "Demo Ltd",
|
|
59
|
+
"topic": "November rent",
|
|
60
|
+
"action": "pay",
|
|
61
|
+
"amount": { "value": 48000, "currency": "RUB", "direction": "out" },
|
|
62
|
+
"due": "2026-11-03",
|
|
63
|
+
"channel": "mail",
|
|
64
|
+
"refs": ["msg:email/7"]
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
"type": "money",
|
|
68
|
+
"who": "Sample LLC",
|
|
69
|
+
"topic": "prepayment, contract 14",
|
|
70
|
+
"amount": { "value": 1200, "currency": "EUR", "direction": "in" }
|
|
71
|
+
}
|
|
72
|
+
]
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
"kind": "meetings",
|
|
76
|
+
"items": [
|
|
77
|
+
{
|
|
78
|
+
"type": "meeting",
|
|
79
|
+
"topic": "Warehouse stand-up",
|
|
80
|
+
"time": "2026-11-06T11:00:00+03:00",
|
|
81
|
+
"who": "Alice Example, Dan Fixture",
|
|
82
|
+
"action": "bring the lease figures",
|
|
83
|
+
"channel": "meeting"
|
|
84
|
+
}
|
|
85
|
+
]
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
"kind": "projects",
|
|
89
|
+
"items": [
|
|
90
|
+
{
|
|
91
|
+
"type": "fact",
|
|
92
|
+
"topic": "Warehouse",
|
|
93
|
+
"detail": "the landlord agreed to 5% off from January",
|
|
94
|
+
"who": "Alice Example"
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
"type": "risk",
|
|
98
|
+
"topic": "Cable delivery",
|
|
99
|
+
"detail": "the supplier has not answered for 4 days",
|
|
100
|
+
"action": "call Dan Fixture before noon",
|
|
101
|
+
"priority": "high"
|
|
102
|
+
}
|
|
103
|
+
],
|
|
104
|
+
"more": 2
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
"kind": "mail",
|
|
108
|
+
"items": [
|
|
109
|
+
{
|
|
110
|
+
"type": "reply",
|
|
111
|
+
"who": "Erin Placeholder",
|
|
112
|
+
"topic": "invoice copy",
|
|
113
|
+
"action": "send the signed copy",
|
|
114
|
+
"since": "2026-11-05T09:00:00+03:00",
|
|
115
|
+
"channel": "mail",
|
|
116
|
+
"refs": ["msg:email/9"]
|
|
117
|
+
}
|
|
118
|
+
]
|
|
119
|
+
}
|
|
120
|
+
],
|
|
121
|
+
"health": [
|
|
122
|
+
{
|
|
123
|
+
"source": "Telegram",
|
|
124
|
+
"account": "@demo",
|
|
125
|
+
"reason": "sync-failed",
|
|
126
|
+
"since": "2026-11-04T23:10:00+03:00",
|
|
127
|
+
"stale_hours": 33,
|
|
128
|
+
"refs": ["sync:telegram/1001"]
|
|
129
|
+
},
|
|
130
|
+
{ "source": "MAX", "account": "test account", "reason": "no-schedule", "stale_hours": 0 },
|
|
131
|
+
{
|
|
132
|
+
"source": "Mail",
|
|
133
|
+
"account": "owner@example.com",
|
|
134
|
+
"reason": "quiet",
|
|
135
|
+
"stale_hours": 50,
|
|
136
|
+
"refs": ["account:max/1"]
|
|
137
|
+
}
|
|
138
|
+
],
|
|
139
|
+
"scope": {
|
|
140
|
+
"from": "2026-11-05T08:30:00+03:00",
|
|
141
|
+
"to": "2026-11-06T08:30:00+03:00",
|
|
142
|
+
"sources": [
|
|
143
|
+
{ "kind": "telegram", "accounts": 1 },
|
|
144
|
+
{ "kind": "max", "accounts": 1 },
|
|
145
|
+
{ "kind": "mail", "accounts": 2 }
|
|
146
|
+
],
|
|
147
|
+
"counts": { "chats": 31, "mail": 12, "tasks": 17, "meetings": 1 }
|
|
148
|
+
},
|
|
149
|
+
"tasks": { "overdue": 5, "today": 3, "tomorrow": 2, "new": 4, "new_since": "2026-11-05T08:30:00+03:00" }
|
|
150
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type AppIdentity } from "../ports/app.js";
|
|
2
2
|
import { type Delivery } from "./deliver.js";
|
|
3
|
-
import { type ReportRun } from "./report/render.js";
|
|
3
|
+
import { type ReportRun, type StoredTemplates } from "./report/render.js";
|
|
4
4
|
import type { ServerSystem } from "./system.js";
|
|
5
5
|
/** Minutes between runs, a daily local time, or one local moment. */
|
|
6
6
|
export type When = {
|
|
@@ -175,7 +175,7 @@ export declare const CREDENTIALS_UNREACHABLE = "credentials.unreachable";
|
|
|
175
175
|
* keyring, so a run that fails for that reason is tried again, a minute then up to 15 minutes apart, for up to
|
|
176
176
|
* 12 hours. A schedule that repeats every few minutes is left to its next tick; a run by hand fails at once.
|
|
177
177
|
*/
|
|
178
|
-
export declare const runSchedule: (app: AppIdentity, system: ServerSystem, env: NodeJS.ProcessEnv, file: string, { now, timer, signal, killAfterMs, untilUnlocked, wait, agentRuns, agentInput, ownerLanguage, }?: {
|
|
178
|
+
export declare const runSchedule: (app: AppIdentity, system: ServerSystem, env: NodeJS.ProcessEnv, file: string, { now, timer, signal, killAfterMs, untilUnlocked, wait, agentRuns, agentInput, ownerLanguage, reportTemplates, }?: {
|
|
179
179
|
now?: () => Date;
|
|
180
180
|
timer?: Timer;
|
|
181
181
|
signal?: AbortSignal;
|
|
@@ -187,4 +187,6 @@ export declare const runSchedule: (app: AppIdentity, system: ServerSystem, env:
|
|
|
187
187
|
agentInput?: AgentInput;
|
|
188
188
|
/** The owner's first language, read only when the answer is a report; Russian when the host cannot say. */
|
|
189
189
|
ownerLanguage?: (env: NodeJS.ProcessEnv) => Promise<string>;
|
|
190
|
+
/** The owner's stored report templates; the shipped ones when the host gives none. */
|
|
191
|
+
reportTemplates?: (env: NodeJS.ProcessEnv) => StoredTemplates;
|
|
190
192
|
}) => Promise<LastRun>;
|
|
@@ -443,7 +443,7 @@ const pause = (ms, signal) => new Promise((resolve) => {
|
|
|
443
443
|
* keyring, so a run that fails for that reason is tried again, a minute then up to 15 minutes apart, for up to
|
|
444
444
|
* 12 hours. A schedule that repeats every few minutes is left to its next tick; a run by hand fails at once.
|
|
445
445
|
*/
|
|
446
|
-
export const runSchedule = async (app, system, env, file, { now = () => new Date(), timer, signal, killAfterMs = KILL_AFTER_MS, untilUnlocked = false, wait = pause, agentRuns = [], agentInput, ownerLanguage = async () => "ru", } = {}) => {
|
|
446
|
+
export const runSchedule = async (app, system, env, file, { now = () => new Date(), timer, signal, killAfterMs = KILL_AFTER_MS, untilUnlocked = false, wait = pause, agentRuns = [], agentInput, ownerLanguage = async () => "ru", reportTemplates, } = {}) => {
|
|
447
447
|
const schedule = readSchedule(file);
|
|
448
448
|
const folder = join(file, "..");
|
|
449
449
|
const runs = join(folder, "runs");
|
|
@@ -481,6 +481,7 @@ export const runSchedule = async (app, system, env, file, { now = () => new Date
|
|
|
481
481
|
now: now().getTime(),
|
|
482
482
|
links: sharedLinks(runEnv),
|
|
483
483
|
env: runEnv,
|
|
484
|
+
...(reportTemplates ? { templates: reportTemplates(runEnv) } : {}),
|
|
484
485
|
})
|
|
485
486
|
: undefined;
|
|
486
487
|
const latestText = join(folder, "latest.txt");
|