@tidegap/core 0.19.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 +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 +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 +17 -2
- package/dist/grid.js +43 -4
- 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 +43 -13
- package/dist/index.js +32 -6
- 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/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 +12 -27
- 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 +919 -109
- 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 +10 -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
package/dist/notify.d.ts
CHANGED
|
@@ -39,6 +39,8 @@ export interface OsNotifyOptions {
|
|
|
39
39
|
export declare function osNotifyCommand(platform: NodeJS.Platform, title: string, body: string, opts?: OsNotifyOptions): OsNotifyCommand | null;
|
|
40
40
|
/** Characters of result text shown in a notification body. */
|
|
41
41
|
export declare const NOTIFY_PREVIEW_CHARS = 140;
|
|
42
|
+
/** `text` cut to `max` characters (code points) plus an ellipsis. */
|
|
43
|
+
export declare function clip(text: string, max: number): string;
|
|
42
44
|
/**
|
|
43
45
|
* Notification title and body for a finished task: the task title (or id),
|
|
44
46
|
* a redacted one-line preview of the result (completed) or the first line
|
package/dist/notify.js
CHANGED
|
@@ -77,7 +77,8 @@ function oneLine(text) {
|
|
|
77
77
|
.replace(/\s+/g, " ")
|
|
78
78
|
.trim();
|
|
79
79
|
}
|
|
80
|
-
|
|
80
|
+
/** `text` cut to `max` characters (code points) plus an ellipsis. */
|
|
81
|
+
export function clip(text, max) {
|
|
81
82
|
const chars = Array.from(text);
|
|
82
83
|
return chars.length > max ? `${chars.slice(0, max).join("")}…` : text;
|
|
83
84
|
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* "Later" (coherence KTD4 to KTD6): move a pending task so it never runs
|
|
3
|
+
* earlier than its current planned start. One preset vocabulary for every
|
|
4
|
+
* surface (KTD15), resolved in the process's local time zone. Pure apart
|
|
5
|
+
* from the limit-reset hint, which the helpers read and write through the
|
|
6
|
+
* store's meta table.
|
|
7
|
+
*
|
|
8
|
+
* `Scheduler.postponeTask` turns a resolved target into a reschedule: delay
|
|
9
|
+
* presets and `after_limit_reset` become `{notBefore}` (the scheduler's
|
|
10
|
+
* `startFields` rule: the deadline rises to at least `notBefore + 2 h` and
|
|
11
|
+
* a later one is kept), day presets become `{dayStart, dayEnd}` (the
|
|
12
|
+
* deadline is the window end), and a pin becomes `{at}`.
|
|
13
|
+
*/
|
|
14
|
+
import type { RescheduleTarget } from "./scheduler.js";
|
|
15
|
+
import type { TaskRecord } from "./types.js";
|
|
16
|
+
/** Preset ids every surface maps to (KTD15). */
|
|
17
|
+
export type PostponePreset = "1h" | "tonight" | "tomorrow_morning" | "after_limit_reset";
|
|
18
|
+
/** Presets in display order. */
|
|
19
|
+
export declare const POSTPONE_PRESETS: readonly PostponePreset[];
|
|
20
|
+
/** Display labels of the presets (KTD15). */
|
|
21
|
+
export declare const POSTPONE_LABELS: Readonly<Record<PostponePreset, string>>;
|
|
22
|
+
/** Target of {@link Scheduler.postponeTask}: a preset, a delay or an exact pin. */
|
|
23
|
+
export type PostponeTarget =
|
|
24
|
+
/** `resetAt` applies to `after_limit_reset` only and wins over every other reset source. */
|
|
25
|
+
{
|
|
26
|
+
preset: PostponePreset;
|
|
27
|
+
resetAt?: string | Date;
|
|
28
|
+
}
|
|
29
|
+
/** Not before the planned start plus `delayMs` (the CLI's `+<dur>`). */
|
|
30
|
+
| {
|
|
31
|
+
delayMs: number;
|
|
32
|
+
}
|
|
33
|
+
/** An exact time (a picked time). */
|
|
34
|
+
| {
|
|
35
|
+
until: string | Date;
|
|
36
|
+
};
|
|
37
|
+
/** A known limit reset and where it came from (`caller`, `hint`, or the source's own name). */
|
|
38
|
+
export interface LimitReset {
|
|
39
|
+
at: Date;
|
|
40
|
+
source: string;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The plugged-in reset source (KTD5), for example the CLI usage module's
|
|
44
|
+
* Claude limit reset. Returns null when it knows no reset for `task`.
|
|
45
|
+
*/
|
|
46
|
+
export type LimitResetSource = (task: TaskRecord<unknown>, now: Date) => LimitReset | null | Promise<LimitReset | null>;
|
|
47
|
+
/** One entry of {@link postponeOptions}; `not_before` is ISO-8601, null when unavailable. */
|
|
48
|
+
export interface PostponeOption {
|
|
49
|
+
preset: PostponePreset;
|
|
50
|
+
label: string;
|
|
51
|
+
not_before: string | null;
|
|
52
|
+
available: boolean;
|
|
53
|
+
reason?: string;
|
|
54
|
+
}
|
|
55
|
+
/** Ledger meta key of the last reset time a caller gave (`{at, provider}`). */
|
|
56
|
+
export declare const LIMIT_RESET_HINT_META_KEY = "limit_reset_hint";
|
|
57
|
+
/** The meta part of a `TaskStore` the hint helpers need. */
|
|
58
|
+
export interface MetaStore {
|
|
59
|
+
getMeta(key: string): string | undefined;
|
|
60
|
+
setMeta(key: string, value: string): void;
|
|
61
|
+
}
|
|
62
|
+
/** Local hour the `tonight` preset starts; also the start of a channel's quiet hours (KTD10). */
|
|
63
|
+
export declare const TONIGHT_START_H = 22;
|
|
64
|
+
/** Local hour the `tomorrow_morning` preset starts; also the end of a channel's quiet hours (KTD10). */
|
|
65
|
+
export declare const MORNING_START_H = 9;
|
|
66
|
+
export declare const NEEDS_RESET_TIME = "needs the reset time";
|
|
67
|
+
/** The stored hint, when it is still in the future. */
|
|
68
|
+
export declare function readLimitResetHint(store: MetaStore | undefined, now: Date): LimitReset | null;
|
|
69
|
+
/** Store a caller-given reset as the hint for later tasks. */
|
|
70
|
+
export declare function writeLimitResetHint(store: MetaStore | undefined, at: Date, provider: string | undefined): void;
|
|
71
|
+
/**
|
|
72
|
+
* The reset `after_limit_reset` waits for (R14): the plugged-in source,
|
|
73
|
+
* else a still-future stored hint. A caller-given `reset_at` is handled by
|
|
74
|
+
* the caller before this, because it wins over both.
|
|
75
|
+
*/
|
|
76
|
+
export declare function resolveLimitReset(record: TaskRecord<unknown>, now: Date, source: LimitResetSource | undefined, store: MetaStore | undefined): Promise<LimitReset | null>;
|
|
77
|
+
/** The provider of a provider-call task, if the body parses. */
|
|
78
|
+
export declare function taskProvider(record: TaskRecord<unknown>): string | undefined;
|
|
79
|
+
/**
|
|
80
|
+
* The planned start (KTD4): `scheduledFor` if in the future, else a future
|
|
81
|
+
* `notBefore`, else now. `future` is false for "now", where any target at
|
|
82
|
+
* or after now is later.
|
|
83
|
+
*/
|
|
84
|
+
export declare function plannedStart(record: TaskRecord<unknown>, now: Date): {
|
|
85
|
+
at: Date;
|
|
86
|
+
future: boolean;
|
|
87
|
+
};
|
|
88
|
+
/** "23:00" on the local day of `now`, else "2026-10-23 23:00". */
|
|
89
|
+
export declare function localTimeLabel(at: Date, now: Date): string;
|
|
90
|
+
/** A resolved postpone: the earliest start and the reschedule that writes it. */
|
|
91
|
+
export type ResolvedPostpone = {
|
|
92
|
+
ok: true;
|
|
93
|
+
notBefore: Date;
|
|
94
|
+
reschedule: RescheduleTarget;
|
|
95
|
+
} | {
|
|
96
|
+
ok: false;
|
|
97
|
+
reason: string;
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Resolve `target` for `record` at `now` (KTD4). `reset` is the reset an
|
|
101
|
+
* `after_limit_reset` target uses (already resolved by precedence); a
|
|
102
|
+
* target's own `resetAt` wins over it. Refuses a task that is not movable,
|
|
103
|
+
* a window with less than one hour left, a missing reset, and any start at
|
|
104
|
+
* or before the planned start.
|
|
105
|
+
*/
|
|
106
|
+
export declare function resolvePostpone(record: TaskRecord<unknown>, target: PostponeTarget, now: Date, reset: LimitReset | null): ResolvedPostpone;
|
|
107
|
+
/**
|
|
108
|
+
* The presets for `record` with their resolved earliest start (KTD6), for
|
|
109
|
+
* every surface: the API's `later_options`, the page menu, the clients and
|
|
110
|
+
* the Telegram buttons. `reset` is the known limit reset, or null.
|
|
111
|
+
*/
|
|
112
|
+
export declare function postponeOptions(record: TaskRecord<unknown>, now: Date, reset: LimitReset | null): PostponeOption[];
|
|
113
|
+
//# sourceMappingURL=postpone.d.ts.map
|
package/dist/postpone.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* "Later" (coherence KTD4 to KTD6): move a pending task so it never runs
|
|
3
|
+
* earlier than its current planned start. One preset vocabulary for every
|
|
4
|
+
* surface (KTD15), resolved in the process's local time zone. Pure apart
|
|
5
|
+
* from the limit-reset hint, which the helpers read and write through the
|
|
6
|
+
* store's meta table.
|
|
7
|
+
*
|
|
8
|
+
* `Scheduler.postponeTask` turns a resolved target into a reschedule: delay
|
|
9
|
+
* presets and `after_limit_reset` become `{notBefore}` (the scheduler's
|
|
10
|
+
* `startFields` rule: the deadline rises to at least `notBefore + 2 h` and
|
|
11
|
+
* a later one is kept), day presets become `{dayStart, dayEnd}` (the
|
|
12
|
+
* deadline is the window end), and a pin becomes `{at}`.
|
|
13
|
+
*/
|
|
14
|
+
import { taskActions } from "./history.js";
|
|
15
|
+
/** Presets in display order. */
|
|
16
|
+
export const POSTPONE_PRESETS = ["1h", "tonight", "tomorrow_morning", "after_limit_reset"];
|
|
17
|
+
/** Display labels of the presets (KTD15). */
|
|
18
|
+
export const POSTPONE_LABELS = {
|
|
19
|
+
"1h": "One hour later",
|
|
20
|
+
tonight: "Tonight",
|
|
21
|
+
tomorrow_morning: "Tomorrow morning",
|
|
22
|
+
after_limit_reset: "After limit reset",
|
|
23
|
+
};
|
|
24
|
+
/** Ledger meta key of the last reset time a caller gave (`{at, provider}`). */
|
|
25
|
+
export const LIMIT_RESET_HINT_META_KEY = "limit_reset_hint";
|
|
26
|
+
const MINUTE_MS = 60_000;
|
|
27
|
+
const HOUR_MS = 3_600_000;
|
|
28
|
+
/** `after_limit_reset` waits this long past the reset. */
|
|
29
|
+
const RESET_MARGIN_MS = 5 * MINUTE_MS;
|
|
30
|
+
/** Local hour the `tonight` preset starts; also the start of a channel's quiet hours (KTD10). */
|
|
31
|
+
export const TONIGHT_START_H = 22;
|
|
32
|
+
const TONIGHT_END_H = 6;
|
|
33
|
+
/** Local hour the `tomorrow_morning` preset starts; also the end of a channel's quiet hours (KTD10). */
|
|
34
|
+
export const MORNING_START_H = 9;
|
|
35
|
+
const MORNING_END_H = 12;
|
|
36
|
+
export const NEEDS_RESET_TIME = "needs the reset time";
|
|
37
|
+
/** The stored hint, when it is still in the future. */
|
|
38
|
+
export function readLimitResetHint(store, now) {
|
|
39
|
+
const raw = store?.getMeta(LIMIT_RESET_HINT_META_KEY);
|
|
40
|
+
if (!raw)
|
|
41
|
+
return null;
|
|
42
|
+
try {
|
|
43
|
+
const at = new Date(JSON.parse(raw).at ?? "");
|
|
44
|
+
return at.getTime() > now.getTime() ? { at, source: "hint" } : null;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** Store a caller-given reset as the hint for later tasks. */
|
|
51
|
+
export function writeLimitResetHint(store, at, provider) {
|
|
52
|
+
store?.setMeta(LIMIT_RESET_HINT_META_KEY, JSON.stringify({ at: at.toISOString(), ...(provider ? { provider } : {}) }));
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The reset `after_limit_reset` waits for (R14): the plugged-in source,
|
|
56
|
+
* else a still-future stored hint. A caller-given `reset_at` is handled by
|
|
57
|
+
* the caller before this, because it wins over both.
|
|
58
|
+
*/
|
|
59
|
+
export async function resolveLimitReset(record, now, source, store) {
|
|
60
|
+
const plugged = source ? await source(record, now) : null;
|
|
61
|
+
return plugged ?? readLimitResetHint(store, now);
|
|
62
|
+
}
|
|
63
|
+
/** The provider of a provider-call task, if the body parses. */
|
|
64
|
+
export function taskProvider(record) {
|
|
65
|
+
try {
|
|
66
|
+
return JSON.parse(record.bodyJson ?? "").provider;
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The planned start (KTD4): `scheduledFor` if in the future, else a future
|
|
74
|
+
* `notBefore`, else now. `future` is false for "now", where any target at
|
|
75
|
+
* or after now is later.
|
|
76
|
+
*/
|
|
77
|
+
export function plannedStart(record, now) {
|
|
78
|
+
for (const iso of [record.scheduledFor, record.notBefore]) {
|
|
79
|
+
const t = iso ? Date.parse(iso) : Number.NaN;
|
|
80
|
+
if (Number.isFinite(t) && t > now.getTime())
|
|
81
|
+
return { at: new Date(t), future: true };
|
|
82
|
+
}
|
|
83
|
+
return { at: now, future: false };
|
|
84
|
+
}
|
|
85
|
+
/** A local wall-clock instant `dayOffset` days after `base`'s local day. */
|
|
86
|
+
function localAt(base, dayOffset, hour) {
|
|
87
|
+
return new Date(base.getFullYear(), base.getMonth(), base.getDate() + dayOffset, hour, 0, 0, 0);
|
|
88
|
+
}
|
|
89
|
+
/** "23:00" on the local day of `now`, else "2026-10-23 23:00". */
|
|
90
|
+
export function localTimeLabel(at, now) {
|
|
91
|
+
const p = (n) => String(n).padStart(2, "0");
|
|
92
|
+
const time = `${p(at.getHours())}:${p(at.getMinutes())}`;
|
|
93
|
+
const sameDay = at.getFullYear() === now.getFullYear() && at.getMonth() === now.getMonth() && at.getDate() === now.getDate();
|
|
94
|
+
return sameDay ? time : `${at.getFullYear()}-${p(at.getMonth() + 1)}-${p(at.getDate())} ${time}`;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Resolve `target` for `record` at `now` (KTD4). `reset` is the reset an
|
|
98
|
+
* `after_limit_reset` target uses (already resolved by precedence); a
|
|
99
|
+
* target's own `resetAt` wins over it. Refuses a task that is not movable,
|
|
100
|
+
* a window with less than one hour left, a missing reset, and any start at
|
|
101
|
+
* or before the planned start.
|
|
102
|
+
*/
|
|
103
|
+
export function resolvePostpone(record, target, now, reset) {
|
|
104
|
+
if (!taskActions(record).later) {
|
|
105
|
+
return { ok: false, reason: `task is ${record.status}; only queued or scheduled provider-call tasks can move` };
|
|
106
|
+
}
|
|
107
|
+
const planned = plannedStart(record, now);
|
|
108
|
+
let start;
|
|
109
|
+
let reschedule;
|
|
110
|
+
if ("until" in target) {
|
|
111
|
+
start = target.until instanceof Date ? target.until : new Date(target.until);
|
|
112
|
+
if (Number.isNaN(start.getTime()))
|
|
113
|
+
return { ok: false, reason: `not an ISO-8601 time: ${JSON.stringify(target.until)}` };
|
|
114
|
+
reschedule = { at: start };
|
|
115
|
+
}
|
|
116
|
+
else if ("delayMs" in target) {
|
|
117
|
+
if (!Number.isFinite(target.delayMs) || target.delayMs <= 0) {
|
|
118
|
+
return { ok: false, reason: "a delay must be a positive duration" };
|
|
119
|
+
}
|
|
120
|
+
start = new Date(planned.at.getTime() + target.delayMs);
|
|
121
|
+
reschedule = { notBefore: start };
|
|
122
|
+
}
|
|
123
|
+
else if (target.preset === "1h") {
|
|
124
|
+
start = new Date(planned.at.getTime() + HOUR_MS);
|
|
125
|
+
reschedule = { notBefore: start };
|
|
126
|
+
}
|
|
127
|
+
else if (target.preset === "after_limit_reset") {
|
|
128
|
+
let at;
|
|
129
|
+
if (target.resetAt !== undefined) {
|
|
130
|
+
at = target.resetAt instanceof Date ? target.resetAt : new Date(target.resetAt);
|
|
131
|
+
if (Number.isNaN(at.getTime()))
|
|
132
|
+
return { ok: false, reason: `reset time is not ISO-8601: ${JSON.stringify(target.resetAt)}` };
|
|
133
|
+
if (at.getTime() <= now.getTime())
|
|
134
|
+
return { ok: false, reason: `the reset time ${at.toISOString()} is in the past` };
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
at = reset?.at;
|
|
138
|
+
}
|
|
139
|
+
if (!at)
|
|
140
|
+
return { ok: false, reason: NEEDS_RESET_TIME };
|
|
141
|
+
start = new Date(Math.max(at.getTime(), now.getTime()) + RESET_MARGIN_MS);
|
|
142
|
+
reschedule = { notBefore: start };
|
|
143
|
+
}
|
|
144
|
+
else if (target.preset === "tonight" || target.preset === "tomorrow_morning") {
|
|
145
|
+
let end;
|
|
146
|
+
if (target.preset === "tomorrow_morning") {
|
|
147
|
+
start = localAt(now, 1, MORNING_START_H);
|
|
148
|
+
end = localAt(now, 1, MORNING_END_H);
|
|
149
|
+
}
|
|
150
|
+
else if (now.getHours() < TONIGHT_END_H) {
|
|
151
|
+
start = now;
|
|
152
|
+
end = localAt(now, 0, TONIGHT_END_H);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
start = new Date(Math.max(localAt(now, 0, TONIGHT_START_H).getTime(), now.getTime()));
|
|
156
|
+
end = localAt(now, 1, TONIGHT_END_H);
|
|
157
|
+
}
|
|
158
|
+
if (end.getTime() - start.getTime() < HOUR_MS)
|
|
159
|
+
return { ok: false, reason: "less than one hour of tonight is left" };
|
|
160
|
+
reschedule = { dayStart: start, dayEnd: end };
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
return { ok: false, reason: `unknown preset ${JSON.stringify(target.preset)}` };
|
|
164
|
+
}
|
|
165
|
+
const later = planned.future ? start.getTime() > planned.at.getTime() : start.getTime() >= now.getTime();
|
|
166
|
+
if (!later) {
|
|
167
|
+
return { ok: false, reason: `already planned for ${localTimeLabel(planned.at, now)}` };
|
|
168
|
+
}
|
|
169
|
+
return { ok: true, notBefore: start, reschedule };
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* The presets for `record` with their resolved earliest start (KTD6), for
|
|
173
|
+
* every surface: the API's `later_options`, the page menu, the clients and
|
|
174
|
+
* the Telegram buttons. `reset` is the known limit reset, or null.
|
|
175
|
+
*/
|
|
176
|
+
export function postponeOptions(record, now, reset) {
|
|
177
|
+
return POSTPONE_PRESETS.map((preset) => {
|
|
178
|
+
const res = resolvePostpone(record, { preset }, now, reset);
|
|
179
|
+
const base = { preset, label: POSTPONE_LABELS[preset] };
|
|
180
|
+
return res.ok
|
|
181
|
+
? { ...base, not_before: res.notBefore.toISOString(), available: true }
|
|
182
|
+
: { ...base, not_before: null, available: false, reason: res.reason };
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
//# sourceMappingURL=postpone.js.map
|
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
*/
|
|
12
12
|
import { normalizeModelName } from "../energy.js";
|
|
13
13
|
import { conversation, ProviderRefusalError } from "./base.js";
|
|
14
|
+
import { budgetMaxTokens } from "./budget.js";
|
|
15
|
+
import { withLlmTimeout } from "./timeout.js";
|
|
14
16
|
/**
|
|
15
17
|
* Output ceiling when the caller sets none. The `claude-api` skill's
|
|
16
18
|
* non-streaming default: large enough not to truncate normal answers,
|
|
@@ -61,11 +63,13 @@ export class AnthropicAdapter {
|
|
|
61
63
|
return Boolean(this.client) || Boolean(this.apiKey);
|
|
62
64
|
}
|
|
63
65
|
async dispatch(model, prompt, options = {}) {
|
|
66
|
+
const maxTokens = budgetMaxTokens(model, prompt, options, DEFAULT_MAX_TOKENS);
|
|
64
67
|
const client = await this.getClient();
|
|
65
|
-
const
|
|
66
|
-
...requestParams(model, prompt, options),
|
|
68
|
+
const req = {
|
|
69
|
+
...requestParams(model, prompt, { ...options, maxTokens }),
|
|
67
70
|
metadata: options.metadata ? { user_id: undefined, ...options.metadata } : undefined,
|
|
68
|
-
}
|
|
71
|
+
};
|
|
72
|
+
const res = (await withLlmTimeout(this.provider, options.timeoutMs, (request) => request ? client.messages.create(req, request) : client.messages.create(req)));
|
|
69
73
|
assertNotRefused(res, model);
|
|
70
74
|
return {
|
|
71
75
|
text: messageText(res),
|
package/dist/providers/base.d.ts
CHANGED
|
@@ -19,7 +19,16 @@
|
|
|
19
19
|
* Adapter modules import the vendor SDK lazily so the package can be
|
|
20
20
|
* installed without forcing both SDK dependencies on every consumer.
|
|
21
21
|
*/
|
|
22
|
-
import type {
|
|
22
|
+
import type { RunSession } from "../run-session.js";
|
|
23
|
+
import type { JobCaps, ProviderCallSpec, TaskMessage } from "../types.js";
|
|
24
|
+
/** The agent session a task came from (KTD13). Only `claude-code` runs receive it. */
|
|
25
|
+
export interface DispatchOrigin {
|
|
26
|
+
sessionId: string;
|
|
27
|
+
/** The session's project directory. */
|
|
28
|
+
cwd: string;
|
|
29
|
+
agent: "claude-code" | "codex";
|
|
30
|
+
transcriptPath?: string;
|
|
31
|
+
}
|
|
23
32
|
export interface DispatchOptions {
|
|
24
33
|
/** Sampling temperature; vendor-specific defaults if omitted. */
|
|
25
34
|
temperature?: number;
|
|
@@ -36,9 +45,87 @@ export interface DispatchOptions {
|
|
|
36
45
|
* empty means a single-turn request, exactly as before.
|
|
37
46
|
*/
|
|
38
47
|
messages?: TaskMessage[];
|
|
48
|
+
/**
|
|
49
|
+
* Claude Code tool names for a `claude-code` run (`[]` = none). Absent
|
|
50
|
+
* means the adapter's default. Other adapters ignore it.
|
|
51
|
+
*/
|
|
52
|
+
tools?: string[];
|
|
53
|
+
/**
|
|
54
|
+
* The session that queued the task, for a run that resumes a copy of it.
|
|
55
|
+
* The scheduler passes it to `claude-code` tasks only.
|
|
56
|
+
*/
|
|
57
|
+
origin?: DispatchOrigin;
|
|
58
|
+
/**
|
|
59
|
+
* Per-call time limit in ms (a job's `llmTimeoutMs`). Overrides the
|
|
60
|
+
* adapter's own limit; adapters without one ignore it.
|
|
61
|
+
*/
|
|
62
|
+
timeoutMs?: number;
|
|
63
|
+
/**
|
|
64
|
+
* Per-call budget in USD (a job's `maxBudgetUsd`). `claude-code` passes it
|
|
65
|
+
* as `--max-budget-usd`; API adapters cap `maxTokens` with it and refuse a
|
|
66
|
+
* call whose input alone costs more (KTD8).
|
|
67
|
+
*/
|
|
68
|
+
maxBudgetUsd?: number;
|
|
69
|
+
/**
|
|
70
|
+
* Working directory of a job occurrence (KTD8). `claude-code` runs fresh
|
|
71
|
+
* there, never as a fork of a session; `~` is the home folder.
|
|
72
|
+
*/
|
|
73
|
+
cwd?: string;
|
|
74
|
+
/** The job the occurrence belongs to; the `command` adapter refuses a call without one. */
|
|
75
|
+
jobId?: string;
|
|
76
|
+
/** The shell step of a command job occurrence (`command` adapter only). */
|
|
77
|
+
command?: ProviderCallSpec["command"];
|
|
78
|
+
/** The tool-less summarize step after the shell step (`command` adapter only). */
|
|
79
|
+
summarize?: ProviderCallSpec["summarize"];
|
|
80
|
+
/** The job's caps (`command` adapter only; other adapters get `timeoutMs` and `maxBudgetUsd`). */
|
|
81
|
+
caps?: JobCaps;
|
|
82
|
+
/**
|
|
83
|
+
* A line the run printed while it runs, already scrubbed of secrets (the
|
|
84
|
+
* `command` adapter calls it). The scheduler records it as a `progress`
|
|
85
|
+
* event within the core caps; an adapter that has no output to report
|
|
86
|
+
* never calls it.
|
|
87
|
+
*/
|
|
88
|
+
onProgress?: (line: string) => void;
|
|
89
|
+
/**
|
|
90
|
+
* The run's output ended (the `command` adapter calls it when its shell
|
|
91
|
+
* step ends, before a summarize step): nothing more is recorded, and a
|
|
92
|
+
* line still waiting for its slot is dropped.
|
|
93
|
+
*/
|
|
94
|
+
onProgressEnd?: () => void;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Extra keys for the occurrence's `completed` or `failed` event (KTD7):
|
|
98
|
+
* the steps that ran, which cap stopped the run, whether output was cut.
|
|
99
|
+
* Never in the signed receipt.
|
|
100
|
+
*/
|
|
101
|
+
export interface RunDetails {
|
|
102
|
+
/** Steps of the run, in order: `command`, `prompt`. */
|
|
103
|
+
steps?: string[];
|
|
104
|
+
/** The cap that stopped the run: `command_timeout`, `llm_timeout`, `budget`. */
|
|
105
|
+
stopped_by?: "command_timeout" | "llm_timeout" | "budget";
|
|
106
|
+
/** True when the command output went over `outputCapBytes` and the rest was dropped. */
|
|
107
|
+
truncated?: boolean;
|
|
39
108
|
}
|
|
40
109
|
/** Prior turns followed by the current prompt as the last user turn. */
|
|
41
110
|
export declare function conversation(prompt: string, options?: Pick<DispatchOptions, "messages">): TaskMessage[];
|
|
111
|
+
/**
|
|
112
|
+
* The tokens one model used in a run (KTD9). `inputTokens` counts fresh
|
|
113
|
+
* input plus cache writes; cache reads are not counted. A count the model
|
|
114
|
+
* did not report is absent: the bill then uses the typical count, as on the
|
|
115
|
+
* single-model path (`billModelUsage`).
|
|
116
|
+
*/
|
|
117
|
+
export interface ModelUsage {
|
|
118
|
+
model: string;
|
|
119
|
+
inputTokens?: number;
|
|
120
|
+
outputTokens?: number;
|
|
121
|
+
/**
|
|
122
|
+
* Set only when `inputTokens` is absent because one input part was not a
|
|
123
|
+
* count: the sum of the parts that were counts. The bill charges the input
|
|
124
|
+
* at the larger of this and the typical input count, and the receipt
|
|
125
|
+
* leaves the input out. Adapter-to-core input only, never on a receipt.
|
|
126
|
+
*/
|
|
127
|
+
partialInputTokens?: number;
|
|
128
|
+
}
|
|
42
129
|
export interface DispatchResult {
|
|
43
130
|
/** The model's text reply, concatenated across content blocks. */
|
|
44
131
|
text: string;
|
|
@@ -47,9 +134,18 @@ export interface DispatchResult {
|
|
|
47
134
|
inputTokens?: number;
|
|
48
135
|
outputTokens?: number;
|
|
49
136
|
totalTokens?: number;
|
|
137
|
+
/** As `ModelUsage.partialInputTokens`, for the single-model path. */
|
|
138
|
+
partialInputTokens?: number;
|
|
50
139
|
};
|
|
51
140
|
/** Vendor identifier we used (e.g. "claude-sonnet-5", "gpt-6-sol"). */
|
|
52
141
|
model: string;
|
|
142
|
+
/**
|
|
143
|
+
* The tokens of each model the run used (KTD9). With two or more billed
|
|
144
|
+
* entries the scheduler bills each model with its own coefficients and
|
|
145
|
+
* writes the breakdown to the receipt; otherwise it bills `usage` under
|
|
146
|
+
* `model`, as before.
|
|
147
|
+
*/
|
|
148
|
+
modelUsage?: ModelUsage[];
|
|
53
149
|
/** Vendor name: "anthropic" | "openai" | "..." */
|
|
54
150
|
provider: string;
|
|
55
151
|
/** Raw vendor response object. Inspect at your own risk. */
|
|
@@ -60,6 +156,22 @@ export interface DispatchResult {
|
|
|
60
156
|
* means `text` is truncated. Absent when the provider does not report one.
|
|
61
157
|
*/
|
|
62
158
|
stopReason?: string;
|
|
159
|
+
/** Tools the run could use (claude-code only). Recorded on the receipt. */
|
|
160
|
+
tools?: string[];
|
|
161
|
+
/** Where the run happened: a fork of the origin session or a fresh one (claude-code only). Recorded on the receipt. */
|
|
162
|
+
session?: RunSession;
|
|
163
|
+
/** Extra keys for the `completed` event; not on the receipt. */
|
|
164
|
+
details?: RunDetails;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* An error the scheduler must not retry: the call may have done work that
|
|
168
|
+
* must not run twice (a job's shell step), or retrying cannot help (a
|
|
169
|
+
* budget the input alone exceeds). `details` go on the `failed` event.
|
|
170
|
+
*/
|
|
171
|
+
export declare class NonRetryableError extends Error {
|
|
172
|
+
readonly nonRetryable = true;
|
|
173
|
+
readonly details?: RunDetails;
|
|
174
|
+
constructor(message: string, details?: RunDetails);
|
|
63
175
|
}
|
|
64
176
|
/**
|
|
65
177
|
* Thrown when the provider declined the request (Anthropic
|
package/dist/providers/base.js
CHANGED
|
@@ -23,6 +23,21 @@
|
|
|
23
23
|
export function conversation(prompt, options = {}) {
|
|
24
24
|
return [...(options.messages ?? []), { role: "user", content: prompt }];
|
|
25
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* An error the scheduler must not retry: the call may have done work that
|
|
28
|
+
* must not run twice (a job's shell step), or retrying cannot help (a
|
|
29
|
+
* budget the input alone exceeds). `details` go on the `failed` event.
|
|
30
|
+
*/
|
|
31
|
+
export class NonRetryableError extends Error {
|
|
32
|
+
nonRetryable = true;
|
|
33
|
+
details;
|
|
34
|
+
constructor(message, details) {
|
|
35
|
+
super(message);
|
|
36
|
+
this.name = "NonRetryableError";
|
|
37
|
+
if (details)
|
|
38
|
+
this.details = details;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
26
41
|
/**
|
|
27
42
|
* Thrown when the provider declined the request (Anthropic
|
|
28
43
|
* `stop_reason: "refusal"`). The call returned HTTP 200, but its content
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-call budget of an API adapter (jobs KTD8): a job's `maxBudgetUsd`
|
|
3
|
+
* becomes a `maxTokens` ceiling through the list prices in
|
|
4
|
+
* `data/prices.json`. A call whose input alone costs more than the budget is
|
|
5
|
+
* refused before it is sent.
|
|
6
|
+
*
|
|
7
|
+
* The input is estimated at 4 characters per token; the cost uses list
|
|
8
|
+
* prices, not the caller's negotiated rate, so the ceiling is an estimate.
|
|
9
|
+
*/
|
|
10
|
+
import { NonRetryableError, type DispatchOptions } from "./base.js";
|
|
11
|
+
/** The call's input alone costs more than its budget. */
|
|
12
|
+
export declare class BudgetExceededError extends NonRetryableError {
|
|
13
|
+
constructor(model: string, inputUsd: number, budgetUsd: number);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The `maxTokens` to send. Without `options.maxBudgetUsd`: `options.maxTokens`
|
|
17
|
+
* as given. With it: the smaller of `options.maxTokens` (else `adapterDefault`)
|
|
18
|
+
* and what the budget leaves after the input; a ceiling above
|
|
19
|
+
* {@link OPEN_CEILING_TOKENS} with no explicit or default value is not sent.
|
|
20
|
+
* A model missing from the price table keeps the adapter default and logs
|
|
21
|
+
* one line. Throws {@link BudgetExceededError} when the input alone costs
|
|
22
|
+
* more than the budget.
|
|
23
|
+
*/
|
|
24
|
+
export declare function budgetMaxTokens(model: string, prompt: string, options: DispatchOptions, adapterDefault?: number): number | undefined;
|
|
25
|
+
//# sourceMappingURL=budget.d.ts.map
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-call budget of an API adapter (jobs KTD8): a job's `maxBudgetUsd`
|
|
3
|
+
* becomes a `maxTokens` ceiling through the list prices in
|
|
4
|
+
* `data/prices.json`. A call whose input alone costs more than the budget is
|
|
5
|
+
* refused before it is sent.
|
|
6
|
+
*
|
|
7
|
+
* The input is estimated at 4 characters per token; the cost uses list
|
|
8
|
+
* prices, not the caller's negotiated rate, so the ceiling is an estimate.
|
|
9
|
+
*/
|
|
10
|
+
import { priceForModel } from "../routing.js";
|
|
11
|
+
import { conversation, NonRetryableError } from "./base.js";
|
|
12
|
+
/** Above this many output tokens the ceiling is not sent: the model's own limit applies. */
|
|
13
|
+
const OPEN_CEILING_TOKENS = 32_000;
|
|
14
|
+
/** The call's input alone costs more than its budget. */
|
|
15
|
+
export class BudgetExceededError extends NonRetryableError {
|
|
16
|
+
constructor(model, inputUsd, budgetUsd) {
|
|
17
|
+
super(`stopped: budget ${budgetUsd} USD is below the input cost of about ${inputUsd.toFixed(4)} USD for ${model}; not sent`, { stopped_by: "budget" });
|
|
18
|
+
this.name = "BudgetExceededError";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** Models with no price that already logged their one line. */
|
|
22
|
+
const unpriced = new Set();
|
|
23
|
+
/**
|
|
24
|
+
* The `maxTokens` to send. Without `options.maxBudgetUsd`: `options.maxTokens`
|
|
25
|
+
* as given. With it: the smaller of `options.maxTokens` (else `adapterDefault`)
|
|
26
|
+
* and what the budget leaves after the input; a ceiling above
|
|
27
|
+
* {@link OPEN_CEILING_TOKENS} with no explicit or default value is not sent.
|
|
28
|
+
* A model missing from the price table keeps the adapter default and logs
|
|
29
|
+
* one line. Throws {@link BudgetExceededError} when the input alone costs
|
|
30
|
+
* more than the budget.
|
|
31
|
+
*/
|
|
32
|
+
export function budgetMaxTokens(model, prompt, options, adapterDefault) {
|
|
33
|
+
const budget = options.maxBudgetUsd;
|
|
34
|
+
if (budget === undefined || !(budget > 0))
|
|
35
|
+
return options.maxTokens;
|
|
36
|
+
const price = priceForModel(model);
|
|
37
|
+
if (!price) {
|
|
38
|
+
if (!unpriced.has(model)) {
|
|
39
|
+
unpriced.add(model);
|
|
40
|
+
// eslint-disable-next-line no-console
|
|
41
|
+
console.warn(`[tidegap] no price for ${model} in data/prices.json; its budget cap is not applied`);
|
|
42
|
+
}
|
|
43
|
+
return options.maxTokens;
|
|
44
|
+
}
|
|
45
|
+
const chars = [options.system ?? "", ...conversation(prompt, options).map((m) => m.content)].join("").length;
|
|
46
|
+
const inputUsd = (Math.ceil(chars / 4) / 1_000_000) * price.inUsdPerMtok;
|
|
47
|
+
if (inputUsd >= budget)
|
|
48
|
+
throw new BudgetExceededError(model, inputUsd, budget);
|
|
49
|
+
if (!(price.outUsdPerMtok > 0))
|
|
50
|
+
return options.maxTokens;
|
|
51
|
+
const allowed = Math.max(1, Math.floor(((budget - inputUsd) / price.outUsdPerMtok) * 1_000_000));
|
|
52
|
+
const wanted = options.maxTokens ?? adapterDefault;
|
|
53
|
+
if (wanted !== undefined)
|
|
54
|
+
return Math.min(wanted, allowed);
|
|
55
|
+
return allowed <= OPEN_CEILING_TOKENS ? allowed : undefined;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=budget.js.map
|
|
@@ -11,5 +11,9 @@ export const DEFAULT_MODEL_BY_PROVIDER = Object.freeze({
|
|
|
11
11
|
openai: "gpt-6-sol",
|
|
12
12
|
gemini: "gemini-3.8-flash",
|
|
13
13
|
ollama: "llama3.1",
|
|
14
|
+
// Claude Code picks its own default model; the adapter omits --model.
|
|
15
|
+
"claude-code": "default",
|
|
16
|
+
// A command job's shell step has no model.
|
|
17
|
+
command: "none",
|
|
14
18
|
});
|
|
15
19
|
//# sourceMappingURL=defaults.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A provider asks the scheduler to put a running task back in the queue
|
|
3
|
+
* until `retryAt` instead of failing it (for example a subscription usage
|
|
4
|
+
* limit that resets at a known time). Not retried; the series does not roll.
|
|
5
|
+
*/
|
|
6
|
+
export declare class ProviderDeferError extends Error {
|
|
7
|
+
readonly retryAt: Date;
|
|
8
|
+
name: string;
|
|
9
|
+
constructor(message: string, retryAt: Date);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The `retryAt` of a defer request, or undefined. Matches by `instanceof`
|
|
13
|
+
* or by name, because the CLI and core can load separate copies of the class.
|
|
14
|
+
*/
|
|
15
|
+
export declare function deferRetryAt(err: unknown): Date | undefined;
|
|
16
|
+
//# sourceMappingURL=defer.d.ts.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A provider asks the scheduler to put a running task back in the queue
|
|
3
|
+
* until `retryAt` instead of failing it (for example a subscription usage
|
|
4
|
+
* limit that resets at a known time). Not retried; the series does not roll.
|
|
5
|
+
*/
|
|
6
|
+
export class ProviderDeferError extends Error {
|
|
7
|
+
retryAt;
|
|
8
|
+
name = "ProviderDeferError";
|
|
9
|
+
constructor(message, retryAt) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.retryAt = retryAt;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The `retryAt` of a defer request, or undefined. Matches by `instanceof`
|
|
16
|
+
* or by name, because the CLI and core can load separate copies of the class.
|
|
17
|
+
*/
|
|
18
|
+
export function deferRetryAt(err) {
|
|
19
|
+
if (!err || typeof err !== "object")
|
|
20
|
+
return undefined;
|
|
21
|
+
const e = err;
|
|
22
|
+
if (!(err instanceof ProviderDeferError) && e.name !== "ProviderDeferError")
|
|
23
|
+
return undefined;
|
|
24
|
+
const at = e.retryAt instanceof Date
|
|
25
|
+
? e.retryAt
|
|
26
|
+
: typeof e.retryAt === "string" || typeof e.retryAt === "number"
|
|
27
|
+
? new Date(e.retryAt)
|
|
28
|
+
: undefined;
|
|
29
|
+
return at && Number.isFinite(at.getTime()) ? at : undefined;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=defer.js.map
|