@agent-native/core 0.139.0 → 0.141.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/corpus/README.md +1 -1
- package/corpus/templates/analytics/actions/query-agent-native-analytics.ts +1 -1
- package/corpus/templates/analytics/changelog/2026-08-05-historical-analytics-rollups-stay-complete-and-atomic.md +6 -0
- package/corpus/templates/analytics/server/lib/dashboard-report.ts +0 -2
- package/corpus/templates/analytics/server/lib/first-party-analytics-rollups.ts +11 -6
- package/corpus/templates/analytics/server/lib/first-party-analytics.ts +8 -18
- package/corpus/templates/analytics/server/plugins/db.ts +174 -0
- package/corpus/templates/calendar/app/components/calendar/OutOfOfficeEvent.tsx +38 -38
- package/corpus/templates/calendar/changelog/2026-08-05-out-of-office-blocks-now-open-from-their-scheduled-time-rang.md +6 -0
- package/corpus/templates/calendar/server/lib/booking-emails.ts +70 -154
- package/corpus/templates/calendar/server/lib/event-guest-notifications.ts +28 -64
- package/corpus/templates/clips/actions/get-recording-player-data.ts +1 -0
- package/corpus/templates/clips/actions/invite-member.ts +16 -39
- package/corpus/templates/clips/app/components/player/comments-panel.tsx +2 -3
- package/corpus/templates/clips/app/components/player/reaction-emojis.ts +10 -0
- package/corpus/templates/clips/app/components/player/reactions-tray.tsx +2 -2
- package/corpus/templates/clips/app/components/player/video-player.tsx +20 -6
- package/corpus/templates/clips/app/routes/embed.$shareId.tsx +33 -0
- package/corpus/templates/clips/app/routes/r.$recordingId.tsx +5 -1
- package/corpus/templates/clips/app/routes/share.$shareId.tsx +27 -1
- package/corpus/templates/clips/changelog/2026-08-05-comment-and-video-reaction-pickers-now-include-eyes-and-mind.md +6 -0
- package/corpus/templates/clips/changelog/2026-08-05-fresh-recordings-now-open-with-the-complete-video.md +6 -0
- package/corpus/templates/clips/server/lib/transactional-email-templates.ts +0 -24
- package/corpus/templates/clips/server/routes/api/public-recording.get.ts +1 -0
- package/corpus/templates/content/server/lib/comment-notifications.ts +26 -57
- package/corpus/templates/design/app/components/design/edit-panel/stroke-properties.tsx +16 -3
- package/corpus/templates/forms/server/lib/response-email.ts +0 -2
- package/corpus/templates/plan/actions/request-plan-access.ts +12 -33
- package/corpus/templates/plan/server/lib/comment-notifications.ts +27 -53
- package/corpus/templates/slides/server/lib/comment-notifications.ts +21 -44
- package/dist/a2a/handlers.js +9 -10
- package/dist/collab/routes.d.ts +1 -1
- package/dist/db/client.js +8 -8
- package/dist/db/migrations.js +39 -19
- package/dist/file-upload/actions/upload-image.d.ts +1 -1
- package/dist/localization/default-messages.d.ts +0 -45
- package/dist/localization/default-messages.js +0 -45
- package/dist/notifications/routes.d.ts +5 -5
- package/dist/observability/routes.d.ts +3 -3
- package/dist/org/handlers.js +1 -8
- package/dist/provider-api/actions/custom-provider-registration.d.ts +8 -8
- package/dist/provider-api/actions/provider-api.d.ts +12 -12
- package/dist/secrets/routes.d.ts +9 -9
- package/dist/server/action-discovery.js +0 -22
- package/dist/server/agent-engine-api-key-route.d.ts +1 -1
- package/dist/server/auth.js +3 -1
- package/dist/server/better-auth-instance.js +2 -17
- package/dist/server/email.d.ts +0 -9
- package/dist/server/email.js +3 -44
- package/dist/server/index.d.ts +0 -1
- package/dist/server/index.js +0 -1
- package/dist/server/transcribe-voice.d.ts +1 -1
- package/dist/templates/workspace-core/.agents/skills/authentication/SKILL.md +3 -2
- package/docs/content/authentication.mdx +25 -19
- package/package.json +2 -1
- package/src/templates/workspace-core/.agents/skills/authentication/SKILL.md +3 -2
- package/corpus/templates/analytics/server/lib/emails.ts +0 -51
- package/corpus/templates/analytics/server/plugins/transactional-emails.ts +0 -5
- package/corpus/templates/calendar/server/lib/emails.ts +0 -167
- package/corpus/templates/calendar/server/plugins/transactional-emails.ts +0 -5
- package/corpus/templates/clips/server/lib/emails.ts +0 -256
- package/corpus/templates/clips/server/plugins/transactional-email-catalog.ts +0 -5
- package/corpus/templates/content/server/lib/emails.ts +0 -70
- package/corpus/templates/content/server/plugins/transactional-emails.ts +0 -5
- package/corpus/templates/dispatch/app/routes/transactional-email.$appId.$id.tsx +0 -4
- package/corpus/templates/dispatch/app/routes/transactional-email.tsx +0 -4
- package/corpus/templates/forms/server/lib/emails.ts +0 -52
- package/corpus/templates/forms/server/plugins/transactional-emails.ts +0 -5
- package/corpus/templates/plan/server/lib/emails.ts +0 -67
- package/corpus/templates/plan/server/plugins/transactional-emails.ts +0 -5
- package/corpus/templates/slides/server/lib/emails.ts +0 -40
- package/corpus/templates/slides/server/plugins/transactional-emails.ts +0 -5
- package/dist/email-catalog/actions/list-email-activity.d.ts +0 -6
- package/dist/email-catalog/actions/list-email-activity.js +0 -15
- package/dist/email-catalog/actions/list-email-engagement.d.ts +0 -6
- package/dist/email-catalog/actions/list-email-engagement.js +0 -15
- package/dist/email-catalog/actions/list-email-log.d.ts +0 -8
- package/dist/email-catalog/actions/list-email-log.js +0 -22
- package/dist/email-catalog/actions/list-transactional-emails.d.ts +0 -23
- package/dist/email-catalog/actions/list-transactional-emails.js +0 -61
- package/dist/email-catalog/actions/render-transactional-email-preview.d.ts +0 -10
- package/dist/email-catalog/actions/render-transactional-email-preview.js +0 -24
- package/dist/email-catalog/authorize.d.ts +0 -2
- package/dist/email-catalog/authorize.js +0 -10
- package/dist/email-catalog/log.d.ts +0 -57
- package/dist/email-catalog/log.js +0 -117
- package/dist/email-catalog/provider-metrics.d.ts +0 -34
- package/dist/email-catalog/provider-metrics.js +0 -165
- package/dist/email-catalog/registry.d.ts +0 -79
- package/dist/email-catalog/registry.js +0 -71
- package/dist/email-catalog/schema.d.ts +0 -212
- package/dist/email-catalog/schema.js +0 -49
- package/dist/email-catalog/system-emails.d.ts +0 -14
- package/dist/email-catalog/system-emails.js +0 -68
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { defineAction } from "../../action.js";
|
|
3
|
-
import { getAppSlug } from "../../server/app-name.js";
|
|
4
|
-
import { getEmailSendStats } from "../log.js";
|
|
5
|
-
import { listTransactionalEmails } from "../registry.js";
|
|
6
|
-
import { registerCoreSystemEmails } from "../system-emails.js";
|
|
7
|
-
const DEFAULT_WINDOW_DAYS = 30;
|
|
8
|
-
export default defineAction({
|
|
9
|
-
description: "List the transactional emails this app can send, with the trigger, recipient and sender logic for each, plus local send counts and last-sent. Engagement metrics such as open rate are not included here — they live at the email provider.",
|
|
10
|
-
schema: z.object({
|
|
11
|
-
windowDays: z.coerce
|
|
12
|
-
.number()
|
|
13
|
-
.int()
|
|
14
|
-
.min(1)
|
|
15
|
-
.max(365)
|
|
16
|
-
.default(DEFAULT_WINDOW_DAYS)
|
|
17
|
-
.describe("How many days of send history to summarize."),
|
|
18
|
-
}),
|
|
19
|
-
http: { method: "GET" },
|
|
20
|
-
run: async ({ windowDays }) => {
|
|
21
|
-
registerCoreSystemEmails();
|
|
22
|
-
const since = Date.now() - windowDays * 24 * 60 * 60 * 1000;
|
|
23
|
-
const app = getAppSlug() ?? "unknown";
|
|
24
|
-
const definitions = listTransactionalEmails();
|
|
25
|
-
// A failed stats read must not masquerade as "no email ever sent" — the
|
|
26
|
-
// catalog is still worth returning, but the caller has to be able to tell
|
|
27
|
-
// that the numbers are missing rather than zero.
|
|
28
|
-
let statsById = null;
|
|
29
|
-
let statsError = null;
|
|
30
|
-
try {
|
|
31
|
-
const stats = await getEmailSendStats(since, app);
|
|
32
|
-
statsById = new Map(stats.map((row) => [row.templateId, row]));
|
|
33
|
-
}
|
|
34
|
-
catch (error) {
|
|
35
|
-
statsError = error instanceof Error ? error.message : String(error);
|
|
36
|
-
}
|
|
37
|
-
return {
|
|
38
|
-
app,
|
|
39
|
-
windowDays,
|
|
40
|
-
statsAvailable: statsError === null,
|
|
41
|
-
statsError,
|
|
42
|
-
emails: definitions.map((definition) => {
|
|
43
|
-
const stats = statsById?.get(definition.id);
|
|
44
|
-
return {
|
|
45
|
-
id: definition.id,
|
|
46
|
-
app: definition.app,
|
|
47
|
-
name: definition.name,
|
|
48
|
-
trigger: definition.trigger,
|
|
49
|
-
recipient: definition.recipient,
|
|
50
|
-
recipientLabel: definition.recipientLabel,
|
|
51
|
-
sender: definition.sender,
|
|
52
|
-
senderLabel: definition.senderLabel,
|
|
53
|
-
sent: stats?.sent ?? (statsById ? 0 : null),
|
|
54
|
-
failed: stats?.failed ?? (statsById ? 0 : null),
|
|
55
|
-
lastSentAt: stats?.lastSentAt ?? null,
|
|
56
|
-
};
|
|
57
|
-
}),
|
|
58
|
-
};
|
|
59
|
-
},
|
|
60
|
-
});
|
|
61
|
-
//# sourceMappingURL=list-transactional-emails.js.map
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { defineAction } from "../../action.js";
|
|
3
|
-
import { renderTransactionalEmailPreview } from "../registry.js";
|
|
4
|
-
import { registerCoreSystemEmails } from "../system-emails.js";
|
|
5
|
-
export default defineAction({
|
|
6
|
-
description: "Render one of this app's transactional emails with representative dummy data, returning the subject, HTML and plain-text bodies for preview.",
|
|
7
|
-
schema: z.object({
|
|
8
|
-
id: z
|
|
9
|
-
.string()
|
|
10
|
-
.describe("Registered email id, e.g. calendar.booking-confirmed."),
|
|
11
|
-
}),
|
|
12
|
-
http: { method: "GET" },
|
|
13
|
-
run: async ({ id }) => {
|
|
14
|
-
registerCoreSystemEmails();
|
|
15
|
-
const rendered = renderTransactionalEmailPreview(id);
|
|
16
|
-
return {
|
|
17
|
-
id,
|
|
18
|
-
subject: rendered.subject,
|
|
19
|
-
html: rendered.html,
|
|
20
|
-
text: rendered.text,
|
|
21
|
-
};
|
|
22
|
-
},
|
|
23
|
-
});
|
|
24
|
-
//# sourceMappingURL=render-transactional-email-preview.js.map
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { currentRequestUserIsOrgAdmin } from "../server/org-admin.js";
|
|
2
|
-
import { getTransactionalEmail } from "./registry.js";
|
|
3
|
-
import { registerCoreSystemEmails } from "./system-emails.js";
|
|
4
|
-
export async function authorizeTransactionalEmailRead(templateIds = []) {
|
|
5
|
-
if (!(await currentRequestUserIsOrgAdmin()))
|
|
6
|
-
return false;
|
|
7
|
-
registerCoreSystemEmails();
|
|
8
|
-
return templateIds.every((id) => Boolean(getTransactionalEmail(id)));
|
|
9
|
-
}
|
|
10
|
-
//# sourceMappingURL=authorize.js.map
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Durable send log for transactional emails.
|
|
3
|
-
*
|
|
4
|
-
* Written by `sendEmail` on every attempt, successful or not. Read by Dispatch
|
|
5
|
-
* to report per-email send counts and last-sent without depending on the
|
|
6
|
-
* provider's activity retention window.
|
|
7
|
-
*/
|
|
8
|
-
export interface RecordEmailSendArgs {
|
|
9
|
-
templateId?: string;
|
|
10
|
-
app?: string;
|
|
11
|
-
recipient: string;
|
|
12
|
-
sender: string;
|
|
13
|
-
subject: string;
|
|
14
|
-
status: "sent" | "failed";
|
|
15
|
-
error?: string;
|
|
16
|
-
provider: string;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Append one send record.
|
|
20
|
-
*
|
|
21
|
-
* Callers treat logging as best-effort: a logging failure must not turn a
|
|
22
|
-
* delivered email into a thrown send. The failure is surfaced on the console
|
|
23
|
-
* rather than swallowed, so a persistently broken log is visible instead of
|
|
24
|
-
* quietly producing an empty activity view.
|
|
25
|
-
*/
|
|
26
|
-
export declare function recordEmailSend(args: RecordEmailSendArgs): Promise<void>;
|
|
27
|
-
export interface EmailSendStats {
|
|
28
|
-
templateId: string;
|
|
29
|
-
sent: number;
|
|
30
|
-
failed: number;
|
|
31
|
-
lastSentAt: number | null;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Per-template send counts and last-sent, for sends at or after `since`.
|
|
35
|
-
* Templates with no rows are absent from the result — callers distinguish
|
|
36
|
-
* "never sent" from "sent zero times in window" by that absence.
|
|
37
|
-
*/
|
|
38
|
-
export declare function getEmailSendStats(since: number, app: string): Promise<EmailSendStats[]>;
|
|
39
|
-
export interface EmailLogEntry {
|
|
40
|
-
id: string;
|
|
41
|
-
templateId: string | null;
|
|
42
|
-
app: string | null;
|
|
43
|
-
recipient: string;
|
|
44
|
-
sender: string;
|
|
45
|
-
subject: string;
|
|
46
|
-
status: string;
|
|
47
|
-
error: string | null;
|
|
48
|
-
provider: string;
|
|
49
|
-
createdAt: number;
|
|
50
|
-
}
|
|
51
|
-
/** Most recent sends for one app, newest first, optionally filtered to one template. */
|
|
52
|
-
export declare function listEmailLog(options: {
|
|
53
|
-
app: string;
|
|
54
|
-
templateId?: string;
|
|
55
|
-
limit?: number;
|
|
56
|
-
}): Promise<EmailLogEntry[]>;
|
|
57
|
-
//# sourceMappingURL=log.d.ts.map
|
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Durable send log for transactional emails.
|
|
3
|
-
*
|
|
4
|
-
* Written by `sendEmail` on every attempt, successful or not. Read by Dispatch
|
|
5
|
-
* to report per-email send counts and last-sent without depending on the
|
|
6
|
-
* provider's activity retention window.
|
|
7
|
-
*/
|
|
8
|
-
import { randomUUID } from "node:crypto";
|
|
9
|
-
import { getDbExec, isPostgres } from "../db/client.js";
|
|
10
|
-
import { ensureIndexExists, ensureTableExists } from "../db/ddl-guard.js";
|
|
11
|
-
let _initPromise;
|
|
12
|
-
async function ensureTable() {
|
|
13
|
-
if (!_initPromise) {
|
|
14
|
-
_initPromise = (async () => {
|
|
15
|
-
const { EMAIL_LOG_CREATE_SQL, EMAIL_LOG_TEMPLATE_INDEX_SQL } = await import("./schema.js");
|
|
16
|
-
// Generic INTEGER maps to BIGINT on Postgres, which millisecond
|
|
17
|
-
// timestamps need.
|
|
18
|
-
const createSql = isPostgres()
|
|
19
|
-
? EMAIL_LOG_CREATE_SQL.replace(/\bINTEGER\b/g, "BIGINT")
|
|
20
|
-
: EMAIL_LOG_CREATE_SQL;
|
|
21
|
-
await ensureTableExists("email_log", createSql);
|
|
22
|
-
await ensureIndexExists("email_log_template_created_idx", EMAIL_LOG_TEMPLATE_INDEX_SQL);
|
|
23
|
-
})().catch((error) => {
|
|
24
|
-
// Don't memoize a failed bootstrap — the next send should retry rather
|
|
25
|
-
// than log nothing forever.
|
|
26
|
-
_initPromise = undefined;
|
|
27
|
-
throw error;
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
return _initPromise;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Append one send record.
|
|
34
|
-
*
|
|
35
|
-
* Callers treat logging as best-effort: a logging failure must not turn a
|
|
36
|
-
* delivered email into a thrown send. The failure is surfaced on the console
|
|
37
|
-
* rather than swallowed, so a persistently broken log is visible instead of
|
|
38
|
-
* quietly producing an empty activity view.
|
|
39
|
-
*/
|
|
40
|
-
export async function recordEmailSend(args) {
|
|
41
|
-
try {
|
|
42
|
-
await ensureTable();
|
|
43
|
-
await getDbExec().execute({
|
|
44
|
-
sql: `INSERT INTO email_log
|
|
45
|
-
(id, template_id, app, recipient, sender, subject, status, error, provider, created_at)
|
|
46
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
47
|
-
args: [
|
|
48
|
-
randomUUID(),
|
|
49
|
-
args.templateId ?? null,
|
|
50
|
-
args.app ?? null,
|
|
51
|
-
args.recipient,
|
|
52
|
-
args.sender,
|
|
53
|
-
args.subject,
|
|
54
|
-
args.status,
|
|
55
|
-
args.error ?? null,
|
|
56
|
-
args.provider,
|
|
57
|
-
Date.now(),
|
|
58
|
-
],
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
catch (error) {
|
|
62
|
-
console.error("[agent-native:email] failed to record send", error);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Per-template send counts and last-sent, for sends at or after `since`.
|
|
67
|
-
* Templates with no rows are absent from the result — callers distinguish
|
|
68
|
-
* "never sent" from "sent zero times in window" by that absence.
|
|
69
|
-
*/
|
|
70
|
-
export async function getEmailSendStats(since, app) {
|
|
71
|
-
await ensureTable();
|
|
72
|
-
const { rows } = await getDbExec().execute({
|
|
73
|
-
sql: `SELECT template_id,
|
|
74
|
-
SUM(CASE WHEN status = 'sent' THEN 1 ELSE 0 END) AS sent,
|
|
75
|
-
SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END) AS failed,
|
|
76
|
-
MAX(CASE WHEN status = 'sent' THEN created_at END) AS last_sent_at
|
|
77
|
-
FROM email_log
|
|
78
|
-
WHERE app = ? AND template_id IS NOT NULL AND created_at >= ?
|
|
79
|
-
GROUP BY template_id`,
|
|
80
|
-
args: [app, since],
|
|
81
|
-
});
|
|
82
|
-
return rows.map((row) => ({
|
|
83
|
-
templateId: String(row.template_id),
|
|
84
|
-
sent: Number(row.sent ?? 0),
|
|
85
|
-
failed: Number(row.failed ?? 0),
|
|
86
|
-
lastSentAt: row.last_sent_at == null ? null : Number(row.last_sent_at),
|
|
87
|
-
}));
|
|
88
|
-
}
|
|
89
|
-
/** Most recent sends for one app, newest first, optionally filtered to one template. */
|
|
90
|
-
export async function listEmailLog(options) {
|
|
91
|
-
await ensureTable();
|
|
92
|
-
const limit = Math.min(Math.max(options.limit ?? 100, 1), 500);
|
|
93
|
-
const where = options.templateId
|
|
94
|
-
? `WHERE app = ? AND template_id = ?`
|
|
95
|
-
: `WHERE app = ?`;
|
|
96
|
-
const args = options.templateId
|
|
97
|
-
? [options.app, options.templateId, limit]
|
|
98
|
-
: [options.app, limit];
|
|
99
|
-
const { rows } = await getDbExec().execute({
|
|
100
|
-
sql: `SELECT id, template_id, app, recipient, sender, subject, status, error, provider, created_at
|
|
101
|
-
FROM email_log ${where} ORDER BY created_at DESC LIMIT ?`,
|
|
102
|
-
args,
|
|
103
|
-
});
|
|
104
|
-
return rows.map((row) => ({
|
|
105
|
-
id: String(row.id),
|
|
106
|
-
templateId: row.template_id == null ? null : String(row.template_id),
|
|
107
|
-
app: row.app == null ? null : String(row.app),
|
|
108
|
-
recipient: String(row.recipient),
|
|
109
|
-
sender: String(row.sender),
|
|
110
|
-
subject: String(row.subject),
|
|
111
|
-
status: String(row.status),
|
|
112
|
-
error: row.error == null ? null : String(row.error),
|
|
113
|
-
provider: String(row.provider),
|
|
114
|
-
createdAt: Number(row.created_at),
|
|
115
|
-
}));
|
|
116
|
-
}
|
|
117
|
-
//# sourceMappingURL=log.js.map
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Engagement metrics and activity for registered transactional emails, read
|
|
3
|
-
* from the active provider in the current app's request context.
|
|
4
|
-
*/
|
|
5
|
-
export type ProviderMetricsResult<T> = {
|
|
6
|
-
available: true;
|
|
7
|
-
data: T;
|
|
8
|
-
} | {
|
|
9
|
-
available: false;
|
|
10
|
-
reason: string;
|
|
11
|
-
};
|
|
12
|
-
export interface EmailEngagement {
|
|
13
|
-
templateId: string;
|
|
14
|
-
delivered: number;
|
|
15
|
-
uniqueOpens: number;
|
|
16
|
-
uniqueClicks: number;
|
|
17
|
-
openRate: number | null;
|
|
18
|
-
}
|
|
19
|
-
export declare function fetchEmailEngagement(templateIds: string[], windowDays: number): Promise<ProviderMetricsResult<EmailEngagement[]>>;
|
|
20
|
-
export interface EmailActivityEntry {
|
|
21
|
-
msgId: string;
|
|
22
|
-
toEmail: string;
|
|
23
|
-
fromEmail: string;
|
|
24
|
-
subject: string;
|
|
25
|
-
status: string;
|
|
26
|
-
opensCount: number;
|
|
27
|
-
clicksCount: number;
|
|
28
|
-
lastEventTime: string;
|
|
29
|
-
}
|
|
30
|
-
export declare function fetchEmailActivity(options: {
|
|
31
|
-
templateId: string;
|
|
32
|
-
limit?: number;
|
|
33
|
-
}): Promise<ProviderMetricsResult<EmailActivityEntry[]>>;
|
|
34
|
-
//# sourceMappingURL=provider-metrics.d.ts.map
|
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Engagement metrics and activity for registered transactional emails, read
|
|
3
|
-
* from the active provider in the current app's request context.
|
|
4
|
-
*/
|
|
5
|
-
import { z } from "zod";
|
|
6
|
-
import { resolveSecret } from "../server/credential-provider.js";
|
|
7
|
-
import { getEmailProvider } from "../server/email.js";
|
|
8
|
-
const SENDGRID_API = "https://api.sendgrid.com/v3";
|
|
9
|
-
const SENDGRID_PAGE_SIZE = 1000;
|
|
10
|
-
const SENDGRID_MAX_PAGES = 10;
|
|
11
|
-
const categoryMetricsSchema = z
|
|
12
|
-
.object({
|
|
13
|
-
delivered: z.number().default(0),
|
|
14
|
-
unique_opens: z.number().default(0),
|
|
15
|
-
unique_clicks: z.number().default(0),
|
|
16
|
-
})
|
|
17
|
-
.passthrough();
|
|
18
|
-
const categoryStatSchema = z
|
|
19
|
-
.object({
|
|
20
|
-
name: z.string(),
|
|
21
|
-
metrics: categoryMetricsSchema.default(() => ({
|
|
22
|
-
delivered: 0,
|
|
23
|
-
unique_opens: 0,
|
|
24
|
-
unique_clicks: 0,
|
|
25
|
-
})),
|
|
26
|
-
})
|
|
27
|
-
.passthrough();
|
|
28
|
-
const categorySumsSchema = z.object({
|
|
29
|
-
stats: z.array(categoryStatSchema).default([]),
|
|
30
|
-
});
|
|
31
|
-
const activitySchema = z.object({
|
|
32
|
-
messages: z
|
|
33
|
-
.array(z
|
|
34
|
-
.object({
|
|
35
|
-
msg_id: z.string().default(""),
|
|
36
|
-
to_email: z.string().default(""),
|
|
37
|
-
from_email: z.string().default(""),
|
|
38
|
-
subject: z.string().default(""),
|
|
39
|
-
status: z.string().default(""),
|
|
40
|
-
opens_count: z.coerce.number().default(0),
|
|
41
|
-
clicks_count: z.coerce.number().default(0),
|
|
42
|
-
last_event_time: z.string().default(""),
|
|
43
|
-
})
|
|
44
|
-
.passthrough())
|
|
45
|
-
.default([]),
|
|
46
|
-
});
|
|
47
|
-
async function activeSendGrid() {
|
|
48
|
-
const provider = await getEmailProvider();
|
|
49
|
-
if (provider === "resend") {
|
|
50
|
-
return {
|
|
51
|
-
available: false,
|
|
52
|
-
reason: "Email delivery uses Resend, so SendGrid metrics do not describe the active transport.",
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
if (provider !== "sendgrid") {
|
|
56
|
-
return {
|
|
57
|
-
available: false,
|
|
58
|
-
reason: "No email provider is configured, so provider metrics cannot be read.",
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
const key = await resolveSecret("SENDGRID_API_KEY");
|
|
62
|
-
if (!key) {
|
|
63
|
-
return {
|
|
64
|
-
available: false,
|
|
65
|
-
reason: "SendGrid is the active email transport, but SENDGRID_API_KEY could not be resolved.",
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
return { available: true, key };
|
|
69
|
-
}
|
|
70
|
-
function isoDate(ms) {
|
|
71
|
-
return new Date(ms).toISOString().slice(0, 10);
|
|
72
|
-
}
|
|
73
|
-
async function sendgridGet(key, path, params) {
|
|
74
|
-
const url = new URL(`${SENDGRID_API}${path}`);
|
|
75
|
-
for (const [name, value] of params)
|
|
76
|
-
url.searchParams.append(name, value);
|
|
77
|
-
const res = await fetch(url, {
|
|
78
|
-
headers: { Authorization: `Bearer ${key}` },
|
|
79
|
-
});
|
|
80
|
-
if (!res.ok) {
|
|
81
|
-
const body = await res
|
|
82
|
-
.text()
|
|
83
|
-
.catch((cause) => `<error body unreadable: ${cause}>`);
|
|
84
|
-
throw new Error(`SendGrid ${res.status} on ${path}: ${body.slice(0, 300)}`);
|
|
85
|
-
}
|
|
86
|
-
return res.json();
|
|
87
|
-
}
|
|
88
|
-
export async function fetchEmailEngagement(templateIds, windowDays) {
|
|
89
|
-
if (!templateIds.length)
|
|
90
|
-
return { available: true, data: [] };
|
|
91
|
-
const sendgrid = await activeSendGrid();
|
|
92
|
-
if (!sendgrid.available)
|
|
93
|
-
return sendgrid;
|
|
94
|
-
const end = Date.now();
|
|
95
|
-
const start = end - windowDays * 24 * 60 * 60 * 1000;
|
|
96
|
-
const requested = new Set(templateIds);
|
|
97
|
-
const data = [];
|
|
98
|
-
try {
|
|
99
|
-
for (let page = 0; page < SENDGRID_MAX_PAGES; page += 1) {
|
|
100
|
-
const payload = categorySumsSchema.parse(await sendgridGet(sendgrid.key, "/categories/stats/sums", [
|
|
101
|
-
["start_date", isoDate(start)],
|
|
102
|
-
["end_date", isoDate(end)],
|
|
103
|
-
["limit", String(SENDGRID_PAGE_SIZE)],
|
|
104
|
-
["offset", String(page * SENDGRID_PAGE_SIZE)],
|
|
105
|
-
]));
|
|
106
|
-
for (const entry of payload.stats) {
|
|
107
|
-
if (!requested.has(entry.name))
|
|
108
|
-
continue;
|
|
109
|
-
requested.delete(entry.name);
|
|
110
|
-
const delivered = entry.metrics.delivered;
|
|
111
|
-
const uniqueOpens = entry.metrics.unique_opens;
|
|
112
|
-
data.push({
|
|
113
|
-
templateId: entry.name,
|
|
114
|
-
delivered,
|
|
115
|
-
uniqueOpens,
|
|
116
|
-
uniqueClicks: entry.metrics.unique_clicks,
|
|
117
|
-
openRate: delivered > 0 ? Math.min(uniqueOpens / delivered, 1) : null,
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
if (requested.size === 0 || payload.stats.length < SENDGRID_PAGE_SIZE) {
|
|
121
|
-
break;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
return { available: true, data };
|
|
125
|
-
}
|
|
126
|
-
catch (error) {
|
|
127
|
-
return {
|
|
128
|
-
available: false,
|
|
129
|
-
reason: error instanceof Error ? error.message : String(error),
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
export async function fetchEmailActivity(options) {
|
|
134
|
-
const sendgrid = await activeSendGrid();
|
|
135
|
-
if (!sendgrid.available)
|
|
136
|
-
return sendgrid;
|
|
137
|
-
const limit = Math.min(Math.max(options.limit ?? 50, 1), 1000);
|
|
138
|
-
const safe = options.templateId.replace(/["\\]/g, "");
|
|
139
|
-
try {
|
|
140
|
-
const payload = activitySchema.parse(await sendgridGet(sendgrid.key, "/messages", [
|
|
141
|
-
["limit", String(limit)],
|
|
142
|
-
["query", `category="${safe}"`],
|
|
143
|
-
]));
|
|
144
|
-
return {
|
|
145
|
-
available: true,
|
|
146
|
-
data: payload.messages.map((message) => ({
|
|
147
|
-
msgId: message.msg_id,
|
|
148
|
-
toEmail: message.to_email,
|
|
149
|
-
fromEmail: message.from_email,
|
|
150
|
-
subject: message.subject,
|
|
151
|
-
status: message.status,
|
|
152
|
-
opensCount: message.opens_count,
|
|
153
|
-
clicksCount: message.clicks_count,
|
|
154
|
-
lastEventTime: message.last_event_time,
|
|
155
|
-
})),
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
catch (error) {
|
|
159
|
-
return {
|
|
160
|
-
available: false,
|
|
161
|
-
reason: error instanceof Error ? error.message : String(error),
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
//# sourceMappingURL=provider-metrics.js.map
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Registry of the transactional emails an app can send.
|
|
3
|
-
*
|
|
4
|
-
* An app declares each email it sends here so the workspace can answer, without
|
|
5
|
-
* reading the app's source: what emails exist, what makes one send, who it goes
|
|
6
|
-
* to, who it comes from, and what it looks like. Dispatch aggregates these
|
|
7
|
-
* across every mounted app via the `list-transactional-emails` action.
|
|
8
|
-
*
|
|
9
|
-
* Declare emails next to the code that sends them, then import that module from
|
|
10
|
-
* a server plugin so registration happens at startup:
|
|
11
|
-
*
|
|
12
|
-
* defineTransactionalEmail({
|
|
13
|
-
* id: "calendar.booking-confirmed",
|
|
14
|
-
* name: "Booking confirmed",
|
|
15
|
-
* trigger: "A guest completes a booking on a public scheduling page.",
|
|
16
|
-
* recipient: "The guest email captured on the booking form.",
|
|
17
|
-
* sender: "EMAIL_FROM, with reply-to set to the event host.",
|
|
18
|
-
* preview: () => renderBookingConfirmedEmail(SAMPLE_BOOKING),
|
|
19
|
-
* });
|
|
20
|
-
*
|
|
21
|
-
* The `id` doubles as the SendGrid category `sendEmail` tags the message with,
|
|
22
|
-
* which is how per-email delivery and open metrics are attributed later.
|
|
23
|
-
*/
|
|
24
|
-
import type { RenderedEmailMessage } from "../server/email-templates.js";
|
|
25
|
-
export interface TransactionalEmailDefinition {
|
|
26
|
-
/**
|
|
27
|
-
* Stable, globally unique id in `<app>.<email>` form, e.g.
|
|
28
|
-
* `calendar.booking-confirmed`. Used as the SendGrid category, so changing it
|
|
29
|
-
* orphans historical metrics for this email.
|
|
30
|
-
*/
|
|
31
|
-
id: string;
|
|
32
|
-
/** Human-readable name, e.g. "Booking confirmed". */
|
|
33
|
-
name: string;
|
|
34
|
-
/**
|
|
35
|
-
* App slug this email belongs to. Defaults to the running app, which is
|
|
36
|
-
* correct for every app-declared email; core system emails set it explicitly.
|
|
37
|
-
*/
|
|
38
|
-
app?: string;
|
|
39
|
-
/** Plain-language description of the condition that causes a send. */
|
|
40
|
-
trigger: string;
|
|
41
|
-
/** Plain-language description of how the recipient address is chosen. */
|
|
42
|
-
recipient: string;
|
|
43
|
-
/**
|
|
44
|
-
* Two-to-four word summary of the recipient for table cells, e.g.
|
|
45
|
-
* "Booking guest". The full `recipient` sentence is shown on the detail view.
|
|
46
|
-
*/
|
|
47
|
-
recipientLabel: string;
|
|
48
|
-
/** Plain-language description of how From and Reply-To are chosen. */
|
|
49
|
-
sender: string;
|
|
50
|
-
/** Two-to-four word summary of the sender, e.g. "Default, reply-to host". */
|
|
51
|
-
senderLabel: string;
|
|
52
|
-
/**
|
|
53
|
-
* Render the email with representative dummy data. Must not read from the
|
|
54
|
-
* database or touch the network — previews are rendered on demand from
|
|
55
|
-
* Dispatch, for apps whose data the caller may not be able to see.
|
|
56
|
-
*/
|
|
57
|
-
preview: () => RenderedEmailMessage;
|
|
58
|
-
}
|
|
59
|
-
/** A definition with its app resolved, as returned to callers. */
|
|
60
|
-
export type RegisteredTransactionalEmail = TransactionalEmailDefinition & {
|
|
61
|
-
app: string;
|
|
62
|
-
};
|
|
63
|
-
/**
|
|
64
|
-
* Register a transactional email. Returns the definition so the call site can
|
|
65
|
-
* export it and reuse `id` when sending.
|
|
66
|
-
*/
|
|
67
|
-
export declare function defineTransactionalEmail(definition: TransactionalEmailDefinition): RegisteredTransactionalEmail;
|
|
68
|
-
/** Every registered email, sorted by app then name. */
|
|
69
|
-
export declare function listTransactionalEmails(): RegisteredTransactionalEmail[];
|
|
70
|
-
export declare function getTransactionalEmail(id: string): RegisteredTransactionalEmail | undefined;
|
|
71
|
-
/**
|
|
72
|
-
* Render one email's preview. Throws when the id is unknown or the renderer
|
|
73
|
-
* fails — a preview that silently returns an empty body would look like an
|
|
74
|
-
* email that legitimately renders blank.
|
|
75
|
-
*/
|
|
76
|
-
export declare function renderTransactionalEmailPreview(id: string): RenderedEmailMessage;
|
|
77
|
-
/** Test seam — drops all registrations. */
|
|
78
|
-
export declare function resetTransactionalEmailRegistry(): void;
|
|
79
|
-
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Registry of the transactional emails an app can send.
|
|
3
|
-
*
|
|
4
|
-
* An app declares each email it sends here so the workspace can answer, without
|
|
5
|
-
* reading the app's source: what emails exist, what makes one send, who it goes
|
|
6
|
-
* to, who it comes from, and what it looks like. Dispatch aggregates these
|
|
7
|
-
* across every mounted app via the `list-transactional-emails` action.
|
|
8
|
-
*
|
|
9
|
-
* Declare emails next to the code that sends them, then import that module from
|
|
10
|
-
* a server plugin so registration happens at startup:
|
|
11
|
-
*
|
|
12
|
-
* defineTransactionalEmail({
|
|
13
|
-
* id: "calendar.booking-confirmed",
|
|
14
|
-
* name: "Booking confirmed",
|
|
15
|
-
* trigger: "A guest completes a booking on a public scheduling page.",
|
|
16
|
-
* recipient: "The guest email captured on the booking form.",
|
|
17
|
-
* sender: "EMAIL_FROM, with reply-to set to the event host.",
|
|
18
|
-
* preview: () => renderBookingConfirmedEmail(SAMPLE_BOOKING),
|
|
19
|
-
* });
|
|
20
|
-
*
|
|
21
|
-
* The `id` doubles as the SendGrid category `sendEmail` tags the message with,
|
|
22
|
-
* which is how per-email delivery and open metrics are attributed later.
|
|
23
|
-
*/
|
|
24
|
-
import { getAppSlug } from "../server/app-name.js";
|
|
25
|
-
const registry = new Map();
|
|
26
|
-
/** Source definitions, so re-registering the same one is a no-op rather than a clash. */
|
|
27
|
-
const sources = new Map();
|
|
28
|
-
/**
|
|
29
|
-
* Register a transactional email. Returns the definition so the call site can
|
|
30
|
-
* export it and reuse `id` when sending.
|
|
31
|
-
*/
|
|
32
|
-
export function defineTransactionalEmail(definition) {
|
|
33
|
-
const existing = sources.get(definition.id);
|
|
34
|
-
if (existing && existing !== definition) {
|
|
35
|
-
// Two emails sharing an id would silently merge their metrics and make the
|
|
36
|
-
// catalog claim one exists when the other actually sent.
|
|
37
|
-
throw new Error(`Duplicate transactional email id "${definition.id}". Ids must be unique across the app.`);
|
|
38
|
-
}
|
|
39
|
-
const resolved = {
|
|
40
|
-
...definition,
|
|
41
|
-
app: definition.app ?? getAppSlug() ?? "unknown",
|
|
42
|
-
};
|
|
43
|
-
registry.set(definition.id, resolved);
|
|
44
|
-
sources.set(definition.id, definition);
|
|
45
|
-
return resolved;
|
|
46
|
-
}
|
|
47
|
-
/** Every registered email, sorted by app then name. */
|
|
48
|
-
export function listTransactionalEmails() {
|
|
49
|
-
return [...registry.values()].sort((a, b) => a.app.localeCompare(b.app) || a.name.localeCompare(b.name));
|
|
50
|
-
}
|
|
51
|
-
export function getTransactionalEmail(id) {
|
|
52
|
-
return registry.get(id);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Render one email's preview. Throws when the id is unknown or the renderer
|
|
56
|
-
* fails — a preview that silently returns an empty body would look like an
|
|
57
|
-
* email that legitimately renders blank.
|
|
58
|
-
*/
|
|
59
|
-
export function renderTransactionalEmailPreview(id) {
|
|
60
|
-
const definition = registry.get(id);
|
|
61
|
-
if (!definition) {
|
|
62
|
-
throw new Error(`Unknown transactional email "${id}".`);
|
|
63
|
-
}
|
|
64
|
-
return definition.preview();
|
|
65
|
-
}
|
|
66
|
-
/** Test seam — drops all registrations. */
|
|
67
|
-
export function resetTransactionalEmailRegistry() {
|
|
68
|
-
registry.clear();
|
|
69
|
-
sources.clear();
|
|
70
|
-
}
|
|
71
|
-
//# sourceMappingURL=registry.js.map
|