@tidegap/core 0.18.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/agent-snippet.d.ts +30 -0
- package/dist/agent-snippet.js +120 -0
- package/dist/budget.d.ts +0 -12
- package/dist/budget.js +2 -24
- package/dist/channels/commands.d.ts +137 -0
- package/dist/channels/commands.js +377 -0
- package/dist/channels/delivery-codes.d.ts +9 -0
- package/dist/channels/delivery-codes.js +53 -0
- package/dist/channels/digest.d.ts +102 -0
- package/dist/channels/digest.js +146 -0
- package/dist/channels/format.d.ts +127 -0
- package/dist/channels/format.js +407 -0
- package/dist/channels/index.d.ts +15 -0
- package/dist/channels/index.js +15 -0
- package/dist/channels/kinds/command.d.ts +13 -0
- package/dist/channels/kinds/command.js +65 -0
- package/dist/channels/kinds/discord.d.ts +16 -0
- package/dist/channels/kinds/discord.js +67 -0
- package/dist/channels/kinds/email.d.ts +20 -0
- package/dist/channels/kinds/email.js +162 -0
- package/dist/channels/kinds/index.d.ts +8 -0
- package/dist/channels/kinds/index.js +17 -0
- package/dist/channels/kinds/ntfy.d.ts +25 -0
- package/dist/channels/kinds/ntfy.js +163 -0
- package/dist/channels/kinds/pushover.d.ts +11 -0
- package/dist/channels/kinds/pushover.js +77 -0
- package/dist/channels/kinds/shared.d.ts +25 -0
- package/dist/channels/kinds/shared.js +39 -0
- package/dist/channels/kinds/shortcuts.d.ts +9 -0
- package/dist/channels/kinds/shortcuts.js +45 -0
- package/dist/channels/kinds/slack.d.ts +8 -0
- package/dist/channels/kinds/slack.js +57 -0
- package/dist/channels/kinds/telegram.d.ts +4 -0
- package/dist/channels/kinds/telegram.js +26 -0
- package/dist/channels/kinds/webhook.d.ts +30 -0
- package/dist/channels/kinds/webhook.js +177 -0
- package/dist/channels/payload.d.ts +36 -0
- package/dist/channels/payload.js +61 -0
- package/dist/channels/plan.d.ts +24 -0
- package/dist/channels/plan.js +66 -0
- package/dist/channels/registry.d.ts +191 -0
- package/dist/channels/registry.js +125 -0
- package/dist/channels/telegram.d.ts +110 -0
- package/dist/channels/telegram.js +168 -0
- package/dist/channels/types.d.ts +107 -0
- package/dist/channels/types.js +26 -0
- package/dist/claude-tools.d.ts +29 -0
- package/dist/claude-tools.js +49 -0
- package/dist/data/tables.generated.d.ts +6 -0
- package/dist/data/tables.generated.js +37 -0
- package/dist/energy.d.ts +77 -0
- package/dist/energy.js +131 -0
- package/dist/grid.d.ts +19 -2
- package/dist/grid.js +84 -28
- package/dist/handoff.d.ts +152 -0
- package/dist/handoff.js +430 -0
- package/dist/history.d.ts +2 -0
- package/dist/history.js +3 -1
- package/dist/index.d.ts +46 -16
- package/dist/index.js +34 -8
- package/dist/job-cadence.d.ts +76 -0
- package/dist/job-cadence.js +240 -0
- package/dist/jobs.d.ts +173 -0
- package/dist/jobs.js +794 -0
- package/dist/notify.d.ts +2 -0
- package/dist/notify.js +2 -1
- package/dist/paths.d.ts +13 -46
- package/dist/paths.js +51 -219
- package/dist/postpone.d.ts +113 -0
- package/dist/postpone.js +185 -0
- package/dist/providers/anthropic.js +7 -3
- package/dist/providers/base.d.ts +113 -1
- package/dist/providers/base.js +15 -0
- package/dist/providers/budget.d.ts +25 -0
- package/dist/providers/budget.js +57 -0
- package/dist/providers/defaults.js +4 -0
- package/dist/providers/defer.d.ts +16 -0
- package/dist/providers/defer.js +31 -0
- package/dist/providers/gemini.d.ts +1 -0
- package/dist/providers/gemini.js +27 -20
- package/dist/providers/index.d.ts +2 -2
- package/dist/providers/index.js +1 -1
- package/dist/providers/ollama.d.ts +1 -0
- package/dist/providers/ollama.js +26 -22
- package/dist/providers/openai.js +5 -5
- package/dist/providers/timeout.d.ts +18 -0
- package/dist/providers/timeout.js +33 -0
- package/dist/quota.d.ts +7 -2
- package/dist/quota.js +13 -29
- package/dist/readiness.d.ts +169 -0
- package/dist/readiness.js +254 -0
- package/dist/redact.d.ts +42 -0
- package/dist/redact.js +110 -0
- package/dist/region-host.d.ts +30 -0
- package/dist/region-host.js +69 -0
- package/dist/region.d.ts +102 -20
- package/dist/region.js +175 -23
- package/dist/run-session.d.ts +36 -0
- package/dist/run-session.js +72 -0
- package/dist/schedule-job.d.ts +95 -0
- package/dist/schedule-job.js +195 -0
- package/dist/scheduler.d.ts +245 -14
- package/dist/scheduler.js +925 -110
- package/dist/sign.d.ts +8 -1
- package/dist/sign.js +49 -3
- package/dist/storage/sqlite.d.ts +157 -5
- package/dist/storage/sqlite.js +661 -20
- package/dist/task-events.d.ts +102 -0
- package/dist/task-events.js +308 -0
- package/dist/tool-surface.d.ts +31 -2
- package/dist/tool-surface.js +359 -65
- package/dist/types.d.ts +246 -3
- package/dist/types.js +8 -1
- package/dist/zone-time.d.ts +22 -0
- package/dist/zone-time.js +76 -0
- package/package.json +11 -6
- package/dist/aggregator.d.ts.map +0 -1
- package/dist/aggregator.js.map +0 -1
- package/dist/budget.d.ts.map +0 -1
- package/dist/budget.js.map +0 -1
- package/dist/data/tables.generated.d.ts.map +0 -1
- package/dist/data/tables.generated.js.map +0 -1
- package/dist/energy.d.ts.map +0 -1
- package/dist/energy.js.map +0 -1
- package/dist/grid.d.ts.map +0 -1
- package/dist/grid.js.map +0 -1
- package/dist/history.d.ts.map +0 -1
- package/dist/history.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/notify.d.ts.map +0 -1
- package/dist/notify.js.map +0 -1
- package/dist/paths.d.ts.map +0 -1
- package/dist/paths.js.map +0 -1
- package/dist/providers/anthropic.d.ts.map +0 -1
- package/dist/providers/anthropic.js.map +0 -1
- package/dist/providers/base.d.ts.map +0 -1
- package/dist/providers/base.js.map +0 -1
- package/dist/providers/defaults.d.ts.map +0 -1
- package/dist/providers/defaults.js.map +0 -1
- package/dist/providers/gemini.d.ts.map +0 -1
- package/dist/providers/gemini.js.map +0 -1
- package/dist/providers/index.d.ts.map +0 -1
- package/dist/providers/index.js.map +0 -1
- package/dist/providers/ollama.d.ts.map +0 -1
- package/dist/providers/ollama.js.map +0 -1
- package/dist/providers/openai.d.ts.map +0 -1
- package/dist/providers/openai.js.map +0 -1
- package/dist/quota.d.ts.map +0 -1
- package/dist/quota.js.map +0 -1
- package/dist/recommend.d.ts.map +0 -1
- package/dist/recommend.js.map +0 -1
- package/dist/redact.d.ts.map +0 -1
- package/dist/redact.js.map +0 -1
- package/dist/region.d.ts.map +0 -1
- package/dist/region.js.map +0 -1
- package/dist/routing.d.ts.map +0 -1
- package/dist/routing.js.map +0 -1
- package/dist/scheduler.d.ts.map +0 -1
- package/dist/scheduler.js.map +0 -1
- package/dist/select-window.d.ts.map +0 -1
- package/dist/select-window.js.map +0 -1
- package/dist/sign.d.ts.map +0 -1
- package/dist/sign.js.map +0 -1
- package/dist/storage/sqlite.d.ts.map +0 -1
- package/dist/storage/sqlite.js.map +0 -1
- package/dist/tool-surface.d.ts.map +0 -1
- package/dist/tool-surface.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
package/dist/region.js
CHANGED
|
@@ -2,56 +2,208 @@
|
|
|
2
2
|
* Grid-region resolution shared by every Tidegap integration — the CLI,
|
|
3
3
|
* the MCP server and the OpenClaw plugin.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
5
|
+
* Precedence (first match wins):
|
|
6
|
+
* 1. an explicit request region (a `--region` flag or a tool argument),
|
|
7
|
+
* 2. a host-configured default (`TIDEGAP_DEFAULT_REGION` in the
|
|
8
|
+
* environment, or OpenClaw's plugin `defaultRegion`),
|
|
9
|
+
* 3. the stored setting (`TIDEGAP_DEFAULT_REGION` in `~/.tidegap/config`),
|
|
10
|
+
* 4. a guess from the host machine's IANA time zone,
|
|
11
|
+
* 5. FALLBACK_REGION.
|
|
12
|
+
*
|
|
13
|
+
* Core stays environment-pure: hosts read their own environment and pass
|
|
14
|
+
* the configured value and the stored setting in.
|
|
9
15
|
*/
|
|
16
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
17
|
+
import { carbonBudgetConfigPath, parseKeyValues } from "./budget.js";
|
|
18
|
+
import { REGION_CATALOG } from "./data/tables.generated.js";
|
|
10
19
|
/** Fallback when nothing else resolves — GB has key-less live data. */
|
|
11
20
|
export const FALLBACK_REGION = "GB";
|
|
21
|
+
/** Key of the region setting in `~/.tidegap/config` (and the env override). */
|
|
22
|
+
export const REGION_SETTING_KEY = "TIDEGAP_DEFAULT_REGION";
|
|
23
|
+
export { REGION_CATALOG };
|
|
12
24
|
/**
|
|
13
25
|
* Best-effort IANA-timezone → grid-region map. Only timezones whose
|
|
14
|
-
* dominant grid is unambiguous are listed;
|
|
15
|
-
*
|
|
26
|
+
* dominant grid is unambiguous are listed; a time zone that spans several
|
|
27
|
+
* grids (Oslo, Stockholm, Copenhagen, Rome, Tokyo, Kolkata, Toronto,
|
|
28
|
+
* Chicago) resolves to FALLBACK_REGION and the user sets the region.
|
|
16
29
|
*/
|
|
17
30
|
export const TIMEZONE_REGION = {
|
|
18
31
|
"Europe/London": "GB",
|
|
19
32
|
"Europe/Paris": "FR",
|
|
20
33
|
"Europe/Berlin": "DE",
|
|
21
34
|
"Europe/Busingen": "DE",
|
|
35
|
+
"Europe/Warsaw": "PL",
|
|
36
|
+
"Europe/Vienna": "AT",
|
|
37
|
+
"Europe/Madrid": "ES",
|
|
38
|
+
"Europe/Amsterdam": "NL",
|
|
39
|
+
"Europe/Brussels": "BE",
|
|
40
|
+
"Europe/Dublin": "IE",
|
|
41
|
+
"Europe/Helsinki": "FI",
|
|
22
42
|
"America/Los_Angeles": "US-CAL-CISO",
|
|
23
43
|
"America/New_York": "US-MIDA-PJM",
|
|
24
44
|
"America/Detroit": "US-MIDA-PJM",
|
|
45
|
+
"Asia/Seoul": "KR",
|
|
46
|
+
"Asia/Singapore": "SG",
|
|
47
|
+
"Asia/Dubai": "AE",
|
|
48
|
+
"Australia/Sydney": "AU-NSW",
|
|
49
|
+
"Africa/Johannesburg": "ZA",
|
|
50
|
+
"Pacific/Auckland": "NZ",
|
|
51
|
+
};
|
|
52
|
+
const UA_NOTE = "No Ukrainian grid data yet; using Poland (PL), the nearest grid in the same synchronous area.";
|
|
53
|
+
const UA_APPROXIMATION = Object.freeze({
|
|
54
|
+
region: "PL",
|
|
55
|
+
approximate: Object.freeze({ country: "UA", note: UA_NOTE }),
|
|
56
|
+
});
|
|
57
|
+
/**
|
|
58
|
+
* Time zones whose own grid is not in the catalog, mapped to the nearest
|
|
59
|
+
* catalog zone. PL is the only catalog zone that borders Ukraine and shares
|
|
60
|
+
* its synchronous area (Continental Europe, since March 2022): hour-of-day
|
|
61
|
+
* timing carries over, absolute grams do not.
|
|
62
|
+
*/
|
|
63
|
+
export const APPROXIMATE_TIMEZONE_REGION = {
|
|
64
|
+
"Europe/Kyiv": UA_APPROXIMATION,
|
|
65
|
+
"Europe/Kiev": UA_APPROXIMATION,
|
|
66
|
+
"Europe/Uzhgorod": UA_APPROXIMATION,
|
|
67
|
+
"Europe/Zaporozhye": UA_APPROXIMATION,
|
|
25
68
|
};
|
|
69
|
+
/** The catalog zone for `input` (case-insensitive), or undefined. */
|
|
70
|
+
export function catalogZone(input) {
|
|
71
|
+
if (!input)
|
|
72
|
+
return undefined;
|
|
73
|
+
const zone = input.trim().toUpperCase();
|
|
74
|
+
return Object.hasOwn(REGION_CATALOG, zone) ? zone : undefined;
|
|
75
|
+
}
|
|
26
76
|
/** Map an IANA timezone to a supported grid region, or undefined. */
|
|
27
77
|
export function regionForTimezone(timeZone) {
|
|
28
|
-
return
|
|
78
|
+
return guessRegionFromTimeZone(timeZone)?.region;
|
|
79
|
+
}
|
|
80
|
+
/** The host machine's IANA time zone, or undefined when it cannot be read. */
|
|
81
|
+
export function hostTimeZone() {
|
|
82
|
+
try {
|
|
83
|
+
return Intl.DateTimeFormat().resolvedOptions().timeZone || undefined;
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Guess a catalog zone from an IANA time zone: an exact map entry, else an
|
|
91
|
+
* approximate one (with the reason), else undefined.
|
|
92
|
+
*/
|
|
93
|
+
export function guessRegionFromTimeZone(timeZone) {
|
|
94
|
+
if (!timeZone)
|
|
95
|
+
return undefined;
|
|
96
|
+
const exact = Object.hasOwn(TIMEZONE_REGION, timeZone) ? TIMEZONE_REGION[timeZone] : undefined;
|
|
97
|
+
if (exact)
|
|
98
|
+
return { region: exact };
|
|
99
|
+
return Object.hasOwn(APPROXIMATE_TIMEZONE_REGION, timeZone)
|
|
100
|
+
? APPROXIMATE_TIMEZONE_REGION[timeZone]
|
|
101
|
+
: undefined;
|
|
29
102
|
}
|
|
30
103
|
/** Guess the grid region from the host machine's IANA timezone. */
|
|
31
104
|
export function detectRegionFromTimezone() {
|
|
105
|
+
return regionForTimezone(hostTimeZone() ?? "");
|
|
106
|
+
}
|
|
107
|
+
/** Warning text for a stored region that is not a catalog zone. */
|
|
108
|
+
export function ignoredRegionMessage(value) {
|
|
109
|
+
return `Stored region ${value} is not supported; ignored.`;
|
|
110
|
+
}
|
|
111
|
+
export function resolveRegion(inputOrRequested, configuredDefault) {
|
|
112
|
+
const input = typeof inputOrRequested === "object" && inputOrRequested !== null
|
|
113
|
+
? inputOrRequested
|
|
114
|
+
: {
|
|
115
|
+
requested: inputOrRequested,
|
|
116
|
+
configured: { value: configuredDefault, source: "config" },
|
|
117
|
+
};
|
|
118
|
+
if (input.requested)
|
|
119
|
+
return { region: input.requested, source: "flag" };
|
|
120
|
+
const configured = input.configured;
|
|
121
|
+
if (configured?.value)
|
|
122
|
+
return { region: configured.value, source: configured.source };
|
|
123
|
+
const raw = input.setting?.trim();
|
|
124
|
+
const stored = catalogZone(raw);
|
|
125
|
+
if (stored)
|
|
126
|
+
return { region: stored, source: "setting" };
|
|
127
|
+
const ignored = raw ? { ignored: raw } : {};
|
|
128
|
+
const timeZone = "timeZone" in input ? input.timeZone : hostTimeZone();
|
|
129
|
+
const zoneInfo = timeZone ? { timeZone } : {};
|
|
130
|
+
const guess = guessRegionFromTimeZone(timeZone);
|
|
131
|
+
if (guess)
|
|
132
|
+
return { ...guess, source: "timezone", ...zoneInfo, ...ignored };
|
|
133
|
+
return { region: FALLBACK_REGION, source: "fallback", ...zoneInfo, ...ignored };
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The raw region setting from `~/.tidegap/config` (or `path`), or undefined
|
|
137
|
+
* when the file or the key is missing, empty or unreadable. Not validated:
|
|
138
|
+
* resolveRegion skips a value that is not a catalog zone.
|
|
139
|
+
*/
|
|
140
|
+
export function loadRegionSetting(path = carbonBudgetConfigPath()) {
|
|
141
|
+
if (!existsSync(path))
|
|
142
|
+
return undefined;
|
|
32
143
|
try {
|
|
33
|
-
|
|
144
|
+
const value = parseKeyValues(readFileSync(path, "utf8"))[REGION_SETTING_KEY]?.trim();
|
|
145
|
+
return value || undefined;
|
|
34
146
|
}
|
|
35
147
|
catch {
|
|
36
148
|
return undefined;
|
|
37
149
|
}
|
|
38
150
|
}
|
|
151
|
+
/** Edit distance (Levenshtein) between two short strings. */
|
|
152
|
+
function editDistance(a, b) {
|
|
153
|
+
let prev = Array.from({ length: b.length + 1 }, (_, j) => j);
|
|
154
|
+
for (let i = 1; i <= a.length; i++) {
|
|
155
|
+
const cur = [i];
|
|
156
|
+
for (let j = 1; j <= b.length; j++) {
|
|
157
|
+
cur[j] = Math.min((prev[j] ?? 0) + 1, (cur[j - 1] ?? 0) + 1, (prev[j - 1] ?? 0) + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
158
|
+
}
|
|
159
|
+
prev = cur;
|
|
160
|
+
}
|
|
161
|
+
return prev[b.length] ?? 0;
|
|
162
|
+
}
|
|
39
163
|
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* 3. a guess from the host machine's timezone,
|
|
44
|
-
* 4. FALLBACK_REGION.
|
|
164
|
+
* Largest edit distance still offered as a closest match: 1 for inputs of up
|
|
165
|
+
* to 3 characters (any two 2-letter codes are within distance 2, so a wider
|
|
166
|
+
* bound would suggest unrelated zones), else 2.
|
|
45
167
|
*/
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
168
|
+
function maxSuggestDistance(text) {
|
|
169
|
+
return text.length <= 3 ? 1 : 2;
|
|
170
|
+
}
|
|
171
|
+
/** Common codes that are not the ISO code of their catalog zone. */
|
|
172
|
+
export const COUNTRY_ALIASES = { UK: "GB" };
|
|
173
|
+
/**
|
|
174
|
+
* Up to `limit` catalog zones closest to an unknown input, in catalog order
|
|
175
|
+
* within each rule. The first rule with a result decides:
|
|
176
|
+
* 1. the zone of a common alias (UK → GB), or the approximate-map
|
|
177
|
+
* target of a typed country code (UA → PL),
|
|
178
|
+
* 2. zones whose code, name, country code or country name starts with it,
|
|
179
|
+
* 3. zones whose code is within edit distance 2 (1 for inputs of up to
|
|
180
|
+
* 3 characters), nearest first.
|
|
181
|
+
*/
|
|
182
|
+
export function closestRegions(input, limit = 3) {
|
|
183
|
+
const text = input.trim().toUpperCase();
|
|
184
|
+
if (!text)
|
|
185
|
+
return [];
|
|
186
|
+
const zones = Object.keys(REGION_CATALOG);
|
|
187
|
+
if (Object.hasOwn(COUNTRY_ALIASES, text))
|
|
188
|
+
return [COUNTRY_ALIASES[text]];
|
|
189
|
+
const approximate = [
|
|
190
|
+
...new Set(Object.values(APPROXIMATE_TIMEZONE_REGION)
|
|
191
|
+
.filter((a) => a.approximate.country === text)
|
|
192
|
+
.map((a) => a.region)),
|
|
193
|
+
];
|
|
194
|
+
if (approximate.length > 0)
|
|
195
|
+
return approximate.slice(0, limit);
|
|
196
|
+
const prefixed = Object.entries(REGION_CATALOG)
|
|
197
|
+
.filter(([zone, e]) => [zone, e.name, e.country, e.countryName].some((f) => f.toUpperCase().startsWith(text)))
|
|
198
|
+
.map(([zone]) => zone);
|
|
199
|
+
if (prefixed.length > 0)
|
|
200
|
+
return prefixed.slice(0, limit);
|
|
201
|
+
const maxDistance = maxSuggestDistance(text);
|
|
202
|
+
return zones
|
|
203
|
+
.map((zone, order) => ({ zone, order, d: editDistance(text, zone) }))
|
|
204
|
+
.filter((m) => m.d <= maxDistance)
|
|
205
|
+
.sort((x, y) => x.d - y.d || x.order - y.order)
|
|
206
|
+
.slice(0, limit)
|
|
207
|
+
.map((m) => m.zone);
|
|
56
208
|
}
|
|
57
209
|
//# sourceMappingURL=region.js.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a `claude-code` task ran: in a copy (fork) of the Claude Code
|
|
3
|
+
* session that queued it, or in a fresh session, and why. The adapter in
|
|
4
|
+
* the `tidegap` CLI decides; the scheduler copies the result onto the
|
|
5
|
+
* receipt, and every surface words it with the helpers below.
|
|
6
|
+
*/
|
|
7
|
+
import type { ProviderCallOrigin, TaskRecord } from "./types.js";
|
|
8
|
+
/** `fork`: a copy of the origin session. `fresh`: a new session with no history. */
|
|
9
|
+
export type RunSessionMode = "fork" | "fresh";
|
|
10
|
+
/** Why a task with (or without) an origin ran fresh. */
|
|
11
|
+
export type FreshReason = "disabled" | "no-origin" | "api-provider" | "web-tools" | "cwd-missing" | "cwd-unsafe" | "session-missing" | "too-large" | "other-cwd";
|
|
12
|
+
/** The session facts of one run, on `DispatchResult.session` and `CarbonReceipt.session`. */
|
|
13
|
+
export interface RunSession {
|
|
14
|
+
mode: RunSessionMode;
|
|
15
|
+
/** The session that ran: the fork's id, or the fresh session's id. */
|
|
16
|
+
sessionId?: string;
|
|
17
|
+
/** The session the task came from, when it had one. */
|
|
18
|
+
originSessionId?: string;
|
|
19
|
+
/** Why the run was fresh. Absent for a fork. */
|
|
20
|
+
reason?: FreshReason;
|
|
21
|
+
/** The context estimate of the origin session, when one was made. */
|
|
22
|
+
contextTokens?: number;
|
|
23
|
+
}
|
|
24
|
+
/** The first 8 characters of a session id, for display. */
|
|
25
|
+
export declare function shortSessionId(id: string): string;
|
|
26
|
+
/** `about 620k tokens`. */
|
|
27
|
+
export declare function tokensLabel(tokens: number): string;
|
|
28
|
+
/** A fresh-run reason in words: `session too large`, `web tools`, ... */
|
|
29
|
+
export declare function freshReasonText(reason: FreshReason): string;
|
|
30
|
+
/** The origin facts of a task's spec (`ProviderCallSpec.origin`), or undefined. */
|
|
31
|
+
export declare function taskOrigin(record: Pick<TaskRecord<unknown>, "bodyJson">): ProviderCallOrigin | undefined;
|
|
32
|
+
/** The command that opens the fork a task ran in (`cd <dir> && claude --resume <fork id>`), or undefined. */
|
|
33
|
+
export declare function forkCommand(record: Pick<TaskRecord<unknown>, "originCwd" | "receipt">): string | undefined;
|
|
34
|
+
/** `fork f1a2b3c4 of 11111111` or `fresh (too-large, about 620k tokens)`. */
|
|
35
|
+
export declare function runSessionSummary(session: RunSession): string;
|
|
36
|
+
//# sourceMappingURL=run-session.d.ts.map
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a `claude-code` task ran: in a copy (fork) of the Claude Code
|
|
3
|
+
* session that queued it, or in a fresh session, and why. The adapter in
|
|
4
|
+
* the `tidegap` CLI decides; the scheduler copies the result onto the
|
|
5
|
+
* receipt, and every surface words it with the helpers below.
|
|
6
|
+
*/
|
|
7
|
+
import { resumeCommand } from "./history.js";
|
|
8
|
+
/** The first 8 characters of a session id, for display. */
|
|
9
|
+
export function shortSessionId(id) {
|
|
10
|
+
return id.slice(0, 8);
|
|
11
|
+
}
|
|
12
|
+
/** `about 620k tokens`. */
|
|
13
|
+
export function tokensLabel(tokens) {
|
|
14
|
+
if (tokens >= 1000)
|
|
15
|
+
return `about ${Math.round(tokens / 1000)}k tokens`;
|
|
16
|
+
return `about ${tokens} tokens`;
|
|
17
|
+
}
|
|
18
|
+
/** A fresh-run reason in words: `session too large`, `web tools`, ... */
|
|
19
|
+
export function freshReasonText(reason) {
|
|
20
|
+
switch (reason) {
|
|
21
|
+
case "disabled":
|
|
22
|
+
return "session resume is off (TIDEGAP_RESUME=off)";
|
|
23
|
+
case "no-origin":
|
|
24
|
+
return "no session found";
|
|
25
|
+
case "api-provider":
|
|
26
|
+
return "the API provider has no session";
|
|
27
|
+
case "web-tools":
|
|
28
|
+
return "web tools";
|
|
29
|
+
case "cwd-missing":
|
|
30
|
+
return "the session directory is gone";
|
|
31
|
+
case "cwd-unsafe":
|
|
32
|
+
return "the session directory is the root or home directory, or not yours";
|
|
33
|
+
case "session-missing":
|
|
34
|
+
return "the session transcript is gone";
|
|
35
|
+
case "too-large":
|
|
36
|
+
return "session too large";
|
|
37
|
+
case "other-cwd":
|
|
38
|
+
return "the task names another folder";
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** The origin facts of a task's spec (`ProviderCallSpec.origin`), or undefined. */
|
|
42
|
+
export function taskOrigin(record) {
|
|
43
|
+
if (!record.bodyJson)
|
|
44
|
+
return undefined;
|
|
45
|
+
try {
|
|
46
|
+
const origin = JSON.parse(record.bodyJson).origin;
|
|
47
|
+
if (!origin || typeof origin !== "object")
|
|
48
|
+
return undefined;
|
|
49
|
+
const agent = origin.agent;
|
|
50
|
+
return agent === "claude-code" || agent === "codex" ? origin : undefined;
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** The command that opens the fork a task ran in (`cd <dir> && claude --resume <fork id>`), or undefined. */
|
|
57
|
+
export function forkCommand(record) {
|
|
58
|
+
const session = record.receipt?.session;
|
|
59
|
+
if (session?.mode !== "fork" || !session.sessionId)
|
|
60
|
+
return undefined;
|
|
61
|
+
return resumeCommand({ originCwd: record.originCwd, originSessionId: session.sessionId });
|
|
62
|
+
}
|
|
63
|
+
/** `fork f1a2b3c4 of 11111111` or `fresh (too-large, about 620k tokens)`. */
|
|
64
|
+
export function runSessionSummary(session) {
|
|
65
|
+
if (session.mode === "fork") {
|
|
66
|
+
const fork = session.sessionId ? shortSessionId(session.sessionId) : "?";
|
|
67
|
+
return session.originSessionId ? `fork ${fork} of ${shortSessionId(session.originSessionId)}` : `fork ${fork}`;
|
|
68
|
+
}
|
|
69
|
+
const parts = [session.reason, session.contextTokens !== undefined ? tokensLabel(session.contextTokens) : undefined].filter(Boolean);
|
|
70
|
+
return parts.length > 0 ? `fresh (${parts.join(", ")})` : "fresh";
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=run-session.js.map
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `schedule_task` sugar for floating recurring jobs (KTD16), shared by the
|
|
3
|
+
* MCP server and the OpenClaw plugin so both hosts read the same `repeat`
|
|
4
|
+
* vocabulary the same way.
|
|
5
|
+
*
|
|
6
|
+
* A job `repeat` (`daily`, `weekdays`, `weekly`, `every_n_hours`) creates a
|
|
7
|
+
* prompt job; `none` queues a one-off task and `quota_reset` keeps the older
|
|
8
|
+
* series path. Agents never create command jobs (KD10): a shell-like
|
|
9
|
+
* argument is refused with the CLI command the user runs instead.
|
|
10
|
+
*/
|
|
11
|
+
import { type JobInput } from "./jobs.js";
|
|
12
|
+
import type { Scheduler } from "./scheduler.js";
|
|
13
|
+
import type { JobRecord, JobTools, ProviderName } from "./types.js";
|
|
14
|
+
/** The `repeat` values that create a job. */
|
|
15
|
+
export declare const JOB_REPEATS: readonly ["daily", "weekdays", "weekly", "every_n_hours"];
|
|
16
|
+
export type JobRepeat = (typeof JOB_REPEATS)[number];
|
|
17
|
+
/** True when `repeat` creates a job rather than a one-off task or a series. */
|
|
18
|
+
export declare function isJobRepeat(repeat: unknown): repeat is JobRepeat;
|
|
19
|
+
/** Parameters that only mean something with a job repeat. */
|
|
20
|
+
export declare const JOB_ONLY_PARAMS: readonly ["every_hours", "days", "window", "time_zone"];
|
|
21
|
+
/** The CLI line that queues a one-off shell command task, which only the user runs. */
|
|
22
|
+
export declare const QUEUE_ADD_COMMAND_LINE = "tidegap queue add --runner command --cwd <dir> --by <when> '<cmd>'";
|
|
23
|
+
/**
|
|
24
|
+
* The refusal for a call that tries to create a command job (AE12) or a
|
|
25
|
+
* one-off shell command task (`runner` or `provider` "command"), or
|
|
26
|
+
* undefined. Checked on the raw arguments: the schemas drop unknown keys.
|
|
27
|
+
*/
|
|
28
|
+
export declare function commandJobRefusal(args: Record<string, unknown>): string | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* The refusal for job-only parameters passed without a job repeat, or
|
|
31
|
+
* undefined. `repeat` is the call's repeat when it creates a job (else
|
|
32
|
+
* undefined); `present` lists the parameter names the call set.
|
|
33
|
+
*/
|
|
34
|
+
export declare function jobParamsRefusal(repeat: JobRepeat | undefined, present: readonly string[]): string | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* The refusal for per-task options a job cannot keep, or undefined. `args`
|
|
37
|
+
* are the call's arguments; an option counts when set to anything but
|
|
38
|
+
* `false`, and `dispatch` counts only when `false` (its default is `true`).
|
|
39
|
+
*/
|
|
40
|
+
export declare function jobUnsupportedRefusal(args: Record<string, unknown>): string | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* The job tool group for a `claude-code` tool list: any file tool keeps the
|
|
43
|
+
* read-only file group, any web tool the web group, `[]` none. The caller
|
|
44
|
+
* has already refused a list that mixes the two (claudeCodeToolsError).
|
|
45
|
+
*/
|
|
46
|
+
export declare function jobToolsFor(tools: readonly string[]): JobTools;
|
|
47
|
+
/** What both hosts pass to build a prompt job from a `schedule_task` call. */
|
|
48
|
+
export interface ScheduleJobArgs {
|
|
49
|
+
prompt: string;
|
|
50
|
+
title?: string;
|
|
51
|
+
repeat: JobRepeat;
|
|
52
|
+
everyHours?: number;
|
|
53
|
+
days?: readonly string[];
|
|
54
|
+
window?: string;
|
|
55
|
+
timeZone?: string;
|
|
56
|
+
/** Deadline of the first run only (AS9). */
|
|
57
|
+
deadline?: string;
|
|
58
|
+
provider: ProviderName;
|
|
59
|
+
model?: string;
|
|
60
|
+
tools: JobTools;
|
|
61
|
+
/** The project directory a run starts in; never a session to fork (KD8). */
|
|
62
|
+
cwd?: string;
|
|
63
|
+
host: "mcp" | "openclaw";
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The {@link JobInput} for a `schedule_task` job call. Throws an Error with
|
|
67
|
+
* a plain message on a bad cadence, window or zone.
|
|
68
|
+
*/
|
|
69
|
+
export declare function scheduleJobInput(args: ScheduleJobArgs, now?: Date): JobInput;
|
|
70
|
+
/** A job as the tool hosts list it. */
|
|
71
|
+
export interface JobToolView {
|
|
72
|
+
jobId: string;
|
|
73
|
+
title: string;
|
|
74
|
+
status: JobRecord["status"];
|
|
75
|
+
/** The guarantee in words, e.g. "runs every day between 00:00 and 08:00". */
|
|
76
|
+
guarantee: string;
|
|
77
|
+
/** Planned hour of the job's live run, if one is planned. */
|
|
78
|
+
nextRun?: string;
|
|
79
|
+
/** Id of that run. */
|
|
80
|
+
nextTaskId?: string;
|
|
81
|
+
lastOutcome?: JobRecord["lastOutcome"];
|
|
82
|
+
pauseReason?: string;
|
|
83
|
+
}
|
|
84
|
+
/** One job's list view: guarantee, next run, last result. */
|
|
85
|
+
export declare function jobToolView(scheduler: Scheduler, job: JobRecord): JobToolView;
|
|
86
|
+
/**
|
|
87
|
+
* `cancel_task series=true` on a job's run (series coexistence map): remove
|
|
88
|
+
* the job, so no further run is materialized. Returns the job when the task
|
|
89
|
+
* belongs to one (already removed included), else undefined. The caller
|
|
90
|
+
* cancels the task itself afterwards (stop flag first, KTD4).
|
|
91
|
+
*/
|
|
92
|
+
export declare function endJobOfTask(scheduler: Scheduler, taskId: string, opts?: {
|
|
93
|
+
actor?: string;
|
|
94
|
+
}): Promise<JobRecord | undefined>;
|
|
95
|
+
//# sourceMappingURL=schedule-job.d.ts.map
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `schedule_task` sugar for floating recurring jobs (KTD16), shared by the
|
|
3
|
+
* MCP server and the OpenClaw plugin so both hosts read the same `repeat`
|
|
4
|
+
* vocabulary the same way.
|
|
5
|
+
*
|
|
6
|
+
* A job `repeat` (`daily`, `weekdays`, `weekly`, `every_n_hours`) creates a
|
|
7
|
+
* prompt job; `none` queues a one-off task and `quota_reset` keeps the older
|
|
8
|
+
* series path. Agents never create command jobs (KD10): a shell-like
|
|
9
|
+
* argument is refused with the CLI command the user runs instead.
|
|
10
|
+
*/
|
|
11
|
+
import { includesFileTool, includesWebTool } from "./claude-tools.js";
|
|
12
|
+
import { guaranteeText, parseWindow, validateJobSchedule } from "./job-cadence.js";
|
|
13
|
+
import { getJob, removeJob } from "./jobs.js";
|
|
14
|
+
import { systemTimeZone } from "./zone-time.js";
|
|
15
|
+
/** The `repeat` values that create a job. */
|
|
16
|
+
export const JOB_REPEATS = ["daily", "weekdays", "weekly", "every_n_hours"];
|
|
17
|
+
/** True when `repeat` creates a job rather than a one-off task or a series. */
|
|
18
|
+
export function isJobRepeat(repeat) {
|
|
19
|
+
return typeof repeat === "string" && JOB_REPEATS.includes(repeat);
|
|
20
|
+
}
|
|
21
|
+
/** Parameters that only mean something with a job repeat. */
|
|
22
|
+
export const JOB_ONLY_PARAMS = ["every_hours", "days", "window", "time_zone"];
|
|
23
|
+
/** Argument names that would make the run execute a shell command. */
|
|
24
|
+
const SHELL_KEYS = ["command", "cmd", "shell", "script"];
|
|
25
|
+
/** The CLI line that queues a one-off shell command task, which only the user runs. */
|
|
26
|
+
export const QUEUE_ADD_COMMAND_LINE = "tidegap queue add --runner command --cwd <dir> --by <when> '<cmd>'";
|
|
27
|
+
/**
|
|
28
|
+
* The refusal for a call that tries to create a command job (AE12) or a
|
|
29
|
+
* one-off shell command task (`runner` or `provider` "command"), or
|
|
30
|
+
* undefined. Checked on the raw arguments: the schemas drop unknown keys.
|
|
31
|
+
*/
|
|
32
|
+
export function commandJobRefusal(args) {
|
|
33
|
+
const field = ["runner", "provider"].find((k) => args[k] === "command");
|
|
34
|
+
if (field) {
|
|
35
|
+
return (`schedule_task cannot queue a shell command (${field} "command"): a shell command task is the user's call. ` +
|
|
36
|
+
`Ask the user to run: ${QUEUE_ADD_COMMAND_LINE}`);
|
|
37
|
+
}
|
|
38
|
+
const key = SHELL_KEYS.find((k) => args[k] !== undefined);
|
|
39
|
+
if (!key)
|
|
40
|
+
return undefined;
|
|
41
|
+
return (`schedule_task cannot create command jobs (got "${key}"). ` +
|
|
42
|
+
`Ask the user to run: tidegap jobs add --command '<cmd>' --daily (see tidegap jobs add --help)`);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The refusal for job-only parameters passed without a job repeat, or
|
|
46
|
+
* undefined. `repeat` is the call's repeat when it creates a job (else
|
|
47
|
+
* undefined); `present` lists the parameter names the call set.
|
|
48
|
+
*/
|
|
49
|
+
export function jobParamsRefusal(repeat, present) {
|
|
50
|
+
if (repeat !== undefined) {
|
|
51
|
+
if (repeat !== "every_n_hours" && present.includes("every_hours"))
|
|
52
|
+
return `every_hours needs repeat "every_n_hours"`;
|
|
53
|
+
if (repeat !== "weekly" && present.includes("days"))
|
|
54
|
+
return `days needs repeat "weekly"`;
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
const stray = JOB_ONLY_PARAMS.filter((p) => present.includes(p));
|
|
58
|
+
if (stray.length === 0)
|
|
59
|
+
return undefined;
|
|
60
|
+
return `${stray.join(", ")} need a job repeat (${JOB_REPEATS.join(", ")})`;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Per-task options a job cannot keep, across both hosts: a job's runs plan
|
|
64
|
+
* against the default region and carry no routing, budget, timing or
|
|
65
|
+
* delivery options of their own.
|
|
66
|
+
*/
|
|
67
|
+
const JOB_UNSUPPORTED_PARAMS = [
|
|
68
|
+
"region",
|
|
69
|
+
"carbon_budget_g",
|
|
70
|
+
"candidates",
|
|
71
|
+
"route_weights",
|
|
72
|
+
"not_before",
|
|
73
|
+
"after_limit_reset",
|
|
74
|
+
"dry_run",
|
|
75
|
+
"output_path",
|
|
76
|
+
"cwd",
|
|
77
|
+
"permission_profile",
|
|
78
|
+
"result_path",
|
|
79
|
+
"wake_session",
|
|
80
|
+
"redact_in_receipt",
|
|
81
|
+
"quota_provider",
|
|
82
|
+
"deliver",
|
|
83
|
+
"webhook_url",
|
|
84
|
+
"file_path",
|
|
85
|
+
"file_format",
|
|
86
|
+
];
|
|
87
|
+
/**
|
|
88
|
+
* The refusal for per-task options a job cannot keep, or undefined. `args`
|
|
89
|
+
* are the call's arguments; an option counts when set to anything but
|
|
90
|
+
* `false`, and `dispatch` counts only when `false` (its default is `true`).
|
|
91
|
+
*/
|
|
92
|
+
export function jobUnsupportedRefusal(args) {
|
|
93
|
+
const present = JOB_UNSUPPORTED_PARAMS.filter((k) => args[k] !== undefined && args[k] !== false);
|
|
94
|
+
if (args.dispatch === false)
|
|
95
|
+
present.push("dispatch");
|
|
96
|
+
if (present.length === 0)
|
|
97
|
+
return undefined;
|
|
98
|
+
return `${present.join(", ")} cannot be used with a job repeat; omit ${present.length === 1 ? "it" : "them"}`;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The job tool group for a `claude-code` tool list: any file tool keeps the
|
|
102
|
+
* read-only file group, any web tool the web group, `[]` none. The caller
|
|
103
|
+
* has already refused a list that mixes the two (claudeCodeToolsError).
|
|
104
|
+
*/
|
|
105
|
+
export function jobToolsFor(tools) {
|
|
106
|
+
if (includesFileTool(tools))
|
|
107
|
+
return "files";
|
|
108
|
+
if (includesWebTool(tools))
|
|
109
|
+
return "web";
|
|
110
|
+
return "none";
|
|
111
|
+
}
|
|
112
|
+
/** Today's weekday in `timeZone` (a `weekly` job with no days, KTD16). */
|
|
113
|
+
function weekdayIn(timeZone, now) {
|
|
114
|
+
return new Intl.DateTimeFormat("en-US", { weekday: "short", timeZone }).format(now).toLowerCase().slice(0, 3);
|
|
115
|
+
}
|
|
116
|
+
function cadenceOf(args, timeZone, now) {
|
|
117
|
+
switch (args.repeat) {
|
|
118
|
+
case "daily":
|
|
119
|
+
return { kind: "daily" };
|
|
120
|
+
case "weekdays":
|
|
121
|
+
return { kind: "weekdays" };
|
|
122
|
+
case "weekly":
|
|
123
|
+
// validateJobSchedule checks the day names.
|
|
124
|
+
return {
|
|
125
|
+
kind: "weekly",
|
|
126
|
+
days: (args.days && args.days.length > 0 ? args.days : [weekdayIn(timeZone, now)]),
|
|
127
|
+
};
|
|
128
|
+
case "every_n_hours":
|
|
129
|
+
if (args.everyHours === undefined)
|
|
130
|
+
throw new Error(`repeat "every_n_hours" needs every_hours (2 to 168)`);
|
|
131
|
+
return { kind: "hours", every: args.everyHours };
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* The {@link JobInput} for a `schedule_task` job call. Throws an Error with
|
|
136
|
+
* a plain message on a bad cadence, window or zone.
|
|
137
|
+
*/
|
|
138
|
+
export function scheduleJobInput(args, now = new Date()) {
|
|
139
|
+
const timeZone = args.timeZone ?? systemTimeZone();
|
|
140
|
+
// The zone is checked before the weekday lookup uses it.
|
|
141
|
+
validateJobSchedule({ cadence: { kind: "daily" }, timeZone });
|
|
142
|
+
const cadence = cadenceOf(args, timeZone, now);
|
|
143
|
+
const window = args.window !== undefined ? parseWindow(args.window) : undefined;
|
|
144
|
+
const schedule = validateJobSchedule({ cadence, window, timeZone });
|
|
145
|
+
return {
|
|
146
|
+
title: args.title?.trim() ? args.title : args.prompt,
|
|
147
|
+
cadence: schedule.cadence,
|
|
148
|
+
window: schedule.window,
|
|
149
|
+
timeZone: schedule.timeZone,
|
|
150
|
+
template: {
|
|
151
|
+
runner: "prompt",
|
|
152
|
+
prompt: args.prompt,
|
|
153
|
+
provider: args.provider,
|
|
154
|
+
model: args.model,
|
|
155
|
+
cwd: args.cwd,
|
|
156
|
+
tools: args.tools,
|
|
157
|
+
},
|
|
158
|
+
originHost: args.host,
|
|
159
|
+
originCwd: args.cwd,
|
|
160
|
+
...(args.deadline !== undefined ? { deadline: args.deadline } : {}),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
const LIVE = new Set(["queued", "scheduled", "running", "submitted"]);
|
|
164
|
+
/** One job's list view: guarantee, next run, last result. */
|
|
165
|
+
export function jobToolView(scheduler, job) {
|
|
166
|
+
const row = job.pendingTaskId ? scheduler.ledger()?.get(job.pendingTaskId) : undefined;
|
|
167
|
+
const live = row && LIVE.has(row.status) ? row : undefined;
|
|
168
|
+
return {
|
|
169
|
+
jobId: job.jobId,
|
|
170
|
+
title: job.title,
|
|
171
|
+
status: job.status,
|
|
172
|
+
guarantee: guaranteeText({ cadence: job.cadence, window: job.window, timeZone: job.timeZone }),
|
|
173
|
+
...(live ? { nextTaskId: live.taskId, nextRun: live.scheduledFor ?? live.notBefore } : {}),
|
|
174
|
+
...(job.lastOutcome ? { lastOutcome: job.lastOutcome } : {}),
|
|
175
|
+
...(job.pauseReason ? { pauseReason: job.pauseReason } : {}),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* `cancel_task series=true` on a job's run (series coexistence map): remove
|
|
180
|
+
* the job, so no further run is materialized. Returns the job when the task
|
|
181
|
+
* belongs to one (already removed included), else undefined. The caller
|
|
182
|
+
* cancels the task itself afterwards (stop flag first, KTD4).
|
|
183
|
+
*/
|
|
184
|
+
export async function endJobOfTask(scheduler, taskId, opts = {}) {
|
|
185
|
+
const jobId = scheduler.ledger()?.get(taskId)?.jobId;
|
|
186
|
+
if (!jobId)
|
|
187
|
+
return undefined;
|
|
188
|
+
const job = getJob(scheduler, jobId);
|
|
189
|
+
if (!job)
|
|
190
|
+
return undefined;
|
|
191
|
+
if (job.status === "removed")
|
|
192
|
+
return job;
|
|
193
|
+
return removeJob(scheduler, jobId, opts);
|
|
194
|
+
}
|
|
195
|
+
//# sourceMappingURL=schedule-job.js.map
|