@mccaskie-labs/workbench-nudges 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/README.md +65 -2
- package/dist/cli.d.ts +12 -0
- package/dist/cli.js +146 -0
- package/dist/config.d.ts +9 -0
- package/dist/config.js +140 -0
- package/dist/errors.d.ts +17 -0
- package/dist/errors.js +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +15 -0
- package/dist/migration.d.ts +4 -0
- package/dist/migration.js +378 -0
- package/dist/nudges.d.ts +179 -0
- package/dist/nudges.js +381 -0
- package/dist/starters.d.ts +80 -0
- package/dist/starters.js +61 -0
- package/dist/styles.d.ts +34 -0
- package/dist/styles.js +70 -0
- package/dist/time.d.ts +18 -0
- package/dist/time.js +100 -0
- package/dist/types.d.ts +136 -0
- package/dist/types.js +3 -0
- package/dist/ui.d.ts +39 -0
- package/dist/ui.js +83 -0
- package/dist/words.d.ts +4 -0
- package/dist/words.js +121 -0
- package/package.json +48 -4
package/dist/time.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// Time in the project's time zone: step lengths ("3d" is three calendar days
|
|
2
|
+
// on the local clock, right across a daylight saving change), quiet hours,
|
|
3
|
+
// and "today" for the digest and date_passed. Plain code, no AI.
|
|
4
|
+
const DURATION = /^([1-9][0-9]{0,3})([mhd])$/;
|
|
5
|
+
const CLOCK = /^([01][0-9]|2[0-3]):([0-5][0-9])$/;
|
|
6
|
+
/** A duration in minutes, or null when it isn't one ("3d", "12h", "30m"). */
|
|
7
|
+
export function durationMinutes(text) {
|
|
8
|
+
if (typeof text !== "string")
|
|
9
|
+
return null;
|
|
10
|
+
const m = DURATION.exec(text);
|
|
11
|
+
if (!m)
|
|
12
|
+
return null;
|
|
13
|
+
const n = Number(m[1]);
|
|
14
|
+
return m[2] === "m" ? n : m[2] === "h" ? n * 60 : n * 1440;
|
|
15
|
+
}
|
|
16
|
+
/** "21:00" in minutes after midnight, or null. */
|
|
17
|
+
export function clockMinutes(text) {
|
|
18
|
+
if (typeof text !== "string")
|
|
19
|
+
return null;
|
|
20
|
+
const m = CLOCK.exec(text);
|
|
21
|
+
return m ? Number(m[1]) * 60 + Number(m[2]) : null;
|
|
22
|
+
}
|
|
23
|
+
export function validZone(tz) {
|
|
24
|
+
if (typeof tz !== "string" || !tz)
|
|
25
|
+
return false;
|
|
26
|
+
try {
|
|
27
|
+
new Intl.DateTimeFormat("en", { timeZone: tz });
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function localParts(at, timeZone) {
|
|
35
|
+
const p = Object.fromEntries(new Intl.DateTimeFormat("en-CA", { timeZone, year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit", hourCycle: "h23" })
|
|
36
|
+
.formatToParts(at)
|
|
37
|
+
.filter((x) => x.type !== "literal")
|
|
38
|
+
.map((x) => [x.type, Number(x.value)]));
|
|
39
|
+
return { year: p.year, month: p.month, day: p.day, hour: p.hour, minute: p.minute, second: p.second };
|
|
40
|
+
}
|
|
41
|
+
/** How far the zone's clock is ahead of UTC at an instant, in milliseconds. */
|
|
42
|
+
function offsetAt(at, timeZone) {
|
|
43
|
+
const p = localParts(at, timeZone);
|
|
44
|
+
return Date.UTC(p.year, p.month - 1, p.day, p.hour, p.minute, p.second) - Math.floor(at.getTime() / 1000) * 1000;
|
|
45
|
+
}
|
|
46
|
+
/** The instant a local wall-clock time (given as if it were UTC) happens in the zone. */
|
|
47
|
+
function fromLocal(wall, timeZone) {
|
|
48
|
+
const guess = wall - offsetAt(new Date(wall), timeZone);
|
|
49
|
+
return new Date(wall - offsetAt(new Date(guess), timeZone));
|
|
50
|
+
}
|
|
51
|
+
const wallOf = (at, tz) => {
|
|
52
|
+
const p = localParts(at, tz);
|
|
53
|
+
return Date.UTC(p.year, p.month - 1, p.day, p.hour, p.minute, p.second) + (at.getTime() % 1000);
|
|
54
|
+
};
|
|
55
|
+
/** `at` plus a duration: days on the local clock, minutes and hours as elapsed time. */
|
|
56
|
+
export function addDuration(at, duration, timeZone) {
|
|
57
|
+
const minutes = durationMinutes(duration);
|
|
58
|
+
if (minutes === null)
|
|
59
|
+
throw new Error(`${duration} isn't a duration such as 3d, 12h or 30m`);
|
|
60
|
+
if (duration.endsWith("d"))
|
|
61
|
+
return fromLocal(wallOf(at, timeZone) + minutes * 60_000, timeZone);
|
|
62
|
+
return new Date(at.getTime() + minutes * 60_000);
|
|
63
|
+
}
|
|
64
|
+
/** `at`, or the end of the quiet hours when `at` falls inside them. */
|
|
65
|
+
export function outOfQuiet(at, quiet, timeZone) {
|
|
66
|
+
if (!quiet)
|
|
67
|
+
return at;
|
|
68
|
+
const from = clockMinutes(quiet.from);
|
|
69
|
+
const to = clockMinutes(quiet.to);
|
|
70
|
+
if (from === to)
|
|
71
|
+
return at;
|
|
72
|
+
const p = localParts(at, timeZone);
|
|
73
|
+
const m = p.hour * 60 + p.minute;
|
|
74
|
+
const inside = from < to ? m >= from && m < to : m >= from || m < to;
|
|
75
|
+
if (!inside)
|
|
76
|
+
return at;
|
|
77
|
+
// Over midnight and still before it: the quiet hours end tomorrow.
|
|
78
|
+
const tomorrow = from > to && m >= from;
|
|
79
|
+
const wall = Date.UTC(p.year, p.month - 1, p.day + (tomorrow ? 1 : 0), Math.floor(to / 60), to % 60, 0);
|
|
80
|
+
return fromLocal(wall, timeZone);
|
|
81
|
+
}
|
|
82
|
+
/** Today in the zone, YYYY-MM-DD. */
|
|
83
|
+
export function todayIn(timeZone, at) {
|
|
84
|
+
const p = localParts(at, timeZone);
|
|
85
|
+
return `${p.year}-${String(p.month).padStart(2, "0")}-${String(p.day).padStart(2, "0")}`;
|
|
86
|
+
}
|
|
87
|
+
/** Minutes after local midnight at `at`. */
|
|
88
|
+
export function minutesIn(timeZone, at) {
|
|
89
|
+
const p = localParts(at, timeZone);
|
|
90
|
+
return p.hour * 60 + p.minute;
|
|
91
|
+
}
|
|
92
|
+
/** A duration for people: "3 days", "1 day", "12 hours", "30 minutes". */
|
|
93
|
+
export function durationWords(duration) {
|
|
94
|
+
const m = DURATION.exec(duration);
|
|
95
|
+
if (!m)
|
|
96
|
+
return duration;
|
|
97
|
+
const n = Number(m[1]);
|
|
98
|
+
const unit = m[2] === "d" ? "day" : m[2] === "h" ? "hour" : "minute";
|
|
99
|
+
return `${n} ${unit}${n === 1 ? "" : "s"}`;
|
|
100
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
export type Lang = "en" | "tl" | "taglish";
|
|
2
|
+
/** What a nudge is about: a row in the app's own tables (an enquiry, an outreach contact). Never a person's words. */
|
|
3
|
+
export interface Subject {
|
|
4
|
+
type: string;
|
|
5
|
+
id: string;
|
|
6
|
+
}
|
|
7
|
+
/** A length of time: a whole number and m (minutes), h (hours) or d (days), such as "3d". */
|
|
8
|
+
export type Duration = `${number}${"m" | "h" | "d"}`;
|
|
9
|
+
/** One follow-up of a rule. */
|
|
10
|
+
export interface NudgeStep {
|
|
11
|
+
/** How long after the start (the first step) or the previous step it comes due. */
|
|
12
|
+
after: Duration;
|
|
13
|
+
/** The reply guide the follow-up is drafted with (workbench/replies/<guide>.json). */
|
|
14
|
+
guide: string;
|
|
15
|
+
/** What this follow-up should say, passed to the drafter as its note. */
|
|
16
|
+
say?: string;
|
|
17
|
+
}
|
|
18
|
+
/** How the owner hears about a due nudge: one email a day with a count, an email as each comes due, or only the app's list. */
|
|
19
|
+
export type EmailOwner = "daily_digest" | "each" | "none";
|
|
20
|
+
export interface NudgeRule {
|
|
21
|
+
/** The app's event that starts it (quote_sent), for the app's own wiring and the docs. */
|
|
22
|
+
starts_on: string;
|
|
23
|
+
/** The events that end it (they_replied, booked, closed): touch() with one of these stops the nudge. */
|
|
24
|
+
stops_on: readonly string[];
|
|
25
|
+
/** The events that start it again from the first step (default none). */
|
|
26
|
+
restarts_on?: readonly string[];
|
|
27
|
+
steps: readonly NudgeStep[];
|
|
28
|
+
skip_if?: {
|
|
29
|
+
/** The facts' `date` (YYYY-MM-DD) has passed in the project's time zone: the nudge ends with date_passed, no draft. */
|
|
30
|
+
date_passed?: boolean;
|
|
31
|
+
/** The facts say `available: "no"`: offer the facts' `alternatives` in the draft, or end the nudge. */
|
|
32
|
+
date_unavailable?: "offer_alternatives" | "stop";
|
|
33
|
+
};
|
|
34
|
+
/** Default daily_digest. */
|
|
35
|
+
email_owner?: EmailOwner;
|
|
36
|
+
/** A due nudge left alone after its last step ends this long after it came due. Default 7d. */
|
|
37
|
+
ends_after?: Duration;
|
|
38
|
+
/** The drafts' language. Default: the file's language. A refresh can choose per nudge. */
|
|
39
|
+
language?: Lang;
|
|
40
|
+
}
|
|
41
|
+
/** workbench/nudges.json, checked. */
|
|
42
|
+
export interface NudgeConfig {
|
|
43
|
+
format: 1;
|
|
44
|
+
/** Quiet hours and the digest's time are in this time zone (IANA name). */
|
|
45
|
+
time_zone: string;
|
|
46
|
+
/** A step that would come due in these hours moves to their end. "HH:MM", 24-hour. */
|
|
47
|
+
quiet_hours?: {
|
|
48
|
+
from: string;
|
|
49
|
+
to: string;
|
|
50
|
+
};
|
|
51
|
+
/** When the daily digest goes, "HH:MM". Default: the end of the quiet hours, or 08:00. */
|
|
52
|
+
digest_at?: string;
|
|
53
|
+
/** The drafts' language. Default en. */
|
|
54
|
+
language?: Lang;
|
|
55
|
+
/** No rule may have more steps than this; 1 to 3 (default 3), so nobody is chased a fourth time. */
|
|
56
|
+
max_steps?: number;
|
|
57
|
+
/** Ended nudges are deleted this many days after they end. Default 90. */
|
|
58
|
+
retention_days?: number;
|
|
59
|
+
/** A draft is deleted when its nudge ends, and after this many days at most. Default 30, at most 30. */
|
|
60
|
+
draft_days?: number;
|
|
61
|
+
/** The plans feature a draft needs (default "nudges"), and a meter each draft counts against (none by default). */
|
|
62
|
+
plans?: {
|
|
63
|
+
feature?: string;
|
|
64
|
+
meter?: string;
|
|
65
|
+
};
|
|
66
|
+
rules: Readonly<Record<string, NudgeRule>>;
|
|
67
|
+
}
|
|
68
|
+
export type NudgeStatus = "waiting" | "due" | "done" | "snoozed" | "dismissed" | "stopped";
|
|
69
|
+
export declare const NUDGE_STATUSES: readonly NudgeStatus[];
|
|
70
|
+
/** Why a due nudge shows, and what its draft did. */
|
|
71
|
+
export type DueReason = "no_reply" | "date_unavailable" | "not_on_plan" | "draft_failed" | "no_ai";
|
|
72
|
+
/** A draft as the reply drafter returned it (workbench-ai's ReplyDraft fits). */
|
|
73
|
+
export interface NudgeDraft {
|
|
74
|
+
text: string;
|
|
75
|
+
language: Lang;
|
|
76
|
+
asked: readonly string[];
|
|
77
|
+
answered: readonly string[];
|
|
78
|
+
needs_human_check: boolean;
|
|
79
|
+
check_reasons: readonly string[];
|
|
80
|
+
link?: {
|
|
81
|
+
url: string;
|
|
82
|
+
code?: string;
|
|
83
|
+
imageUrl?: string;
|
|
84
|
+
expiresAt?: string;
|
|
85
|
+
};
|
|
86
|
+
workbench: {
|
|
87
|
+
request_id: string;
|
|
88
|
+
cost_usd: number;
|
|
89
|
+
model: string;
|
|
90
|
+
alias: string;
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
export interface Nudge {
|
|
94
|
+
id: string;
|
|
95
|
+
rule: string;
|
|
96
|
+
account: string;
|
|
97
|
+
subject: Subject;
|
|
98
|
+
/** The step that is waiting, or the one that is due (from 1). */
|
|
99
|
+
step: number;
|
|
100
|
+
/** When it comes due (waiting, snoozed), or when the next step or the end comes (due). */
|
|
101
|
+
due_at: string;
|
|
102
|
+
status: NudgeStatus;
|
|
103
|
+
/** Why it stopped or ended: the touch event (they_replied), date_passed, date_unavailable, steps_done, subject_gone, or the owner's reason. */
|
|
104
|
+
stop_reason: string | null;
|
|
105
|
+
/** Why the due nudge shows. */
|
|
106
|
+
reason: DueReason | null;
|
|
107
|
+
/** When the current step came due. */
|
|
108
|
+
made_due_at: string | null;
|
|
109
|
+
started_at: string;
|
|
110
|
+
ended_at: string | null;
|
|
111
|
+
}
|
|
112
|
+
/** A due nudge for the app's Waiting for you list, with its draft. */
|
|
113
|
+
export interface DueNudge {
|
|
114
|
+
id: string;
|
|
115
|
+
rule: string;
|
|
116
|
+
subject: Subject;
|
|
117
|
+
step: number;
|
|
118
|
+
due_at: string;
|
|
119
|
+
draft?: NudgeDraft;
|
|
120
|
+
/** One line for the owner: "No reply in 3 days". English; the list's words carry the other languages. */
|
|
121
|
+
reason: string;
|
|
122
|
+
reason_code: DueReason;
|
|
123
|
+
/** When this step came due. */
|
|
124
|
+
made_due_at: string;
|
|
125
|
+
/** How many steps the rule has. */
|
|
126
|
+
steps: number;
|
|
127
|
+
/** This step's wait ("3d"), for the reason in other languages. */
|
|
128
|
+
after: string;
|
|
129
|
+
}
|
|
130
|
+
export interface TickResult {
|
|
131
|
+
made_due: number;
|
|
132
|
+
drafted: number;
|
|
133
|
+
emailed: number;
|
|
134
|
+
/** Nudges that ended in this tick (skip_if, a refresh that said stop, a last step left alone). */
|
|
135
|
+
ended: number;
|
|
136
|
+
}
|
package/dist/types.js
ADDED
package/dist/ui.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Nudges } from "./nudges.ts";
|
|
2
|
+
import type { DueNudge, Lang, NudgeDraft } from "./types.ts";
|
|
3
|
+
/** The project's strings lookup: `t('ui.wbnudge.sent')`. Undefined falls back to the words of `lang`. */
|
|
4
|
+
export type Words = (key: string, params?: Readonly<Record<string, string | number>>) => string | undefined;
|
|
5
|
+
export interface ListOptions {
|
|
6
|
+
/** The project's route that hands the posted fields to nudgeAction. */
|
|
7
|
+
endpoint: string;
|
|
8
|
+
t?: Words;
|
|
9
|
+
/** The words' language when `t` has none. Default en. */
|
|
10
|
+
lang?: Lang;
|
|
11
|
+
/** What each nudge is about, from the app's own data ("Maria · Sat 12 Dec"). Shown as the nudge's title. */
|
|
12
|
+
label?: (n: DueNudge) => string | undefined;
|
|
13
|
+
/** workbench-ai's renderReplyBox, so the draft gets the reply drafter's box and its Copy button. */
|
|
14
|
+
replyBox?: (draft: NudgeDraft, opts: {
|
|
15
|
+
hidden?: Readonly<Record<string, string>>;
|
|
16
|
+
}) => string;
|
|
17
|
+
}
|
|
18
|
+
/** One due nudge: what it's about, why, which step, the draft and its buttons. */
|
|
19
|
+
export declare function renderNudge(n: DueNudge, o: ListOptions): string;
|
|
20
|
+
/** The account's Waiting for you list (from due()), with its count. */
|
|
21
|
+
export declare function renderNudgeList(due: readonly DueNudge[], o: ListOptions): string;
|
|
22
|
+
export interface ActionResult {
|
|
23
|
+
status: number;
|
|
24
|
+
body: {
|
|
25
|
+
ok: true;
|
|
26
|
+
action: string;
|
|
27
|
+
} | {
|
|
28
|
+
ok: false;
|
|
29
|
+
error: string;
|
|
30
|
+
message: string;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The project's endpoint (behind the owner's sign-in) hands the posted form
|
|
35
|
+
* fields here with the signed-in owner's account: `sent` (the follow-up went:
|
|
36
|
+
* touch we_replied), `snooze` (back in a day, out of the quiet hours) or
|
|
37
|
+
* `dismiss`. A nudge of another account is answered as unknown.
|
|
38
|
+
*/
|
|
39
|
+
export declare function nudgeAction(nudges: Nudges, body: Record<string, unknown>, account: string, now?: Date): Promise<ActionResult>;
|
package/dist/ui.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// The Waiting for you list (docs/capabilities/09-follow-up-nudges.md): plain
|
|
2
|
+
// HTML strings, no script of its own, mobile first. Each due nudge shows why
|
|
3
|
+
// (no reply in 3 days), which step it is, its draft, and three buttons, each
|
|
4
|
+
// a form post to the project's endpoint (behind the owner's sign-in), which
|
|
5
|
+
// hands the fields to nudgeAction. The draft is the reply drafter's box when
|
|
6
|
+
// the app passes workbench-ai's renderReplyBox (Copy works with its
|
|
7
|
+
// REPLY_SCRIPT); otherwise a plain text box to copy from. Every element
|
|
8
|
+
// carries a wb-nudge-* class (NUDGE_STYLES), and every button is at least
|
|
9
|
+
// 44 px tall.
|
|
10
|
+
import { NudgesError } from "./errors.js";
|
|
11
|
+
import { addDuration, outOfQuiet } from "./time.js";
|
|
12
|
+
import { NUDGE_WORDS } from "./words.js";
|
|
13
|
+
const esc = (s) => s.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]);
|
|
14
|
+
function fill(text, params = {}) {
|
|
15
|
+
return text.replace(/\{(\w+)\}/g, (m, k) => (Object.hasOwn(params, k) ? String(params[k]) : m));
|
|
16
|
+
}
|
|
17
|
+
function words(o) {
|
|
18
|
+
const base = NUDGE_WORDS[o.lang ?? "en"] ?? NUDGE_WORDS.en;
|
|
19
|
+
return (key, params = {}) => o.t?.(`ui.wbnudge.${key}`, params) ?? fill(base[key], params);
|
|
20
|
+
}
|
|
21
|
+
/** "3 days", "1 day", "12 hours" in the list's language. */
|
|
22
|
+
function afterWords(after, w) {
|
|
23
|
+
const m = /^(\d+)([mhd])$/.exec(after);
|
|
24
|
+
if (!m)
|
|
25
|
+
return after;
|
|
26
|
+
const n = Number(m[1]);
|
|
27
|
+
const unit = m[2] === "d" ? "day" : m[2] === "h" ? "hour" : "minute";
|
|
28
|
+
return `${n} ${w(`unit.${unit}${n === 1 ? "" : "s"}`)}`;
|
|
29
|
+
}
|
|
30
|
+
/** One due nudge: what it's about, why, which step, the draft and its buttons. */
|
|
31
|
+
export function renderNudge(n, o) {
|
|
32
|
+
const w = words(o);
|
|
33
|
+
const form = (action, cls, label) => `<form class="wb-nudge-form" method="post" action="${esc(o.endpoint)}"><input type="hidden" name="nudge" value="${esc(n.id)}"><input type="hidden" name="action" value="${action}"><button class="${cls}" type="submit">${esc(label)}</button></form>`;
|
|
34
|
+
const title = o.label?.(n);
|
|
35
|
+
const reason = n.reason_code === "no_reply" ? w("reason.no_reply", { after: afterWords(n.after, w) }) : `${w("reason.no_reply", { after: afterWords(n.after, w) })}. ${w(`reason.${n.reason_code}`)}`;
|
|
36
|
+
const draft = n.draft
|
|
37
|
+
? o.replyBox
|
|
38
|
+
? o.replyBox(n.draft, { hidden: { nudge: n.id } })
|
|
39
|
+
: `<textarea class="wb-nudge-text" readonly rows="5" lang="${n.draft.language === "en" ? "en" : "fil"}">${esc(n.draft.text)}</textarea>`
|
|
40
|
+
: `<p class="wb-nudge-none">${esc(w("no_draft"))}</p>`;
|
|
41
|
+
const cls = ["wb-nudge", n.reason_code !== "no_reply" ? "wb-nudge-taken" : ""].filter(Boolean).join(" ");
|
|
42
|
+
return `<article class="${cls}" id="wb-nudge-${esc(n.id)}">${title ? `<h3 class="wb-nudge-title">${esc(title)}</h3>` : ""}<p class="wb-nudge-reason">${esc(reason)}</p><p class="wb-nudge-step">${esc(w("step", { step: n.step, steps: n.steps }))}</p><div class="wb-nudge-draft">${draft}</div><div class="wb-nudge-actions">${form("sent", "wb-nudge-sent", w("sent"))}${form("snooze", "wb-nudge-snooze", w("snooze"))}${form("dismiss", "wb-nudge-dismiss", w("dismiss"))}</div></article>`;
|
|
43
|
+
}
|
|
44
|
+
/** The account's Waiting for you list (from due()), with its count. */
|
|
45
|
+
export function renderNudgeList(due, o) {
|
|
46
|
+
const w = words(o);
|
|
47
|
+
if (due.length === 0)
|
|
48
|
+
return `<section class="wb-nudge-list"><h2 class="wb-nudge-list-title">${esc(w("list.title"))}</h2><p class="wb-nudge-empty">${esc(w("list.empty"))}</p></section>`;
|
|
49
|
+
const items = due.map((n) => `<li class="wb-nudge-item">${renderNudge(n, o)}</li>`).join("");
|
|
50
|
+
return `<section class="wb-nudge-list"><h2 class="wb-nudge-list-title">${esc(w("list.title"))} <span class="wb-nudge-count">${due.length}</span></h2><ul class="wb-nudge-items">${items}</ul></section>`;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The project's endpoint (behind the owner's sign-in) hands the posted form
|
|
54
|
+
* fields here with the signed-in owner's account: `sent` (the follow-up went:
|
|
55
|
+
* touch we_replied), `snooze` (back in a day, out of the quiet hours) or
|
|
56
|
+
* `dismiss`. A nudge of another account is answered as unknown.
|
|
57
|
+
*/
|
|
58
|
+
export async function nudgeAction(nudges, body, account, now = new Date()) {
|
|
59
|
+
try {
|
|
60
|
+
const action = String(body.action ?? "");
|
|
61
|
+
if (!["sent", "snooze", "dismiss"].includes(action))
|
|
62
|
+
throw new NudgesError("bad_input", "action must be sent, snooze or dismiss");
|
|
63
|
+
const n = await nudges.get(String(body.nudge ?? ""));
|
|
64
|
+
if (!n || n.account !== account || !["waiting", "due", "snoozed"].includes(n.status))
|
|
65
|
+
throw new NudgesError("unknown_nudge", "no such nudge, or it has ended");
|
|
66
|
+
if (action === "sent")
|
|
67
|
+
await nudges.touch(n.subject, "we_replied");
|
|
68
|
+
else if (action === "snooze") {
|
|
69
|
+
const cfg = nudges.rules;
|
|
70
|
+
await nudges.snooze(n.id, outOfQuiet(addDuration(now, "1d", cfg.time_zone), cfg.quiet_hours, cfg.time_zone));
|
|
71
|
+
}
|
|
72
|
+
else
|
|
73
|
+
await nudges.dismiss(n.id, typeof body.reason === "string" && body.reason.trim() ? body.reason.trim().slice(0, 200) : undefined);
|
|
74
|
+
return { status: 200, body: { ok: true, action } };
|
|
75
|
+
}
|
|
76
|
+
catch (e) {
|
|
77
|
+
if (e instanceof NudgesError) {
|
|
78
|
+
const status = e.code === "unknown_nudge" ? 404 : e.code === "store_failed" ? 502 : e.code === "bad_config" ? 500 : e.code === "not_allowed" ? 403 : 400;
|
|
79
|
+
return { status, body: { ok: false, error: e.code, message: e.message } };
|
|
80
|
+
}
|
|
81
|
+
throw e;
|
|
82
|
+
}
|
|
83
|
+
}
|
package/dist/words.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const NUDGE_WORD_KEYS: readonly ["list.title", "list.empty", "step", "reason.no_reply", "reason.date_unavailable", "reason.not_on_plan", "reason.draft_failed", "reason.no_ai", "no_draft", "sent", "snooze", "dismiss", "unit.minute", "unit.minutes", "unit.hour", "unit.hours", "unit.day", "unit.days", "email.digest.subject", "email.digest.subject_one", "email.due.subject", "email.due.subject_one", "email.body", "email.body_one", "email.open", "email.footer"];
|
|
2
|
+
export type NudgeWordKey = (typeof NUDGE_WORD_KEYS)[number];
|
|
3
|
+
/** The words by language, as `ui.wbnudge.<key>` would hold them. */
|
|
4
|
+
export declare const NUDGE_WORDS: Readonly<Record<"en" | "tl" | "taglish", Readonly<Record<NudgeWordKey, string>>>>;
|
package/dist/words.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// The Waiting for you list's and the emails' words, keyed as the project's
|
|
2
|
+
// strings catalogue keys them (`ui.wbnudge.*`), in English, Tagalog and
|
|
3
|
+
// Taglish. A project copies them into its catalogue (or keeps its own) and
|
|
4
|
+
// passes its `t`; a word the catalogue lacks falls back to the English here.
|
|
5
|
+
// The Tagalog and Taglish are starter words: have a fluent reader check them
|
|
6
|
+
// before a client sees them. No email ever names a customer or quotes a
|
|
7
|
+
// message: a count and a link only.
|
|
8
|
+
export const NUDGE_WORD_KEYS = [
|
|
9
|
+
"list.title",
|
|
10
|
+
"list.empty",
|
|
11
|
+
"step",
|
|
12
|
+
"reason.no_reply",
|
|
13
|
+
"reason.date_unavailable",
|
|
14
|
+
"reason.not_on_plan",
|
|
15
|
+
"reason.draft_failed",
|
|
16
|
+
"reason.no_ai",
|
|
17
|
+
"no_draft",
|
|
18
|
+
"sent",
|
|
19
|
+
"snooze",
|
|
20
|
+
"dismiss",
|
|
21
|
+
"unit.minute",
|
|
22
|
+
"unit.minutes",
|
|
23
|
+
"unit.hour",
|
|
24
|
+
"unit.hours",
|
|
25
|
+
"unit.day",
|
|
26
|
+
"unit.days",
|
|
27
|
+
"email.digest.subject",
|
|
28
|
+
"email.digest.subject_one",
|
|
29
|
+
"email.due.subject",
|
|
30
|
+
"email.due.subject_one",
|
|
31
|
+
"email.body",
|
|
32
|
+
"email.body_one",
|
|
33
|
+
"email.open",
|
|
34
|
+
"email.footer",
|
|
35
|
+
];
|
|
36
|
+
const en = {
|
|
37
|
+
"list.title": "Waiting for you",
|
|
38
|
+
"list.empty": "Nothing to follow up right now.",
|
|
39
|
+
step: "Follow-up {step} of {steps}",
|
|
40
|
+
"reason.no_reply": "No reply in {after}",
|
|
41
|
+
"reason.date_unavailable": "The date is now taken: the draft offers other dates.",
|
|
42
|
+
"reason.not_on_plan": "Drafted follow-ups aren't on your plan: write this one yourself.",
|
|
43
|
+
"reason.draft_failed": "The draft didn't come through: write it yourself, or remind me tomorrow for a new one.",
|
|
44
|
+
"reason.no_ai": "Write this follow-up yourself.",
|
|
45
|
+
no_draft: "No draft for this one.",
|
|
46
|
+
sent: "I sent it",
|
|
47
|
+
snooze: "Remind me tomorrow",
|
|
48
|
+
dismiss: "Not needed",
|
|
49
|
+
"unit.minute": "minute",
|
|
50
|
+
"unit.minutes": "minutes",
|
|
51
|
+
"unit.hour": "hour",
|
|
52
|
+
"unit.hours": "hours",
|
|
53
|
+
"unit.day": "day",
|
|
54
|
+
"unit.days": "days",
|
|
55
|
+
"email.digest.subject": "{count} follow-ups ready",
|
|
56
|
+
"email.digest.subject_one": "1 follow-up ready",
|
|
57
|
+
"email.due.subject": "{count} follow-ups ready",
|
|
58
|
+
"email.due.subject_one": "A follow-up is ready",
|
|
59
|
+
"email.body": "You have {count} follow-ups ready to send, each already drafted.",
|
|
60
|
+
"email.body_one": "You have a follow-up ready to send, already drafted.",
|
|
61
|
+
"email.open": "Open Waiting for you",
|
|
62
|
+
"email.footer": "This is a reminder for you. Nothing was sent to your customers.",
|
|
63
|
+
};
|
|
64
|
+
const tl = {
|
|
65
|
+
"list.title": "Naghihintay sa iyo",
|
|
66
|
+
"list.empty": "Wala pang kailangang i-follow up ngayon.",
|
|
67
|
+
step: "Follow-up {step} sa {steps}",
|
|
68
|
+
"reason.no_reply": "Walang sagot sa loob ng {after}",
|
|
69
|
+
"reason.date_unavailable": "May nakakuha na ng petsa: ibang petsa ang alok ng draft.",
|
|
70
|
+
"reason.not_on_plan": "Wala sa plano ninyo ang mga handang follow-up: kayo na po ang sumulat nito.",
|
|
71
|
+
"reason.draft_failed": "Hindi dumating ang draft: kayo na po ang sumulat, o ipaalala bukas para sa bago.",
|
|
72
|
+
"reason.no_ai": "Kayo na po ang sumulat ng follow-up na ito.",
|
|
73
|
+
no_draft: "Walang draft para dito.",
|
|
74
|
+
sent: "Naipadala ko na",
|
|
75
|
+
snooze: "Ipaalala bukas",
|
|
76
|
+
dismiss: "Hindi na kailangan",
|
|
77
|
+
"unit.minute": "minuto",
|
|
78
|
+
"unit.minutes": "minuto",
|
|
79
|
+
"unit.hour": "oras",
|
|
80
|
+
"unit.hours": "oras",
|
|
81
|
+
"unit.day": "araw",
|
|
82
|
+
"unit.days": "araw",
|
|
83
|
+
"email.digest.subject": "{count} follow-up ang handa na",
|
|
84
|
+
"email.digest.subject_one": "1 follow-up ang handa na",
|
|
85
|
+
"email.due.subject": "{count} follow-up ang handa na",
|
|
86
|
+
"email.due.subject_one": "May handang follow-up",
|
|
87
|
+
"email.body": "May {count} follow-up kayong handa nang ipadala, at may draft na ang bawat isa.",
|
|
88
|
+
"email.body_one": "May isang follow-up kayong handa nang ipadala, at may draft na.",
|
|
89
|
+
"email.open": "Buksan ang Naghihintay sa iyo",
|
|
90
|
+
"email.footer": "Paalala lang ito para sa inyo. Walang naipadala sa inyong mga customer.",
|
|
91
|
+
};
|
|
92
|
+
const taglish = {
|
|
93
|
+
"list.title": "Waiting for you",
|
|
94
|
+
"list.empty": "Wala pang kailangang i-follow up ngayon.",
|
|
95
|
+
step: "Follow-up {step} of {steps}",
|
|
96
|
+
"reason.no_reply": "Walang reply in {after}",
|
|
97
|
+
"reason.date_unavailable": "Taken na ang date: other dates ang offer ng draft.",
|
|
98
|
+
"reason.not_on_plan": "Wala sa plan n'yo ang drafted follow-ups: kayo na po ang mag-write nito.",
|
|
99
|
+
"reason.draft_failed": "Hindi dumating ang draft: kayo na po ang mag-write, o i-remind bukas para sa bago.",
|
|
100
|
+
"reason.no_ai": "Kayo na po ang mag-write ng follow-up na ito.",
|
|
101
|
+
no_draft: "Walang draft para dito.",
|
|
102
|
+
sent: "Na-send ko na",
|
|
103
|
+
snooze: "I-remind bukas",
|
|
104
|
+
dismiss: "Hindi na kailangan",
|
|
105
|
+
"unit.minute": "minute",
|
|
106
|
+
"unit.minutes": "minutes",
|
|
107
|
+
"unit.hour": "hour",
|
|
108
|
+
"unit.hours": "hours",
|
|
109
|
+
"unit.day": "day",
|
|
110
|
+
"unit.days": "days",
|
|
111
|
+
"email.digest.subject": "{count} follow-ups ready na",
|
|
112
|
+
"email.digest.subject_one": "1 follow-up ready na",
|
|
113
|
+
"email.due.subject": "{count} follow-ups ready na",
|
|
114
|
+
"email.due.subject_one": "May follow-up na ready",
|
|
115
|
+
"email.body": "May {count} follow-ups kayong ready to send, may draft na ang bawat isa.",
|
|
116
|
+
"email.body_one": "May isang follow-up kayong ready to send, may draft na.",
|
|
117
|
+
"email.open": "Buksan ang Waiting for you",
|
|
118
|
+
"email.footer": "Reminder lang ito para sa inyo. Walang na-send sa mga customer n'yo.",
|
|
119
|
+
};
|
|
120
|
+
/** The words by language, as `ui.wbnudge.<key>` would hold them. */
|
|
121
|
+
export const NUDGE_WORDS = { en, tl, taglish };
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mccaskie-labs/workbench-nudges",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Follow-up nudges for Workbench apps: remind the business to follow up when a conversation goes quiet (\"no reply in 3 days\"), with the follow-up already drafted by the reply drafter when it comes due. One scan from the app's cron, quiet hours, at most three steps, and a daily email digest to the owner. Never messages the business's customer.",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/mccaskie-labs/workbench.git",
|
|
10
|
+
"directory": "packages/nudges"
|
|
11
|
+
},
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=24"
|
|
14
|
+
},
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./styles": {
|
|
22
|
+
"types": "./dist/styles.d.ts",
|
|
23
|
+
"default": "./dist/styles.js"
|
|
24
|
+
},
|
|
25
|
+
"./package.json": "./package.json"
|
|
26
|
+
},
|
|
27
|
+
"bin": {
|
|
28
|
+
"workbench-nudges": "./dist/cli.js"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"README.md",
|
|
33
|
+
"CHANGELOG.md"
|
|
34
|
+
],
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"typecheck": "tsc -p tsconfig.check.json && tsc -p test/tsconfig.json",
|
|
40
|
+
"test": "vitest run",
|
|
41
|
+
"test:store": "vitest run --config vitest.db.config.ts",
|
|
42
|
+
"build": "node scripts/build.mjs",
|
|
43
|
+
"prepack": "node scripts/build.mjs"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/node": "24.19.2",
|
|
47
|
+
"postgres": "4.1.0",
|
|
48
|
+
"vitest": "^5.1.0"
|
|
49
|
+
}
|
|
50
|
+
}
|