@ody./pi-at 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +59 -2
- package/package.json +44 -4
- package/src/command/at-args.ts +32 -0
- package/src/command/job-menu.ts +55 -0
- package/src/command/register-at.ts +54 -0
- package/src/index.ts +7 -0
- package/src/schedule/job-state.ts +74 -0
- package/src/schedule/schedule.ts +85 -0
- package/src/time-parser.ts +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright © 2026 Cody Herriges
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the “Software”), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,60 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ody./pi-at
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
pi extension that schedules a one-time message into a session at a given time, like `at(1)`.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Queue one message for delivery to the current session at a later time, for example after a rate limit resets overnight.
|
|
8
|
+
- Pi stores jobs in the session file, and they survive `/reload` and `/resume`.
|
|
9
|
+
- Delivery is a `followUp`, so a job never interrupts running work.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
pi install npm:@ody./pi-at
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
From a checkout: `pi -e ./src/index.ts`.
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
| Command | Effect |
|
|
22
|
+
| --- | --- |
|
|
23
|
+
| `/at` | Open the job menu in TUI or RPC mode. |
|
|
24
|
+
| `/at TIME -- MESSAGE` | Schedule MESSAGE for TIME. Splits on the first ` -- `. |
|
|
25
|
+
| `/at list` | List pending jobs of this session. |
|
|
26
|
+
| `/at delete ID` | Cancel a pending job. |
|
|
27
|
+
|
|
28
|
+
The job menu lists pending jobs and offers guided creation, inspection, and cancellation. Escape dismisses a prompt without changing jobs. The menu requires TUI or RPC mode; direct commands also work in print and JSON modes.
|
|
29
|
+
|
|
30
|
+
### TIME forms
|
|
31
|
+
|
|
32
|
+
`chrono-node` parses TIME in local time, and the whole string must parse.
|
|
33
|
+
|
|
34
|
+
Accepted: `06:30`, `6:30am`, `0630`, `noon`, `midnight`, `teatime` (16:00), `tomorrow 6:30am`, `noon Jan 2`, `2027-01-02 06:30`, `now + 3 hours`, `06:30 + 2 days`.
|
|
35
|
+
|
|
36
|
+
Rejected with an error:
|
|
37
|
+
|
|
38
|
+
- Trailing text such as `06:30 foo bar`.
|
|
39
|
+
- A time at or before now, including `now`.
|
|
40
|
+
- `DD.MM.YY` and `MMDDYY`. Use `YYYY-MM-DD`.
|
|
41
|
+
- A leading `next`, such as `next friday`.
|
|
42
|
+
- An empty MESSAGE or a missing ` -- `.
|
|
43
|
+
|
|
44
|
+
## Keep pi running
|
|
45
|
+
|
|
46
|
+
Jobs fire from a timer inside the pi process, polled every 30 seconds, so delivery can be up to 30 seconds late. Pi must stay running until the fire time, for example in tmux.
|
|
47
|
+
|
|
48
|
+
Pi does not send a job whose time passes while it is not running. On the next start or resume, pi marks it `missed` and names it in a notification. Jobs belong to the session that scheduled them; a fork or clone does not fire them.
|
|
49
|
+
|
|
50
|
+
## Source map
|
|
51
|
+
|
|
52
|
+
One bullet per file under `src/`: the path in backticks, then its group and a short description.
|
|
53
|
+
|
|
54
|
+
- `src/index.ts` — root: extension entry
|
|
55
|
+
- `src/time-parser.ts` — root: TIME string to Date, via chrono-node
|
|
56
|
+
- `src/command/at-args.ts` — command: parse `/at` arguments
|
|
57
|
+
- `src/command/job-menu.ts` — command: interactive job management
|
|
58
|
+
- `src/command/register-at.ts` — command: register `/at` and run its subcommands
|
|
59
|
+
- `src/schedule/job-state.ts` — schedule: job entry types and pending replay
|
|
60
|
+
- `src/schedule/schedule.ts` — schedule: poll timer, delivery, overdue handling
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ody./pi-at",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Schedule a one-time message into a pi session at a given time, like at(1).",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"pi-package",
|
|
9
|
+
"pi-extension"
|
|
10
|
+
],
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://codeberg.org/ody/pi-at.git"
|
|
14
|
+
},
|
|
15
|
+
"pi": {
|
|
16
|
+
"extensions": [
|
|
17
|
+
"./src/index.ts"
|
|
18
|
+
]
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"src",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"test": "vitest run",
|
|
26
|
+
"typecheck": "tsc --noEmit",
|
|
27
|
+
"lint": "biome lint .",
|
|
28
|
+
"lint:fix": "biome lint --write .",
|
|
29
|
+
"check": "npm run lint && npm run typecheck && npm test"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"chrono-node": "2.10.3"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"@earendil-works/pi-coding-agent": ">=1.0.0",
|
|
36
|
+
"typebox": "*"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@biomejs/biome": "^2.6.7",
|
|
40
|
+
"@earendil-works/pi-coding-agent": "^1.5.2",
|
|
41
|
+
"@types/node": "^26.6.6",
|
|
42
|
+
"typebox": "^1.4.7",
|
|
43
|
+
"typescript": "7.0.2",
|
|
44
|
+
"vitest": "^5.1.0"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export type AtArgs =
|
|
2
|
+
| { kind: "menu" }
|
|
3
|
+
| { kind: "list" }
|
|
4
|
+
| { kind: "delete"; id: number }
|
|
5
|
+
| { kind: "add"; time: string; message: string }
|
|
6
|
+
| { kind: "error"; error: string };
|
|
7
|
+
|
|
8
|
+
const USAGE = "Usage: /at TIME -- MESSAGE | /at list | /at delete ID";
|
|
9
|
+
const SEPARATOR = " -- ";
|
|
10
|
+
|
|
11
|
+
export function parseAtArgs(args: string): AtArgs {
|
|
12
|
+
const trimmed = args.trim();
|
|
13
|
+
const [first = "", ...rest] = trimmed.split(/\s+/);
|
|
14
|
+
if (trimmed === "") return { kind: "menu" };
|
|
15
|
+
if (first === "list") {
|
|
16
|
+
if (rest.length !== 0) return { kind: "error", error: "Usage: /at list" };
|
|
17
|
+
return { kind: "list" };
|
|
18
|
+
}
|
|
19
|
+
if (first === "delete") {
|
|
20
|
+
if (rest.length !== 1 || !/^\d+$/.test(rest[0])) {
|
|
21
|
+
return { kind: "error", error: "Usage: /at delete ID" };
|
|
22
|
+
}
|
|
23
|
+
return { kind: "delete", id: Number(rest[0]) };
|
|
24
|
+
}
|
|
25
|
+
const split = args.indexOf(SEPARATOR);
|
|
26
|
+
if (split === -1) return { kind: "error", error: `Missing " -- " separator. ${USAGE}` };
|
|
27
|
+
const time = args.slice(0, split).trim();
|
|
28
|
+
const message = args.slice(split + SEPARATOR.length);
|
|
29
|
+
if (time === "") return { kind: "error", error: `Missing TIME. ${USAGE}` };
|
|
30
|
+
if (message.trim() === "") return { kind: "error", error: `Empty MESSAGE. ${USAGE}` };
|
|
31
|
+
return { kind: "add", time, message };
|
|
32
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { Job } from "../schedule/job-state.ts";
|
|
3
|
+
import type { Schedule } from "../schedule/schedule.ts";
|
|
4
|
+
import { parseTime } from "../time-parser.ts";
|
|
5
|
+
import { parseAtArgs } from "./at-args.ts";
|
|
6
|
+
|
|
7
|
+
const CREATE = "Create job";
|
|
8
|
+
const CANCEL = "Cancel job";
|
|
9
|
+
const BACK = "Back";
|
|
10
|
+
|
|
11
|
+
async function createJob(ctx: ExtensionCommandContext, schedule: Schedule, valid: () => boolean, signal: AbortSignal): Promise<void> {
|
|
12
|
+
const time = await ctx.ui.input("TIME", "e.g. tomorrow 6:30am", { signal });
|
|
13
|
+
if (time === undefined || !valid()) return;
|
|
14
|
+
const parsed = parseTime(time, new Date());
|
|
15
|
+
if (!parsed.ok) {
|
|
16
|
+
ctx.ui.notify(parsed.error, "error");
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const message = await ctx.ui.input("MESSAGE", undefined, { signal });
|
|
20
|
+
if (message === undefined || !valid()) return;
|
|
21
|
+
const args = parseAtArgs(`time -- ${message}`);
|
|
22
|
+
if (args.kind !== "add") {
|
|
23
|
+
ctx.ui.notify(args.kind === "error" ? args.error : "Invalid message", "error");
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (parsed.date.getTime() <= Date.now()) {
|
|
27
|
+
ctx.ui.notify(`Time "${time.trim()}" is not in the future.`, "error");
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const id = schedule.addJob(ctx, parsed.date, args.message);
|
|
31
|
+
ctx.ui.notify(`at: job ${id} scheduled for ${parsed.date.toLocaleString()}`, "info");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function inspectJob(ctx: ExtensionCommandContext, schedule: Schedule, job: Job, valid: () => boolean, signal: AbortSignal): Promise<void> {
|
|
35
|
+
const choice = await ctx.ui.select(`Job ${job.id}\n${new Date(job.fireAt).toLocaleString()}\n${job.message}`, [CANCEL, BACK], { signal });
|
|
36
|
+
if (choice !== CANCEL || !valid()) return;
|
|
37
|
+
if (schedule.cancelJob(ctx, job.id)) ctx.ui.notify(`at: job ${job.id} cancelled`, "info");
|
|
38
|
+
else ctx.ui.notify(`at: no pending job ${job.id}`, "error");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function openJobMenu(ctx: ExtensionCommandContext, schedule: Schedule, signal: AbortSignal): Promise<void> {
|
|
42
|
+
const sessionId = ctx.sessionManager.getSessionId();
|
|
43
|
+
const valid = (): boolean => !signal.aborted && ctx.sessionManager.getSessionId() === sessionId;
|
|
44
|
+
while (valid()) {
|
|
45
|
+
const jobs = schedule.listPending(ctx);
|
|
46
|
+
const labels = jobs.map((job) => `${job.id} ${new Date(job.fireAt).toLocaleString()} ${job.message.replace(/\s+/g, " ")}`);
|
|
47
|
+
const choice = await ctx.ui.select("Pending jobs", [CREATE, ...labels], { signal });
|
|
48
|
+
if (choice === undefined || !valid()) return;
|
|
49
|
+
if (choice === CREATE) await createJob(ctx, schedule, valid, signal);
|
|
50
|
+
else {
|
|
51
|
+
const job = jobs[labels.indexOf(choice)];
|
|
52
|
+
if (job) await inspectJob(ctx, schedule, job, valid, signal);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { Job } from "../schedule/job-state.ts";
|
|
3
|
+
import type { Schedule } from "../schedule/schedule.ts";
|
|
4
|
+
import { parseTime } from "../time-parser.ts";
|
|
5
|
+
import { parseAtArgs } from "./at-args.ts";
|
|
6
|
+
import { openJobMenu } from "./job-menu.ts";
|
|
7
|
+
|
|
8
|
+
const formatJob = (job: Job): string =>
|
|
9
|
+
`${job.id} ${new Date(job.fireAt).toLocaleString()} ${job.message}`;
|
|
10
|
+
|
|
11
|
+
export function registerAtCommand(pi: ExtensionAPI, schedule: Schedule): void {
|
|
12
|
+
const menus = new Set<AbortController>();
|
|
13
|
+
pi.on("session_shutdown", () => {
|
|
14
|
+
for (const menu of menus) menu.abort();
|
|
15
|
+
menus.clear();
|
|
16
|
+
});
|
|
17
|
+
pi.registerCommand("at", {
|
|
18
|
+
description: "Schedule a one-time message: /at TIME -- MESSAGE | list | delete ID",
|
|
19
|
+
handler: async (args, ctx) => {
|
|
20
|
+
const parsed = parseAtArgs(args);
|
|
21
|
+
if (parsed.kind === "menu") {
|
|
22
|
+
if (!ctx.hasUI || (ctx.mode !== "tui" && ctx.mode !== "rpc")) {
|
|
23
|
+
throw new Error("at: interactive menu requires TUI or RPC mode");
|
|
24
|
+
}
|
|
25
|
+
const controller = new AbortController();
|
|
26
|
+
menus.add(controller);
|
|
27
|
+
try {
|
|
28
|
+
await openJobMenu(ctx, schedule, controller.signal);
|
|
29
|
+
} finally {
|
|
30
|
+
menus.delete(controller);
|
|
31
|
+
}
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (parsed.kind === "error") {
|
|
35
|
+
ctx.ui.notify(parsed.error, "error");
|
|
36
|
+
} else if (parsed.kind === "list") {
|
|
37
|
+
const jobs = schedule.listPending(ctx);
|
|
38
|
+
ctx.ui.notify(jobs.length === 0 ? "at: no pending jobs" : jobs.map(formatJob).join("\n"), "info");
|
|
39
|
+
} else if (parsed.kind === "delete") {
|
|
40
|
+
const cancelled = schedule.cancelJob(ctx, parsed.id);
|
|
41
|
+
if (cancelled) ctx.ui.notify(`at: job ${parsed.id} cancelled`, "info");
|
|
42
|
+
else ctx.ui.notify(`at: no pending job ${parsed.id}`, "error");
|
|
43
|
+
} else {
|
|
44
|
+
const time = parseTime(parsed.time, new Date());
|
|
45
|
+
if (!time.ok) {
|
|
46
|
+
ctx.ui.notify(time.error, "error");
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const id = schedule.addJob(ctx, time.date, parsed.message);
|
|
50
|
+
ctx.ui.notify(`at: job ${id} scheduled for ${time.date.toLocaleString()}`, "info");
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { registerAtCommand } from "./command/register-at.ts";
|
|
3
|
+
import { registerSchedule } from "./schedule/schedule.ts";
|
|
4
|
+
|
|
5
|
+
export default function (pi: ExtensionAPI): void {
|
|
6
|
+
registerAtCommand(pi, registerSchedule(pi));
|
|
7
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export const JOB_ENTRY = "at-job";
|
|
2
|
+
export const DONE_ENTRY = "at-done";
|
|
3
|
+
|
|
4
|
+
export type DoneOutcome = "sent" | "cancelled" | "missed";
|
|
5
|
+
|
|
6
|
+
export interface Job {
|
|
7
|
+
id: number;
|
|
8
|
+
sessionId: string;
|
|
9
|
+
fireAt: number;
|
|
10
|
+
message: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface DoneRecord {
|
|
14
|
+
id: number;
|
|
15
|
+
outcome: DoneOutcome;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface EntryLike {
|
|
19
|
+
type: string;
|
|
20
|
+
customType?: string;
|
|
21
|
+
data?: unknown;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isJob(data: unknown): data is Job {
|
|
25
|
+
if (typeof data !== "object" || data === null) return false;
|
|
26
|
+
const d = data as Record<string, unknown>;
|
|
27
|
+
return (
|
|
28
|
+
typeof d.id === "number" &&
|
|
29
|
+
typeof d.sessionId === "string" &&
|
|
30
|
+
typeof d.fireAt === "number" &&
|
|
31
|
+
typeof d.message === "string"
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function isDone(data: unknown): data is DoneRecord {
|
|
36
|
+
if (typeof data !== "object" || data === null) return false;
|
|
37
|
+
const d = data as Record<string, unknown>;
|
|
38
|
+
return typeof d.id === "number" && typeof d.outcome === "string";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function customEntries(entries: readonly EntryLike[], customType: string): unknown[] {
|
|
42
|
+
return entries
|
|
43
|
+
.filter((e) => e.type === "custom" && e.customType === customType)
|
|
44
|
+
.map((e) => e.data);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function nextJobId(entries: readonly EntryLike[]): number {
|
|
48
|
+
let highest = 0;
|
|
49
|
+
for (const data of customEntries(entries, JOB_ENTRY)) {
|
|
50
|
+
if (isJob(data) && data.id > highest) highest = data.id;
|
|
51
|
+
}
|
|
52
|
+
return highest + 1;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function pendingJobs(entries: readonly EntryLike[], sessionId: string): Job[] {
|
|
56
|
+
const done = new Set<number>();
|
|
57
|
+
for (const data of customEntries(entries, DONE_ENTRY)) {
|
|
58
|
+
if (isDone(data)) done.add(data.id);
|
|
59
|
+
}
|
|
60
|
+
return customEntries(entries, JOB_ENTRY)
|
|
61
|
+
.filter(isJob)
|
|
62
|
+
.filter((job) => job.sessionId === sessionId && !done.has(job.id));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function isDue(job: Job, now: number): boolean {
|
|
66
|
+
return job.fireAt <= now;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function splitOverdue(jobs: readonly Job[], now: number): { overdue: Job[]; armed: Job[] } {
|
|
70
|
+
return {
|
|
71
|
+
overdue: jobs.filter((job) => isDue(job, now)),
|
|
72
|
+
armed: jobs.filter((job) => !isDue(job, now)),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import {
|
|
3
|
+
DONE_ENTRY,
|
|
4
|
+
type DoneOutcome,
|
|
5
|
+
isDue,
|
|
6
|
+
JOB_ENTRY,
|
|
7
|
+
type Job,
|
|
8
|
+
nextJobId,
|
|
9
|
+
pendingJobs,
|
|
10
|
+
splitOverdue,
|
|
11
|
+
} from "./job-state.ts";
|
|
12
|
+
|
|
13
|
+
export const POLL_INTERVAL_MS = 30_000;
|
|
14
|
+
|
|
15
|
+
export interface Schedule {
|
|
16
|
+
addJob(ctx: ExtensionContext, fireTime: Date, message: string): number;
|
|
17
|
+
listPending(ctx: ExtensionContext): Job[];
|
|
18
|
+
cancelJob(ctx: ExtensionContext, id: number): boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function registerSchedule(pi: ExtensionAPI): Schedule {
|
|
22
|
+
let timer: ReturnType<typeof setInterval> | undefined;
|
|
23
|
+
|
|
24
|
+
const pending = (ctx: ExtensionContext): Job[] =>
|
|
25
|
+
pendingJobs(ctx.sessionManager.getEntries(), ctx.sessionManager.getSessionId());
|
|
26
|
+
|
|
27
|
+
const finish = (id: number, outcome: DoneOutcome): void => {
|
|
28
|
+
pi.appendEntry(DONE_ENTRY, { id, outcome });
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const stop = (): void => {
|
|
32
|
+
if (timer !== undefined) clearInterval(timer);
|
|
33
|
+
timer = undefined;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const poll = (ctx: ExtensionContext): void => {
|
|
37
|
+
const now = Date.now();
|
|
38
|
+
for (const job of pending(ctx)) {
|
|
39
|
+
if (!isDue(job, now)) continue;
|
|
40
|
+
finish(job.id, "sent");
|
|
41
|
+
pi.sendUserMessage(job.message, { deliverAs: "followUp" });
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const arm = (ctx: ExtensionContext): void => {
|
|
46
|
+
stop();
|
|
47
|
+
timer = setInterval(() => poll(ctx), POLL_INTERVAL_MS);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const dropOverdue = (ctx: ExtensionContext): void => {
|
|
51
|
+
const { overdue } = splitOverdue(pending(ctx), Date.now());
|
|
52
|
+
for (const job of overdue) {
|
|
53
|
+
finish(job.id, "missed");
|
|
54
|
+
ctx.ui.notify(`at: job ${job.id} missed (pi was not running): ${job.message}`, "warning");
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
pi.on("session_start", (event, ctx) => {
|
|
59
|
+
if (event.reason === "startup" || event.reason === "resume") dropOverdue(ctx);
|
|
60
|
+
arm(ctx);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
pi.on("session_shutdown", () => {
|
|
64
|
+
stop();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
addJob(ctx, fireTime, message) {
|
|
69
|
+
const id = nextJobId(ctx.sessionManager.getEntries());
|
|
70
|
+
pi.appendEntry(JOB_ENTRY, {
|
|
71
|
+
id,
|
|
72
|
+
sessionId: ctx.sessionManager.getSessionId(),
|
|
73
|
+
fireAt: fireTime.getTime(),
|
|
74
|
+
message,
|
|
75
|
+
});
|
|
76
|
+
return id;
|
|
77
|
+
},
|
|
78
|
+
listPending: pending,
|
|
79
|
+
cancelJob(ctx, id) {
|
|
80
|
+
if (!pending(ctx).some((job) => job.id === id)) return false;
|
|
81
|
+
finish(id, "cancelled");
|
|
82
|
+
return true;
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as chrono from "chrono-node/en";
|
|
2
|
+
|
|
3
|
+
export type ParseTimeResult = { ok: true; date: Date } | { ok: false; error: string };
|
|
4
|
+
|
|
5
|
+
const REJECTED_FORMS = [/^\d{1,2}\.\d{1,2}\.\d{2,4}$/, /^\d{6}$/, /^next(\s|$)/i];
|
|
6
|
+
|
|
7
|
+
function rewrite(input: string): string {
|
|
8
|
+
return input
|
|
9
|
+
.replace(/\bteatime\b/gi, "16:00")
|
|
10
|
+
.replace(/^(\d{2})(\d{2})(?=\s|$)/, "$1:$2");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function parseTime(input: string, now: Date): ParseTimeResult {
|
|
14
|
+
const trimmed = input.trim();
|
|
15
|
+
if (REJECTED_FORMS.some((form) => form.test(trimmed))) {
|
|
16
|
+
return { ok: false, error: `Unsupported time "${trimmed}". Use YYYY-MM-DD, e.g. 2027-01-02 06:30.` };
|
|
17
|
+
}
|
|
18
|
+
const rewritten = rewrite(trimmed);
|
|
19
|
+
const results = chrono.parse(rewritten, now, { forwardDate: true });
|
|
20
|
+
const [result] = results;
|
|
21
|
+
if (results.length !== 1 || result.index !== 0 || result.text !== rewritten) {
|
|
22
|
+
return { ok: false, error: `Cannot parse time "${trimmed}". Use forms like 06:30, noon, tomorrow 6:30am, or YYYY-MM-DD 06:30.` };
|
|
23
|
+
}
|
|
24
|
+
const date = result.date();
|
|
25
|
+
if (date.getTime() <= now.getTime()) {
|
|
26
|
+
return { ok: false, error: `Time "${trimmed}" is not in the future.` };
|
|
27
|
+
}
|
|
28
|
+
return { ok: true, date };
|
|
29
|
+
}
|