@hogsend/cli 0.2.0 → 0.2.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hogsend/cli",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -32,7 +32,7 @@
32
32
  "tsup": "^8.5.1",
33
33
  "tsx": "^4.22.4",
34
34
  "vitest": "^4.1.7",
35
- "@hogsend/studio": "^0.3.0",
35
+ "@hogsend/studio": "^0.5.0",
36
36
  "@repo/typescript-config": "0.0.0"
37
37
  },
38
38
  "engines": {
@@ -59,7 +59,7 @@ export const welcome = defineJourney({
59
59
  ## Key concepts
60
60
 
61
61
  - **`ctx` is orchestration primitives ONLY** — `sleep`, `sleepUntil`, `when`,
62
- `checkpoint`, `trigger`, `identify`, `guard.isSubscribed`,
62
+ `waitForEvent`, `checkpoint`, `trigger`, `identify`, `guard.isSubscribed`,
63
63
  `history.hasEvent/journey/email`, `posthog.capture`. Features are standalone
64
64
  imports: `sendEmail()` and `getPostHog()` come from `@hogsend/engine`, NOT off
65
65
  `ctx`.
@@ -63,6 +63,30 @@ export const activation = defineJourney({
63
63
  after a long `ctx.sleep` — a user can unsubscribe during the wait. Enrollment
64
64
  only checks preferences at entry, not at each send.
65
65
 
66
+ ## Reactive alternative: `ctx.waitForEvent`
67
+
68
+ The pattern above sleeps a **fixed window** then polls history — good when you
69
+ want to wait a set time regardless. When you're waiting **for a specific event to
70
+ happen** (and want to react the moment it does, or give up after a deadline),
71
+ `ctx.waitForEvent` is the sharper tool: it resumes the instant the user fires the
72
+ event, or returns `timedOut: true` when the timeout wins.
73
+
74
+ ```ts
75
+ const { timedOut } = await ctx.waitForEvent({
76
+ event: Events.FEATURE_USED,
77
+ timeout: days(7), // required; capped at the 720h task limit
78
+ label: "await-activation",
79
+ });
80
+ if (!timedOut) return; // they activated on their own — done
81
+ if (!(await ctx.guard.isSubscribed())) return;
82
+ await sendEmail({ /* nudge */ });
83
+ ```
84
+
85
+ Rule of thumb: **fixed delay → `ctx.sleep` then `ctx.history.hasEvent`**;
86
+ **wait until they do X (or time out) → `ctx.waitForEvent`**. The wait is
87
+ forward-looking (only events after it begins count), and an `exitOn` match
88
+ cancels it mid-wait, so no post-wait send fires after the journey exits.
89
+
66
90
  ## Idempotency — journeys can replay
67
91
 
68
92
  A journey task is durable, and a step before a `ctx.sleep` can be re-executed if
@@ -29,6 +29,29 @@ await ctx.sleepUntil(at, { label: "morning-nudge" });
29
29
  // .in(days(3)).at("HH:mm"), and chainers .tz(zone) / .window(start,end) / .ifPast("next"|"now")
30
30
  ```
31
31
 
32
+ ## Durable wait-for-event
33
+
34
+ ```ts
35
+ // Park the journey until THIS user emits `event`, OR `timeout` elapses —
36
+ // whichever first. The reactive alternative to "sleep a fixed window, then poll
37
+ // ctx.history": it resumes the INSTANT the event lands. Forward-looking — only
38
+ // events fired AFTER the wait begins count (use ctx.history.hasEvent for the past).
39
+ const { timedOut } = await ctx.waitForEvent({
40
+ event: Events.FEATURE_USED,
41
+ timeout: days(7), // REQUIRED, capped at the 720h task execution limit
42
+ label: "await-activation", // optional — written as currentNodeId
43
+ });
44
+ if (timedOut) {
45
+ // they never did it — nudge (re-check ctx.guard.isSubscribed() first after a long wait)
46
+ } else {
47
+ // event arrived — they activated on their own
48
+ }
49
+ ```
50
+
51
+ If the journey `exitOn`-matches (or is cancelled) WHILE waiting, the run aborts
52
+ cleanly — state goes `"exited"`, the durable run is cancelled, and no post-wait
53
+ step (or email) fires. You don't catch anything; the engine handles it.
54
+
32
55
  ## Observability
33
56
 
34
57
  ```ts
@@ -129,13 +129,16 @@ overlapping runs of the same journey.
129
129
  A `journeyStates` row tracks each run. Once the gates pass:
130
130
 
131
131
  - **enter** → row created with `status: "active"`, `currentNodeId: "start"`.
132
- - **`ctx.sleep` / `ctx.sleepUntil`** → `status: "waiting"` for the duration, back
133
- to `"active"` on resume.
132
+ - **`ctx.sleep` / `ctx.sleepUntil` / `ctx.waitForEvent`** → `status: "waiting"`
133
+ while suspended, back to `"active"` on resume.
134
134
  - **`run()` returns** → `status: "completed"`, `completedAt` set, and a
135
135
  `journey:completed` event is pushed.
136
136
  - **`run()` throws** → `status: "failed"`, `errorMessage` recorded, and a
137
137
  `journey:failed` event is pushed; the error re-throws so Hatchet sees the
138
138
  failure.
139
+ - **`exitOn` matches (or cancelled)** → `status: "exited"`. If it happens while
140
+ the journey is suspended in a `ctx.sleep`/`ctx.waitForEvent`, the durable run
141
+ is cancelled so no further step runs — even mid-wait.
139
142
 
140
143
  Because the gates run before any state is created, a skipped event is invisible
141
144
  in `journeyStates` — to debug "why didn't this user enroll?", check the gate
@@ -0,0 +1,82 @@
1
+ ---
2
+ name: hogsend-extending
3
+ description: Use when extending a Hogsend app beyond journeys/emails/buckets — swapping the email or analytics provider behind its engine-owned contract (EmailProvider / PostHogService), wiring an outbound integration (Slack, a CRM, Stripe) as plain code called from a journey, or deciding when to publish a reusable @hogsend/plugin-* package. Covers the two categories of extension and where each is wired.
4
+ license: MIT
5
+ metadata:
6
+ author: withSeismic
7
+ version: "1.0.0"
8
+ ---
9
+
10
+ # Extending Hogsend
11
+
12
+ There are **two** ways to extend a Hogsend app, and they are different
13
+ mechanisms — don't reach for the wrong one.
14
+
15
+ 1. **Capability providers** (email, analytics). The engine itself drives these,
16
+ so each has an **engine-owned contract** you implement and swap behind.
17
+ `EmailProvider` and `PostHogService` are defined in `@hogsend/core` and
18
+ re-exported from `@hogsend/engine` (the canonical import). You supply an
19
+ implementation via `createHogsendClient({ email: { provider }, analytics })`
20
+ and the engine routes to it — including inbound provider webhooks.
21
+ `@hogsend/plugin-resend` and `@hogsend/plugin-posthog` are the **bundled
22
+ defaults and reference implementations**; you only swap when you want a
23
+ different vendor. → `references/swap-a-provider.md`.
24
+
25
+ 2. **Integrations** (everything you call *out* to — Slack, a CRM, Stripe, an
26
+ internal HTTP API). **No contract, no framework.** Install the SDK, write a
27
+ thin wrapper in your own `src/lib/`, import it into a journey, and call it like
28
+ a function. The engine never sees it. → `references/build-an-integration.md`.
29
+
30
+ **The deciding question: does the engine call it, or do you?** If the engine
31
+ drives the capability (sending mail, capturing analytics) it's a provider behind
32
+ a contract. If your journey reaches outward, it's a plain integration.
33
+
34
+ ## Swapping a capability provider — the short version
35
+
36
+ - **Implement the contract.** `import type { EmailProvider } from "@hogsend/engine"`
37
+ — four methods: `send`, `sendBatch`, `verifyWebhook`, `parseWebhook`. The
38
+ reference implementation to copy is `packages/plugin-resend/src/provider.ts`
39
+ (`createResendProvider`).
40
+ - **Wire it.** `createHogsendClient({ email: { templates, provider: createMyProvider(...) } })`.
41
+ Analytics is a **top-level** option (the engine itself fires captures):
42
+ `createHogsendClient({ analytics: createMyAnalytics(...) })`.
43
+ - **You get everything else for free.** Template rendering, link-click + open
44
+ tracking, preference/suppression checks, the frequency cap, and the
45
+ `email_sends` row all live in the engine's `createTrackedMailer` — never in the
46
+ provider. They come along regardless of which provider you supply.
47
+ - **Defaults.** Pass nothing and you get Resend (built from `RESEND_API_KEY`) +
48
+ PostHog (from `POSTHOG_API_KEY`). Inbound delivery webhooks land at the
49
+ engine-owned route `POST /v1/webhooks/resend`.
50
+ - ⚠️ The contract's `SendEmailOptions` imports from `@hogsend/core` (or
51
+ `@hogsend/plugin-resend`), **not** `@hogsend/engine` — the engine's own
52
+ `SendEmailOptions` is a different, higher-level send type.
53
+
54
+ ## Wiring an integration — the short version
55
+
56
+ - Install the SDK; write `src/lib/<service>.ts` exporting a
57
+ `create<Service>(config)` factory that **validates config at construction, not
58
+ at import** (so tests don't blow up on a missing env var).
59
+ - Import it into a journey and call it: `await slack.sendMessage({ ... })`. It's a
60
+ function call, **not** `ctx.sendMessage` — `ctx` is orchestration-only.
61
+ - Heavy or background work (a nightly CRM sync, a fan-out import) → author a
62
+ Hatchet task in `src/workflows/` and register it via
63
+ `createWorker({ extraWorkflows })`.
64
+
65
+ ## When to publish a `@hogsend/plugin-*` package
66
+
67
+ Almost never from a scaffolded app. A standalone module in your `src/` is the
68
+ right default. Author and publish a real `@hogsend/plugin-*` package only when an
69
+ integration is **reusable across multiple apps** or you intend to **contribute it
70
+ back to the engine** — that is engine-development work done in a clone of the
71
+ Hogsend monorepo, not in your client app.
72
+
73
+ ## What NOT to do
74
+
75
+ - **Don't put a service on `ctx`.** `ctx` is durable-orchestration primitives only
76
+ (`sleep`, `checkpoint`, `trigger`, `guard`, `history`, `posthog`, `identify`).
77
+ - **Don't reach for a provider contract for a one-directional call** — that's an
78
+ integration (just code).
79
+ - **Don't import the `EmailProvider`/`PostHogService` contract from a
80
+ `@hogsend/plugin-*` package** in new code — use `@hogsend/engine` (canonical).
81
+ The plugins still re-export the contracts for back-compat, but new code should
82
+ import from the engine.
@@ -0,0 +1,114 @@
1
+ # Building an integration
2
+
3
+ An integration is **something your journey calls out to** — Slack, a CRM, Stripe,
4
+ an internal HTTP API. It has **no contract** and the engine knows nothing about
5
+ it. The simplest integration is the best one: install the SDK, write a thin
6
+ wrapper, import it into a journey.
7
+
8
+ This is *not* a capability provider — don't implement an engine contract for it,
9
+ and don't put it on `ctx`. (For email/analytics, which the engine drives, see
10
+ `swap-a-provider.md`.)
11
+
12
+ ## 1. Install the SDK
13
+
14
+ ```bash
15
+ pnpm add @slack/web-api
16
+ ```
17
+
18
+ ## 2. Write a thin wrapper — fail at construction, not at import
19
+
20
+ ```ts
21
+ // src/lib/slack.ts — your content
22
+ import { WebClient } from "@slack/web-api";
23
+
24
+ export interface SlackServiceConfig {
25
+ token: string;
26
+ defaultChannel?: string;
27
+ }
28
+
29
+ export function createSlackService(config: SlackServiceConfig) {
30
+ if (!config.token) {
31
+ throw new Error("SlackServiceConfig.token is required");
32
+ }
33
+ const client = new WebClient(config.token);
34
+
35
+ return {
36
+ async sendMessage(opts: { channel?: string; text: string }) {
37
+ const channel = opts.channel ?? config.defaultChannel;
38
+ if (!channel) throw new Error("No channel and no defaultChannel configured");
39
+ try {
40
+ const result = await client.chat.postMessage({ channel, text: opts.text });
41
+ return { ts: result.ts, channel: result.channel };
42
+ } catch (error) {
43
+ // Don't swallow — let the journey's error handling mark the run failed.
44
+ throw new Error(
45
+ `Slack sendMessage failed for ${channel}: ${
46
+ error instanceof Error ? error.message : String(error)
47
+ }`,
48
+ );
49
+ }
50
+ },
51
+ };
52
+ }
53
+ ```
54
+
55
+ Top-level `process.env.X!` access throws on *import* (even in tests). Validate
56
+ inside the factory instead.
57
+
58
+ ## 3. Use it in a journey — a function call, not `ctx`
59
+
60
+ ```ts
61
+ // src/journeys/churn-alert.ts — your content
62
+ import { days } from "@hogsend/core";
63
+ import { defineJourney, sendEmail } from "@hogsend/engine";
64
+ import { createSlackService } from "../lib/slack.js";
65
+ import { Events, Templates } from "./constants/index.js";
66
+
67
+ const slack = createSlackService({
68
+ token: process.env.SLACK_BOT_TOKEN ?? "",
69
+ defaultChannel: "#lifecycle-alerts",
70
+ });
71
+
72
+ export const churnAlert = defineJourney({
73
+ meta: { id: "churn-alert", name: "Churn alert", enabled: true, trigger: { event: Events.PAYMENT_FAILED } },
74
+ run: async (user, ctx) => {
75
+ await slack.sendMessage({ text: `Payment failed for ${user.email}.` });
76
+ await sendEmail({ to: user.email, userId: user.id, template: Templates.CHURN_PAYMENT_FAILED, subject: "Your payment didn't go through" });
77
+ await ctx.sleep({ duration: days(2) });
78
+ // ...escalate if still failing
79
+ },
80
+ });
81
+ ```
82
+
83
+ `slack.sendMessage` and `sendEmail` are both plain imports — neither is on `ctx`.
84
+ That keeps the journey context focused on orchestration and avoids coupling
85
+ integrations to the engine.
86
+
87
+ ## Conventions
88
+
89
+ - **Optional caching.** If your service fetches slow-changing data (like
90
+ `@hogsend/plugin-posthog` does for person properties), accept an optional Redis
91
+ client + TTL in config and cache there.
92
+ - **Need the DB?** Open a connection with `createDatabase()` from `@hogsend/db`
93
+ against your `DATABASE_URL`, or query a client-track table you defined in
94
+ `src/schema/`.
95
+ - **Cleanup?** Expose a `shutdown()` and call it from your `src/worker.ts`
96
+ graceful-shutdown handler alongside `worker.stop()`.
97
+ - **Testing.** Test against a mocked SDK client — no real API calls. The bundled
98
+ `packages/plugin-resend/src/__tests__/` show the pattern.
99
+
100
+ ## Background jobs (Hatchet tasks)
101
+
102
+ Some integrations are better as durable background work than inline — a nightly
103
+ CRM sync, a heavy backfill, a fan-out import. Author them as Hatchet tasks in your
104
+ `src/workflows/` and register via `createWorker({ extraWorkflows })` (the option
105
+ is `extraWorkflows`, NOT `workflows`). They run on the same worker as your
106
+ journeys. For heavy backfills, use `runBatchedBackfill` from `@hogsend/engine`;
107
+ the scaffold ships a `src/workflows/backfill-example.ts` to copy. See the
108
+ `hogsend-webhooks-and-workflows` skill for the full pattern.
109
+
110
+ ## What integrations are not
111
+
112
+ No manifest, no auto-discovery, no lifecycle hooks, no registration. You import
113
+ what you need, where you need it. Integrations live entirely in your content, and
114
+ the engine upgrades underneath them with `pnpm up`.
@@ -0,0 +1,126 @@
1
+ # Swapping a capability provider
2
+
3
+ A capability provider is a **swappable implementation of an engine-owned
4
+ contract**. The engine drives the capability and routes to whatever you supply.
5
+ Today there are two: email (`EmailProvider`) and analytics (`PostHogService`).
6
+
7
+ ## The `EmailProvider` contract
8
+
9
+ Defined in `@hogsend/core`, re-exported canonically from `@hogsend/engine`. It is
10
+ a **dumb wire** — delivery + webhook parse/verify only. No tracking, DB,
11
+ preference, or render logic lives here.
12
+
13
+ ```ts
14
+ import type { EmailProvider } from "@hogsend/engine";
15
+
16
+ interface EmailProvider {
17
+ send(options: SendEmailOptions): Promise<SendResult>; // → { id }
18
+ sendBatch(emails: BatchEmailItem[]): Promise<{ results: SendResult[] }>;
19
+ // Verify a provider webhook signature and return the parsed event.
20
+ // Throws if the signature is missing/invalid.
21
+ verifyWebhook(opts: { payload: string; headers: Record<string, string> }): WebhookEvent;
22
+ // Parse an unsigned payload (trusted contexts/tests).
23
+ parseWebhook(payload: string): WebhookEvent;
24
+ }
25
+ ```
26
+
27
+ `SendEmailOptions`, `BatchEmailItem`, `SendResult`, and `WebhookEvent` are the
28
+ contract's supporting types. **Import the contract types from `@hogsend/engine`**,
29
+ **except `SendEmailOptions`**, which collides with the engine's higher-level send
30
+ type — import that one from `@hogsend/core`:
31
+
32
+ ```ts
33
+ import type { EmailProvider, SendResult, WebhookEvent } from "@hogsend/engine";
34
+ import type { SendEmailOptions } from "@hogsend/core";
35
+ ```
36
+
37
+ ## A provider skeleton
38
+
39
+ The reference implementation to copy is `createResendProvider`
40
+ (`packages/plugin-resend/src/provider.ts`). A custom one mirrors it:
41
+
42
+ ```ts
43
+ // src/lib/my-email-provider.ts — your content
44
+ import type { SendEmailOptions } from "@hogsend/core";
45
+ import type { EmailProvider, SendResult, WebhookEvent } from "@hogsend/engine";
46
+
47
+ export function createMyEmailProvider(config: { apiKey: string; webhookSecret?: string }): EmailProvider {
48
+ return {
49
+ async send(options: SendEmailOptions): Promise<SendResult> {
50
+ // Call your vendor's SDK. The engine hands you HTML already rewritten for
51
+ // link/open tracking (options.html) on the tracked path; render
52
+ // options.react yourself (renderToHtml/renderToPlainText from
53
+ // @hogsend/email) only if your vendor can't take React.
54
+ const id = await myVendor.send({ from: options.from, to: options.to, subject: options.subject, html: options.html });
55
+ return { id };
56
+ },
57
+ async sendBatch(emails) {
58
+ const results = await Promise.all(emails.map((e) => this.send(e as never)));
59
+ return { results };
60
+ },
61
+ verifyWebhook({ payload, headers }): WebhookEvent {
62
+ if (!config.webhookSecret) throw new Error("webhookSecret required to verify webhooks");
63
+ // Verify with your vendor's scheme, then NORMALIZE into the engine's
64
+ // WebhookEvent shape ({ type: "email.delivered" | "email.bounced" | ... }).
65
+ return normalizeMyVendorEvent(verify(payload, headers, config.webhookSecret));
66
+ },
67
+ parseWebhook(payload): WebhookEvent {
68
+ return normalizeMyVendorEvent(JSON.parse(payload));
69
+ },
70
+ };
71
+ }
72
+ ```
73
+
74
+ ## Wire it
75
+
76
+ ```ts
77
+ // src/index.ts — your content
78
+ import { createHogsendClient } from "@hogsend/engine";
79
+ import { templates } from "./emails/registry.js";
80
+ import { createMyEmailProvider } from "./lib/my-email-provider.js";
81
+
82
+ const client = createHogsendClient({
83
+ journeys,
84
+ email: {
85
+ templates, // REQUIRED, nested under email
86
+ provider: createMyEmailProvider({ apiKey: process.env.MY_API_KEY! }),
87
+ },
88
+ // analytics: createMyAnalytics(...), // top-level (engine uses it)
89
+ });
90
+ ```
91
+
92
+ Pass nothing under `email.provider` and the engine builds the **default Resend
93
+ provider** from `RESEND_API_KEY` / `RESEND_WEBHOOK_SECRET`.
94
+
95
+ ## What comes along for free
96
+
97
+ Everything except the wire. The engine's `createTrackedMailer` runs, in order:
98
+ check preferences/suppression → frequency cap → resolve + render the template →
99
+ write the `email_sends` row (status `queued`) → rewrite links + inject the open
100
+ pixel → **then** `provider.send(...)` → update status. So a swapped provider
101
+ keeps **all** of tracking, rendering, preferences, and the `email_sends`
102
+ pipeline.
103
+
104
+ ## Inbound webhooks
105
+
106
+ The engine owns one inbound email-webhook route: `POST /v1/webhooks/resend`. It
107
+ reads the raw body + headers and calls your provider's `verifyWebhook`, then maps
108
+ the normalized `WebhookEvent` to `email_sends` status updates and
109
+ bounce/complaint → suppression. Your provider only has to verify + normalize; the
110
+ DB effects are engine-owned.
111
+
112
+ ## Analytics (`PostHogService`)
113
+
114
+ Same shape: the `PostHogService` contract lives in `@hogsend/core`
115
+ (canonical `@hogsend/engine`); `createPostHogService` (`@hogsend/plugin-posthog`)
116
+ is the default + reference impl. Supply your own via the **top-level**
117
+ `createHogsendClient({ analytics })` option. PostHog is optional — with no
118
+ `POSTHOG_API_KEY` the engine resolves analytics to `undefined` and every capture
119
+ is a no-op.
120
+
121
+ ## Don't over-reach
122
+
123
+ You are implementing a contract, not building a framework. There is no provider
124
+ registry, no marketplace, and no `@hogsend/provider-*` packages — to support a
125
+ new vendor you implement `EmailProvider` (or `PostHogService`) and pass it in.
126
+ That's the whole story.