@goodandready/dsh-cron 0.3.43 → 0.3.44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/lib/client.js +998 -3987
  2. package/lib/llm-ask.js +2 -1
  3. package/lib/routes/api-actions.js +36 -6
  4. package/lib/routes/api-item.js +1 -1
  5. package/lib/routes/api-webhook.js +2 -4
  6. package/lib/runtimes/runner-external.js +2 -3
  7. package/lib/runtimes/runner-session.js +385 -0
  8. package/lib/runtimes/runner-turn.js +255 -0
  9. package/lib/runtimes/runner.js +43 -616
  10. package/lib/scheduler/scheduler-execution.js +4 -1
  11. package/lib/scheduler/scheduler-lifecycle.js +65 -0
  12. package/lib/scheduler/scheduler-parser.js +261 -0
  13. package/lib/scheduler/scheduler.js +13 -319
  14. package/lib/store/store-archive.js +195 -0
  15. package/lib/store/store-io.js +331 -0
  16. package/lib/store/store-loader.js +165 -0
  17. package/lib/store/store-migration.js +38 -0
  18. package/lib/store/store-settings.js +126 -0
  19. package/lib/store/store-stats.js +55 -0
  20. package/lib/store/store.js +75 -888
  21. package/package.json +8 -6
  22. package/lib/client-src/00-open.js +0 -63
  23. package/lib/client-src/10-locales.js +0 -540
  24. package/lib/client-src/20-styles.js +0 -64
  25. package/lib/client-src/30-utils.js +0 -372
  26. package/lib/client-src/40-delivery-settings.js +0 -96
  27. package/lib/client-src/45-dag-graph-view.js +0 -287
  28. package/lib/client-src/46-live-logs-modal.js +0 -108
  29. package/lib/client-src/47-analytics-digest-modal.js +0 -166
  30. package/lib/client-src/48-timeline-heatmap-view.js +0 -176
  31. package/lib/client-src/50-cron-screen-state.js +0 -457
  32. package/lib/client-src/52-cron-screen-actions.js +0 -330
  33. package/lib/client-src/53-cron-screen-modal-actions.js +0 -677
  34. package/lib/client-src/54-cron-screen-analytics-actions.js +0 -68
  35. package/lib/client-src/55-cron-screen-view.js +0 -378
  36. package/lib/client-src/60-modal-task-form-head.js +0 -581
  37. package/lib/client-src/62-modal-task-form-tail.js +0 -563
  38. package/lib/client-src/65-modal-dialogs.js +0 -350
  39. package/lib/client-src/70-settings-card.js +0 -453
  40. package/lib/client-src/75-quick-action-bar.js +0 -146
  41. package/lib/client-src/80-sidebar-jobs.js +0 -200
  42. package/lib/client-src/90-close.js +0 -219
@@ -0,0 +1,38 @@
1
+ import path from 'node:path';
2
+
3
+ export const CURRENT_SCHEMA_VERSION = 2;
4
+
5
+ export function runStoreMigrations(data, fromVersion = 1, toVersion = CURRENT_SCHEMA_VERSION) {
6
+ if (!data || typeof data !== 'object') return data;
7
+ let current = { ...data };
8
+
9
+ // Migration from version 1 to 2 (Issue #390 & #393)
10
+ if (fromVersion < 2 && toVersion >= 2) {
11
+ if (Array.isArray(current.tasks)) {
12
+ current.tasks = current.tasks.map((task) => {
13
+ if (!task || typeof task !== 'object') return task;
14
+ return {
15
+ ...task,
16
+ maxMemoryMb: task.maxMemoryMb !== undefined ? Number(task.maxMemoryMb) : 0,
17
+ executionTimeoutMs: task.executionTimeoutMs !== undefined
18
+ ? Number(task.executionTimeoutMs)
19
+ : (Number(task.timeoutSeconds) > 0 ? Number(task.timeoutSeconds) * 1000 : 900000),
20
+ maxLogBytesPerRun: task.maxLogBytesPerRun !== undefined ? Number(task.maxLogBytesPerRun) : (5 * 1024 * 1024),
21
+ cpus: task.cpus !== undefined ? Number(task.cpus) : 0,
22
+ maxArchiveMonths: task.maxArchiveMonths !== undefined ? Number(task.maxArchiveMonths) : 6,
23
+ };
24
+ });
25
+ }
26
+ current.schemaVersion = 2;
27
+ }
28
+
29
+ return current;
30
+ }
31
+
32
+ export function getDefaultStorePath() {
33
+ const base = process.env.DSH_DATA_DIR
34
+ || (process.env.DSH_HOME
35
+ ? path.join(process.env.DSH_HOME, 'data')
36
+ : path.join(process.env.HOME || '.', '.dsh', 'data'));
37
+ return path.join(base, 'cron', 'tasks.json');
38
+ }
@@ -0,0 +1,126 @@
1
+ import { logger } from '../logger.js';
2
+ import { getDshDefaultTelegramCredentials } from '../telegram.js';
3
+
4
+ /** Mask a secret for transport to the browser, keeping a readable hint. */
5
+ export function maskSecretValue(value) {
6
+ const s = String(value == null ? '' : value);
7
+ if (!s) return '';
8
+ if (s.length <= 8) return '••••••••';
9
+ return s.slice(0, 4) + '••••••••' + s.slice(-3);
10
+ }
11
+
12
+ /**
13
+ * Raw secret values that must not be stored in plugin settings; the
14
+ * corresponding *Ref credential names are the supported way to configure
15
+ * these channels.
16
+ */
17
+ export const FORBIDDEN_SETTING_KEYS = ['giteaToken', 'ntfyToken', 'pushplusToken', 'discordToken', 'slackToken'];
18
+
19
+ /**
20
+ * Settings that hold a secret and are therefore masked on read: tokens and
21
+ * webhook URLs (the URL embeds the auth token), plus the Bark device key.
22
+ * These are stored in the plugin settings file; the credential-reference
23
+ * fields (botTokenRef, ntfyTokenRef, …) take precedence over them.
24
+ */
25
+ export const MASKED_SETTING_KEYS = ['botToken', 'discordWebhookUrl', 'slackWebhookUrl', 'barkKey', 'apiToken', 'telegramWebhookSecret'];
26
+
27
+ export function getSettings(store) {
28
+ const defaults = getDshDefaultTelegramCredentials();
29
+ const base = { ...store.settings };
30
+ base.botToken = base.botToken !== undefined ? base.botToken : (defaults.botToken || '');
31
+ base.botTokenRef = base.botTokenRef || '';
32
+ base.chatId = base.chatId !== undefined ? base.chatId : (defaults.chatId || '');
33
+ base.notifyTelegram = Boolean(base.notifyTelegram);
34
+ base.onlyOnFailure = Boolean(base.onlyOnFailure);
35
+ base.kanbanBaseUrl = base.kanbanBaseUrl || 'http://127.0.0.1:3000';
36
+ base.hasDefaultCredentials = Boolean(defaults.botToken && defaults.chatId);
37
+ base.llmActionsEnabled = Boolean(base.llmActionsEnabled);
38
+ base.showChatHeaderButton = base.showChatHeaderButton !== undefined ? Boolean(base.showChatHeaderButton) : true;
39
+ base.showChatHeaderActions = base.showChatHeaderActions !== undefined ? Boolean(base.showChatHeaderActions) : true;
40
+ return base;
41
+ }
42
+
43
+ export function getClientSettings(store) {
44
+ const raw = getSettings(store);
45
+ const isCustom = Boolean(store.settings.botToken);
46
+ const hasToken = Boolean(raw.botToken);
47
+ const masked = hasToken ? (raw.botToken.length > 8 ? raw.botToken.slice(0, 4) + '••••••••' + raw.botToken.slice(-3) : '••••••••') : '';
48
+ const { botToken, ...rest } = raw;
49
+ const out = {
50
+ ...rest,
51
+ botToken: masked,
52
+ botTokenMasked: masked,
53
+ hasBotToken: hasToken,
54
+ hasCustomBotToken: isCustom,
55
+ };
56
+ // Webhook URLs and the Bark device key carry an embedded secret, so they
57
+ // are never sent to the browser in clear text either.
58
+ for (const key of MASKED_SETTING_KEYS) {
59
+ if (key === 'botToken') continue;
60
+ out[key] = maskSecretValue(raw[key]);
61
+ }
62
+ return out;
63
+ }
64
+
65
+ export function saveSettings(store, newSettings = {}) {
66
+ const patch = { ...newSettings };
67
+
68
+ for (const key of FORBIDDEN_SETTING_KEYS) {
69
+ if (key in patch) {
70
+ logger.warn(`[dsh-cron] refusing to store raw secret "${key}" in settings — use a credential reference instead`);
71
+ delete patch[key];
72
+ }
73
+ }
74
+
75
+ let botTokenToSave = store.settings.botToken || '';
76
+ if (patch.botToken !== undefined) {
77
+ const val = String(patch.botToken).trim();
78
+ // If client sends masked token or empty string while custom token existed, don't overwrite with dots
79
+ if (val && !val.includes('••') && !val.includes('****')) {
80
+ botTokenToSave = val;
81
+ } else if (val === '') {
82
+ botTokenToSave = '';
83
+ }
84
+ delete patch.botToken;
85
+ }
86
+
87
+ // A masked value echoed back by the UI must never overwrite the real one.
88
+ for (const key of MASKED_SETTING_KEYS) {
89
+ if (key === 'botToken') continue;
90
+ const val = patch[key];
91
+ if (typeof val === 'string' && (val.includes('••') || val.includes('****'))) {
92
+ delete patch[key];
93
+ }
94
+ }
95
+
96
+ for (const [key, value] of Object.entries(patch)) {
97
+ if (value === undefined) delete patch[key];
98
+ else if (typeof value === 'string') patch[key] = value.trim();
99
+ }
100
+
101
+ const prevSettings = store.settings;
102
+ store.settings = {
103
+ ...store.settings,
104
+ ...patch,
105
+ botToken: botTokenToSave,
106
+ kanbanBaseUrl: patch.kanbanBaseUrl !== undefined
107
+ ? (patch.kanbanBaseUrl || 'http://127.0.0.1:3000')
108
+ : (store.settings.kanbanBaseUrl || 'http://127.0.0.1:3000'),
109
+ notifyTelegram: patch.notifyTelegram !== undefined ? Boolean(patch.notifyTelegram) : Boolean(store.settings.notifyTelegram),
110
+ onlyOnFailure: patch.onlyOnFailure !== undefined ? Boolean(patch.onlyOnFailure) : Boolean(store.settings.onlyOnFailure),
111
+ llmActionsEnabled: patch.llmActionsEnabled !== undefined ? Boolean(patch.llmActionsEnabled) : Boolean(store.settings.llmActionsEnabled),
112
+ showChatHeaderButton: patch.showChatHeaderButton !== undefined ? Boolean(patch.showChatHeaderButton) : (store.settings.showChatHeaderButton !== undefined ? Boolean(store.settings.showChatHeaderButton) : true),
113
+ showChatHeaderActions: patch.showChatHeaderActions !== undefined ? Boolean(patch.showChatHeaderActions) : (store.settings.showChatHeaderActions !== undefined ? Boolean(store.settings.showChatHeaderActions) : true),
114
+ };
115
+ if (store._readOnly || store._loadFailed) {
116
+ logger.warn(`[dsh-cron] store is in read-only recovery mode; updated in-memory settings but skipped writing to "${store.filePath}".`);
117
+ return getSettings(store);
118
+ }
119
+ try {
120
+ store.save();
121
+ } catch (err) {
122
+ store.settings = prevSettings;
123
+ throw err;
124
+ }
125
+ return getSettings(store);
126
+ }
@@ -0,0 +1,55 @@
1
+ export function getTaskStats(store, id) {
2
+ const runs = store.history.get(id) || [];
3
+ const total = runs.length;
4
+ let success = 0;
5
+ let error = 0;
6
+ let timeout = 0;
7
+ const recentDurations = [];
8
+
9
+ for (let i = 0; i < runs.length; i++) {
10
+ const r = runs[i];
11
+ if (r.status === 'success') success++;
12
+ else if (r.status === 'error') error++;
13
+ else if (r.status === 'timeout') timeout++;
14
+
15
+ if (recentDurations.length < 10 && r.durationMs !== undefined) {
16
+ recentDurations.push({
17
+ at: r.at,
18
+ durationMs: r.durationMs,
19
+ status: r.status,
20
+ });
21
+ }
22
+ }
23
+
24
+ const successRate = total > 0 ? Number((success / total).toFixed(2)) : 1.0;
25
+ return {
26
+ totalRuns: total,
27
+ successRuns: success,
28
+ errorRuns: error,
29
+ timeoutRuns: timeout,
30
+ successRate,
31
+ recentDurations,
32
+ };
33
+ }
34
+
35
+ export function getAggregatedStats(store) {
36
+ let totalRuns = 0;
37
+ let totalTokens = 0;
38
+ let totalCostUsd = 0;
39
+ let activeTasks = 0;
40
+
41
+ for (const t of store.tasks.values()) {
42
+ if (t.status === 'active') activeTasks++;
43
+ totalTokens += (t.totalTokens || 0);
44
+ totalCostUsd += (t.totalCostUsd || 0);
45
+ const h = store.history.get(t.id) || [];
46
+ totalRuns += h.length;
47
+ }
48
+
49
+ return {
50
+ activeTasks,
51
+ totalRuns,
52
+ totalTokens,
53
+ totalCostUsd: Number(totalCostUsd.toFixed(4)),
54
+ };
55
+ }