@flowapt/flowiq-cli 0.4.9 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -199,6 +199,16 @@ template's variables **per row** from the CSV's own columns. The
199
199
  highest-stakes command in the CLI — built as a map → preview → commit
200
200
  pipeline with several layers of deliberate friction.
201
201
 
202
+ **v0.6.0 — the CSV send is a server-side fan-out.** A commit imports every
203
+ sendable row as a contact carrying the campaign tag `bc-<campaign>` plus that
204
+ row's values as campaign-scoped attributes (`bc_<campaign>_param1…` /
205
+ `_button`), then fires **ONE** python `/meta-broadcast` call whose params are
206
+ `{{attributes.bc_<campaign>_paramN}}` tokens — python resolves them **per
207
+ contact** (body AND URL-button) and paces the send server-side. The CLI
208
+ returns in seconds; closing the laptop no longer matters. A 1,846-row CSV
209
+ that took ~77 min on the old per-row loop is now python-paced like any big
210
+ tag send.
211
+
202
212
  ```bash
203
213
  # 1) Build the column→param mapping interactively (saved per campaign, no send)
204
214
  flowiq bc map <org_id> --template rewards_referral_v1 --csv ./people.csv --campaign july-referrals
@@ -206,11 +216,15 @@ flowiq bc map <org_id> --template rewards_referral_v1 --csv ./people.csv --campa
206
216
  # 2) Dry-run: validation + exact rendered messages for sample rows (default)
207
217
  flowiq bc send <org_id> --template rewards_referral_v1 --csv ./people.csv --campaign july-referrals
208
218
 
209
- # 3) Commit: upsert contacts, then send per row @ ≤8/s
219
+ # 3) Commit: import+tag the rows, fire ONE python broadcast (returns a broadcastId)
210
220
  flowiq bc send <org_id> --template … --csv … --campaign july-referrals --commit
211
221
  # → prompts: type the campaign name to confirm
222
+ # → then audit with: flowiq bc status <org_id> <broadcastId> [--failures]
223
+
224
+ # 4) Re-fire ONLY the failed recipients (python campaigns have no per-row resume):
225
+ flowiq bc retry <org_id> <broadcastId> --commit
212
226
 
213
- # 4) Interrupted? Continue only unsent rows:
227
+ # (pre-v0.6.0 campaigns with a per-row status log still finish on the old loop:)
214
228
  flowiq bc resume <org_id> --campaign july-referrals --commit [--retry-failed]
215
229
 
216
230
  # SCHEDULE a tag send for later instead of sending now (--at is SAST):
@@ -307,10 +321,31 @@ flowiq bc scheduled cancel <org_id> <queue_id> --confirm
307
321
  DRY-RUN by default (shows the failed count + reason breakdown); `--commit` fires.
308
322
  Note: permanent failures (e.g. `131026` undeliverable, opt-outs) just fail again —
309
323
  retry earns its keep on transient (throttle/throughput) failures. Capped at 500.
324
+ - **CSV fan-out semantics (v0.6.0)** — the things the engine change makes true:
325
+ - **Per-row values persist on the contact** as `bc_<campaign>_paramN` /
326
+ `_button` attributes (and the `bc-<campaign>` tag) — deliberate: they're the
327
+ audit trail of exactly what each person was sent. Re-running a campaign
328
+ overwrites them with the current CSV's values. Names are safe: a blank/missing
329
+ CSV name can never null a real contact's name (the upsert RPC preserves it).
330
+ - **One campaign = one audience.** A campaign that already fanned out refuses
331
+ a re-run (`--resend` overrides, and resends to EVERY tagged contact — for
332
+ failures use `bc retry`, which re-fires only the failed ones). Smoke-test
333
+ with `--limit` under a THROWAWAY `--campaign`: a limited fan-off still
334
+ records the campaign as fired, and the full run would need `--resend`.
335
+ - **The audience is the TAG, not the file.** If an earlier run of the same
336
+ campaign tagged rows since removed from the CSV, python still resolves them —
337
+ the CLI cross-checks python's count against the CSV after tagging and makes
338
+ you type `YES` when the tag carries MORE people than the file.
339
+ - No per-row write-ahead log or `resume` on fresh sends — the campaign log
340
+ records ONE `handoff` line (broadcastId); delivery/failure detail lives in
341
+ `bc status <org> <broadcastId> [--failures]` and `bc retry`.
342
+ - `--rate` / `--skip-upsert` only apply to pre-v0.6.0 `resume`; fresh sends
343
+ ignore them (python paces itself; the upsert IS the mechanism).
310
344
  - **Engine auto-routing (v0.3.6):** any `send --tag` of **more than 10** eligible
311
345
  recipients **always uses the python engine** (a `>2000` tag routes there too).
312
- Only a ≤10 send stays on the resumable per-row Node engine. `--python` forces
313
- python at any size. (CSV per-row sends stay on Node — python takes uniform params.)
346
+ Only a ≤10 tag send stays on the per-row Node engine. `--python` forces
347
+ python at any size. **CSV sends are ALWAYS python from v0.6.0** (per-row values
348
+ ride as contact attributes — see above).
314
349
  - **`--python` engine (v0.3.4, `send --tag` only)**: hand the whole send to the
315
350
  **python `/meta-broadcast` endpoint** — the *same* sender the dashboard's "Python
316
351
  endpoint" toggle uses — via a staff-gated proxy (the master key stays server-side
@@ -350,17 +385,19 @@ flowiq bc scheduled cancel <org_id> <queue_id> --confirm
350
385
  blocked contacts are skipped, always.
351
386
  - **Dry-run is the default**; `--commit` + typing the campaign name is the
352
387
  only way to send (`--yes` for CI skips the typing, never the dry-run).
353
- - **Write-ahead status log** (`.flowiq/campaigns/<campaign>.status.jsonl`):
354
- every row logs `sending` *before* the POST and `sent`/`failed` after. A
355
- crash mid-row leaves an *ambiguous* row that is **never auto-resent** —
356
- it's surfaced for manual review. `resume` sends only never-attempted rows
357
- (`--retry-failed` adds confirmed failures).
358
- - **Error handling**: `#132000` aborts the run (config bug — every row would
359
- fail); rate errors back off exponentially and halve the send rate; a Meta
360
- daily-cap soft-stops cleanly with a resume hint.
361
- - Contacts are **pre-upserted** (`/cli/contacts-upsert`, ≤4000/batch, retries
362
- then aborts) so the send payload never needs a `name` key — the classic
363
- `#132000` footgun.
388
+ - **Campaign status log** (`.flowiq/campaigns/<campaign>.status.jsonl`): a
389
+ fresh (v0.6.0) send records a `meta` header + ONE `handoff` line carrying the
390
+ `broadcastId` — the re-run guard and your local pointer to `bc status`.
391
+ Pre-v0.6.0 campaigns keep their per-row `sending`/`sent`/`failed` lines and
392
+ finish on `resume` (ambiguous mid-crash rows are still never auto-resent;
393
+ `--retry-failed` adds confirmed failures).
394
+ - **Error handling**: all pre-send validation is unchanged and client-side;
395
+ per-recipient send errors are python-side — audit them with
396
+ `bc status <org> <broadcastId> --failures`, re-fire with `bc retry`.
397
+ - Contacts are **imported/updated** via `/cli/contacts-upsert` (≤4000/batch,
398
+ retries then aborts) carrying the campaign tag + attributes; the python
399
+ params are pure `{{attributes.*}}` tokens, so the payload never needs a
400
+ `name` key — the classic `#132000` footgun stays structurally impossible.
364
401
 
365
402
  **Tag mode** — send to everyone carrying a tag (e.g. a `segments` batch tag)
366
403
  instead of a CSV. Same guards, same status log, same resume:
@@ -759,6 +796,40 @@ Validation: `platform` ∈ `{whatsapp, web}`, `type` from the per-platform
759
796
  allowed enum, valid http(s) URL, no duplicate (platform, type, url)
760
797
  tuples.
761
798
 
799
+ **Auth fields (19 Aug 2026).** Each row also carries `auth_type`
800
+ (`none` | `bearer` | `basic` | `api_key` | `oauth2_client_credentials`),
801
+ `auth_config`, `headers` and `secret` — how FlowIQ authenticates when it POSTs
802
+ the event, plus an optional HMAC signing key. Pull returns all four; push
803
+ writes all four.
804
+
805
+ **Because push is FULL-REPLACE, a body that omits these fields STRIPS the
806
+ credentials off every webhook.** Always pull-edit-push the same file; never
807
+ hand-write a push body. The dry-run diff includes an auth fingerprint, so
808
+ changing only a credential correctly shows as `to insert 1 / to delete 1`
809
+ rather than "unchanged".
810
+
811
+ ```jsonc
812
+ {
813
+ "url": "https://middleware.example.com/notification/log",
814
+ "type": "sent_message",
815
+ "platform": "whatsapp",
816
+ "auth_type": "oauth2_client_credentials",
817
+ "auth_config": {
818
+ "token_url": "https://middleware.example.com/oauth2/token",
819
+ "client_id": "acme_flowapt",
820
+ "client_secret": "…",
821
+ "scope": "notify:write", // optional
822
+ "client_auth": "basic" // optional: "basic" (default) | "body"
823
+ },
824
+ "headers": { "X-Tenant": "acme" }, // optional static headers
825
+ "secret": "…" // optional → X-Flowiq-Signature: sha256=<hmac>
826
+ }
827
+ ```
828
+
829
+ Push refuses an `auth_type` whose credentials are incomplete (e.g. `bearer`
830
+ with no `auth_config.token`), because that would make every delivery fail
831
+ closed — FlowIQ never falls back to an unauthenticated send.
832
+
762
833
  ### FlowMod prompts — `flowiq flowmod pull|push <slug>` (alias `fm`)
763
834
 
764
835
  Round-trips a FlowMod org's **master-group** prompts + config. FlowMod groups
@@ -834,6 +905,33 @@ Editable fields: `name`, `description`, `status` (open / in_progress / done),
834
905
  null), `organizations[]`, `media[]`, `messages[]`, `created_by`. Array columns
835
906
  are full-replace within the row.
836
907
 
908
+ ### Client hours — `flowiq hours log|list|summary` (v0.5.0)
909
+
910
+ The client-hours ledger. Every org's package includes **10 hours of Flowapt
911
+ work per calendar month**; every piece of client work gets logged against it —
912
+ manually here, or automatically by Claude sessions via the FlowIQ MCP's
913
+ `log_client_hours` tool. Clients see their own usage on the Client Console;
914
+ the cross-org view lives in the super-admin Changelog dialog → Client hours.
915
+
916
+ ```bash
917
+ flowiq hours log <org_id> --hours 1.5 --desc "Rebuilt the abandoned-cart copy" # fractions fine
918
+ flowiq hours log <org_id> --minutes 45 --desc "Fixed template header" --date 2026-08-18
919
+ flowiq hours list <org_id> # this month's entries + package standing
920
+ flowiq hours list <org_id> --month 2026-07 # a past month
921
+ flowiq hours summary # every org with logged work this month
922
+ ```
923
+
924
+ - `log` takes exactly ONE of `--minutes <n>` (whole minutes) or `--hours <h>`
925
+ (fractions fine — `--hours 1.5` = 90 min). `--desc` is required and should be
926
+ plain language the client could read (it shows on their Client Console).
927
+ - `--by` overrides who did the work (default: your staff login email);
928
+ `--date YYYY-MM-DD` backdates an entry (it counts toward THAT month);
929
+ `--session` tags the session/task name.
930
+ - `list` / `summary` default to the current month and print each org's usage
931
+ against the 10h package, flagging any org that has gone over.
932
+ - Corrections (delete/edit) live in the Changelog → Client hours page, not the
933
+ CLI. Every `log` is audited.
934
+
837
935
  ### WhatsApp templates — `flowiq templates pull|list|create|status` (alias `tpl`)
838
936
 
839
937
  Read an org's live templates straight from Meta (read-only), and submit new
package/TEAM-GUIDE.md CHANGED
@@ -60,7 +60,11 @@ When you see it, run `npm i -g @flowapt/flowiq-cli` — a stale version also mea
60
60
  would change before anything writes.
61
61
  3. **Pushes are full-replace.** What's in your file becomes the whole surface
62
62
  (all prompt sections, all tools, all webhooks). Deleting an entry from the
63
- file deletes it from the platform.
63
+ file deletes it from the platform. This is also why you must never
64
+ hand-write a push body: messaging webhooks carry their own credentials
65
+ (`auth_type` / `auth_config` / `headers` / `secret`), and a body that leaves
66
+ those fields out strips the auth off every webhook for that org. Pull, edit
67
+ that same file, push it.
64
68
  4. **`--agent <id>` targets a specific agent**; without it you get the org's
65
69
  *active* agent. Files remember which agent they came from, so a push always
66
70
  goes back to the agent you pulled — never "whatever is active now".
@@ -101,7 +105,7 @@ When you see it, run `npm i -g @flowapt/flowiq-cli` — a stale version also mea
101
105
  | Short links for every URL in a message | `flowiq links shorten <org_id> --file ./message.txt --campaign X --content X --commit` |
102
106
  | Check which links a campaign has, and their clicks | `flowiq links list <org_id> --campaign 13Aug_Seeds` |
103
107
  | A link already looks short (`linklnk.io/abc123`) — can I re-tag it? | No: paste the DESTINATION url instead. Re-shortening keeps the old campaign tag and splits the click count, so the CLI refuses it |
104
- | Send a template broadcast to a CSV of people | `flowiq bc map <org_id> --template … --csv …` → `flowiq bc send … ` (dry-run) → `… --commit` |
108
+ | Send a template broadcast to a CSV of people | `flowiq bc map <org_id> --template … --csv …` → `flowiq bc send … ` (dry-run) → `… --commit`. **v0.6.0: the commit imports every row as a contact (tag `bc-<campaign>` + the row's values as attributes) and fires ONE python broadcast** — it returns in seconds with a `broadcastId`; python sends in the background. Check delivery with `bc status`, re-fire failures with `bc retry`. A campaign that already fired refuses a re-run (`--resend` deliberately resends to everyone tagged). |
105
109
  | Tag every repeat buyer (e.g. 2+ orders in the last 60 days) | `flowiq seg plan <org_id> --tag-prefix repeat-60d --min-orders 2 --window 60d` → `flowiq seg apply <org_id> repeat-60d` (dry run) → `… --commit` |
106
110
  | Tag everyone who bought a product (accurate, windowable) | `flowiq seg plan <org_id> --tag-prefix whey --bought "Whey" --window 90d` → `flowiq seg apply … --commit` |
107
111
  | Advanced Tagging in the terminal (one named tag on a matched set) | `flowiq tag field\|cohort\|segment\|attributes\|messages <org_id> …` (dry-run) → add `--tag <name> --commit` |
@@ -128,12 +132,14 @@ When you see it, run `npm i -g @flowapt/flowiq-cli` — a stale version also mea
128
132
  | Export an org's full chat history | `flowiq export chats <org_id>` |
129
133
  | Check / create WhatsApp templates | `flowiq tpl pull <org_id>` / `flowiq tpl create <org_id> --request-file req.json` |
130
134
  | Manage Shopify/Woo platform webhooks | `flowiq wh pull <org_id>` → edit → `flowiq wh push <slug>` |
131
- | Manage outbound messaging webhooks | `flowiq mw pull <org_id>` → `flowiq mw push <slug> --dry-run` → push |
135
+ | Manage outbound messaging webhooks (incl. their auth) | `flowiq mw pull <org_id>` → `flowiq mw push <slug> --dry-run` → push |
132
136
  | Create a brand-new client org | `flowiq org create --name "Client Name"` → then `agent create` on the printed id |
133
137
  | See a client's pending change requests | `flowiq au pull <org_id>` |
134
138
  | Close a client's change request (after verifying the fix!) | `flowiq au resolve <update_id> --note "what changed"` — the client reads the note |
135
139
  | Check an org's platform + active agent | `flowiq org info <org_id>` |
136
140
  | Work a Pin Board task | `flowiq pin list-remote open` → `pull` → edit → `push` |
141
+ | Log hours you worked for a client (every package = 10h Flowapt work/month) | `flowiq hours log <org_id> --hours 1.5 --desc "what you did"` — plain language, the client sees it on their Client Console |
142
+ | Check a client's package hours / the month across all clients | `flowiq hours list <org_id>` / `flowiq hours summary` |
137
143
  | Re-read this guide / the full command reference | `flowiq guide` / `flowiq guide --reference` |
138
144
 
139
145
  The flow is the same everywhere: **pull → edit the JSON → push**. Slugs are the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowapt/flowiq-cli",
3
- "version": "0.4.9",
3
+ "version": "0.6.0",
4
4
  "description": "Command-line tool for FlowIQ staff: round-trip agent prompts, questionnaires, fine-tuning, pin-board tasks, webhooks, templates, agent-updates, chat exports, and live agent testing without ever touching service-role credentials.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -6,17 +6,23 @@
6
6
  // send <org> --template T --csv F [--commit] full pipeline; DRY-RUN by default
7
7
  // resume <org> --campaign C [--commit] continue from the status log
8
8
  //
9
- // Pipeline: introspect template (live Meta + DB labels, via /cli/broadcast) →
10
- // parse CSV → build/load column→param MAPPING (each row fills the template
11
- // with its OWN values) → validate (V-1..V-13) → broadcast-safety classify
12
- // (opted-out/archived/blocked are skipped) → preview → [--commit] upsert
13
- // contacts (/cli/contacts-upsert, ≤4000/batch, 3× retry then ABORT) → send
14
- // per row via /api/send-template (org-UUID branch, NO auth header), paced
15
- // ≤10 msg/s, with a WRITE-AHEAD per-row status log:
16
- // {status:"sending"} appended BEFORE the POST, the terminal sent/failed
17
- // line after — so a crash mid-row leaves an ambiguous row that is NEVER
18
- // auto-resent (surfaced for manual review). Resume dedups on
19
- // normalized-number-within-campaign, never on CSV fingerprint.
9
+ // Pipeline (v0.6.0 — python server-side fan-out): introspect template (live
10
+ // Meta + DB labels, via /cli/broadcast) → parse CSV → build/load column→param
11
+ // MAPPING (each row fills the template with its OWN values) → validate
12
+ // (V-1..V-13) → broadcast-safety classify (opted-out/archived/blocked are
13
+ // skipped) → preview → [--commit] upsert contacts (/cli/contacts-upsert,
14
+ // ≤4000/batch, 3× retry then ABORT) carrying the campaign TAG bc-<campaign>
15
+ // plus each row's values as campaign-scoped ATTRIBUTES
16
+ // (bc_<campaign>_paramN / _button) → ONE python /meta-broadcast call with
17
+ // {{attributes.bc_<campaign>_paramN}} tokens, which python resolves PER
18
+ // CONTACT (broadcast.py substitute_contact_fields — body AND URL-button
19
+ // params). The send is fire-and-forget: python owns pacing + the broadcast
20
+ // record; audit via `bc status <org> <broadcastId>` / `bc retry`.
21
+ //
22
+ // The OLD per-row Node loop (write-ahead status log, `resume`) survives ONLY
23
+ // to finish campaigns started before v0.6.0 — a fresh CSV send never uses it.
24
+ // A python campaign records ONE {type:"handoff"} line instead; re-running it
25
+ // needs --resend (the tag fan-out cannot skip already-reached rows).
20
26
  //
21
27
  // #132000 structural guard: the payload builder whitelists /^param\d+$/ keys
22
28
  // only — a stray "name" key (which would add a body param on a POSITIONAL
@@ -308,16 +314,18 @@ async function saveCampaign(cfg) {
308
314
  async function loadStatusLog(campaign) {
309
315
  const map = new Map();
310
316
  let header = null;
317
+ let handoff = null; // last {type:"handoff"} line — a completed python fan-off
311
318
  try {
312
319
  const text = await fs.readFile(logPath(campaign), "utf8");
313
320
  for (const line of text.split("\n")) {
314
321
  if (!line.trim()) continue;
315
322
  let obj; try { obj = JSON.parse(line); } catch { continue; }
316
323
  if (obj.type === "meta") { header = obj; continue; }
324
+ if (obj.type === "handoff") { handoff = obj; continue; }
317
325
  if (obj.type === "row" && obj.number) map.set(obj.number, obj);
318
326
  }
319
327
  } catch { /* no log yet */ }
320
- return { header, map };
328
+ return { header, map, handoff };
321
329
  }
322
330
 
323
331
  async function appendLog(campaign, obj) {
@@ -602,11 +610,54 @@ function renderPreview(template, sample, headerMedia) {
602
610
  }
603
611
  }
604
612
 
605
- async function upsertContacts(orgId, rows, batchCap) {
613
+ // ---------------------------------------------------------------------------
614
+ // CSV → python fan-out builders (v0.6.0). Exported for the test harness.
615
+ // ---------------------------------------------------------------------------
616
+
617
+ /** The campaign's contact tag — what python resolves the audience from. */
618
+ export const csvCampaignTag = (campaign) => `bc-${campaign}`;
619
+
620
+ /** Campaign-scoped attribute key for one template slot ("param1".."paramN" | "button"). */
621
+ export const csvAttrKey = (campaign, slot) => `bc_${campaign}_${slot}`;
622
+
623
+ /** workSet rows → /cli/contacts-upsert contact rows: tag + per-row values as
624
+ * attributes. full_name only when the CSV actually carries a non-empty name
625
+ * (the RPC COALESCEs, so an omitted name can never null a real one). */
626
+ export function buildCsvContactRows(workSet, campaign) {
627
+ const tag = csvCampaignTag(campaign);
628
+ return workSet.map((r) => {
629
+ const attributes = {};
630
+ for (const [k, v] of Object.entries(r.values)) {
631
+ // Same structural guard as the old per-row payload builder (V-12).
632
+ if (!/^param\d+$/.test(k)) throw new Error(`internal: stray value key "${k}" — refusing (V-12)`);
633
+ attributes[csvAttrKey(campaign, k)] = v;
634
+ }
635
+ if (r.buttonValue != null) attributes[csvAttrKey(campaign, "button")] = r.buttonValue;
636
+ return {
637
+ whatsapp_id: r.number,
638
+ ...(typeof r.contactName === "string" && r.contactName.trim() ? { full_name: r.contactName.trim() } : {}),
639
+ tags: [tag],
640
+ attributes,
641
+ };
642
+ });
643
+ }
644
+
645
+ /** The ONE shared param set handed to python — every value is an
646
+ * {{attributes.bc_<campaign>_slot}} token python resolves per contact. */
647
+ export function buildCsvTokenParams(template, campaign) {
648
+ const body = {};
649
+ for (const pos of template.body_positions) {
650
+ body[`param${pos}`] = `{{attributes.${csvAttrKey(campaign, `param${pos}`)}}}`;
651
+ }
652
+ const button = template.url_button?.present ? `{{attributes.${csvAttrKey(campaign, "button")}}}` : null;
653
+ return { body, button };
654
+ }
655
+
656
+ async function upsertContacts(orgId, contactRows, batchCap) {
606
657
  let created = 0, updated = 0, skippedN = 0, dropped = 0;
607
- const uniq = rows;
658
+ const uniq = contactRows;
608
659
  for (let i = 0; i < uniq.length; i += batchCap) {
609
- const chunk = uniq.slice(i, i + batchCap).map((r) => ({ whatsapp_id: r.number, full_name: r.contactName }));
660
+ const chunk = uniq.slice(i, i + batchCap);
610
661
  let resp = null, lastErr = null;
611
662
  for (let attempt = 1; attempt <= 3; attempt++) {
612
663
  try { resp = await http.post("contacts-upsert", { organization_id: orgId, contacts: chunk }); break; }
@@ -749,9 +800,16 @@ async function runPipeline(orgId, opts, { commitStage, isResume }) {
749
800
  let sendable = await applySafetyExclusions(orgId, valid, skipped);
750
801
  if (opts.limit) sendable = sendable.slice(0, Number(opts.limit));
751
802
 
752
- // resume folding
753
- const { header: logHeader, map: statusMap } = await loadStatusLog(campaign);
803
+ // resume folding (pre-v0.6.0 per-row campaigns only — python campaigns have no per-row log)
804
+ const { header: logHeader, map: statusMap, handoff } = await loadStatusLog(campaign);
754
805
  if (isResume) {
806
+ if (handoff) {
807
+ console.error(`Campaign "${campaign}" already fanned out via python on ${handoff.at}${handoff.broadcast_id ? ` (broadcastId ${handoff.broadcast_id})` : ""}.`);
808
+ console.error("There is no per-row resume on the python engine — python owns the send. Audit / re-fire failures with:");
809
+ console.error(` flowiq bc status ${orgId} ${handoff.broadcast_id || "<broadcast_id>"} [--failures]`);
810
+ console.error(` flowiq bc retry ${orgId} ${handoff.broadcast_id || "<broadcast_id>"} --commit`);
811
+ process.exit(1);
812
+ }
755
813
  if (!logHeader) { console.error(`No status log for campaign "${campaign}" — nothing to resume.`); process.exit(1); }
756
814
  if (logHeader.csv_fingerprint && logHeader.csv_fingerprint !== csv.header_fingerprint) {
757
815
  console.error("CSV headers changed since this campaign started — start a NEW campaign instead of resuming.");
@@ -778,37 +836,142 @@ async function runPipeline(orgId, opts, { commitStage, isResume }) {
778
836
  console.log(`Summary: ${sendable.length} sendable · skipped ${skipped.length} (${Object.entries(skippedByReason).map(([k, v]) => `${k}: ${v}`).join(", ") || "none"})`);
779
837
  if (statusMap.size) console.log(`Status log: ${[...statusMap.values()].filter((s) => s.status === "sent").length} already sent · work set ${workSet.length}`);
780
838
  if (ambiguous.length) console.log(`⚠ ${ambiguous.length} number(s) have an AMBIGUOUS in-flight status (crash mid-send?) — never auto-resent: ${ambiguous.slice(0, 5).join(", ")}${ambiguous.length > 5 ? "…" : ""}. Check the chat, then edit the status log if they must be retried.`);
781
- console.log(`Upsert plan: ${workSet.length} contacts in ${Math.ceil(workSet.length / CONTACT_BATCH_CAP)} batch(es)`);
782
- const rate = Math.min(Number(opts.rate ?? 8) || 8, HARD_RATE_CAP);
783
- console.log(`Send plan: ${workSet.length} messages @ ≤${rate}/s ≈ ${Math.ceil(workSet.length / rate)}s`);
839
+ // ── OLD-STYLE RESUME (pre-v0.6.0 per-row campaign): finish it on the Node loop ──
840
+ if (isResume) {
841
+ const rate = Math.min(Number(opts.rate ?? 8) || 8, HARD_RATE_CAP);
842
+ console.log(`Send plan: ${workSet.length} messages @ ≤${rate}/s (per-row Node loop — pre-v0.6.0 campaign)`);
843
+ if (!commitStage) {
844
+ console.log("");
845
+ console.log("DRY RUN — nothing was sent, no contacts written. Add --commit to send.");
846
+ return;
847
+ }
848
+ if (!workSet.length) { console.log("Nothing to send — all rows are already sent/skipped."); return; }
849
+ if (!opts.yes) {
850
+ console.log("");
851
+ const answer = await ask(`Type the campaign name ("${campaign}") to send ${workSet.length} message(s): `);
852
+ if (answer !== campaign) { console.log("Mismatch — aborted, nothing sent."); process.exit(0); }
853
+ }
854
+ await executeSendLoop({
855
+ orgId, campaign, templateName, workSet, statusMap, rate, headerMedia,
856
+ skippedCount: skipped.length,
857
+ writeMetaHeader: !logHeader,
858
+ metaExtras: { csv_fingerprint: csv.header_fingerprint, total_rows: csv.rows.length, valid_rows: sendable.length },
859
+ });
860
+ return;
861
+ }
862
+
863
+ // ── FRESH SEND: python server-side fan-out (v0.6.0) ────────────────────────
864
+ // Per-row values ride as campaign-scoped contact attributes; ONE python
865
+ // /meta-broadcast call resolves {{attributes.bc_<campaign>_paramN}} per
866
+ // contact and paces the send server-side. No laptop in the loop.
867
+ const tag = csvCampaignTag(campaign);
868
+ const tokens = buildCsvTokenParams(template, campaign);
869
+ const batchCap = Math.min(Number(opts.upsertBatch ?? CONTACT_BATCH_CAP) || CONTACT_BATCH_CAP, CONTACT_BATCH_CAP);
870
+ if (opts.skipUpsert) console.log("⚠ --skip-upsert is ignored on the python CSV path — the upsert IS how per-row values reach python.");
871
+ console.log(`Engine: PYTHON server-side fan-out (yapi.store/meta-broadcast) — fire-and-forget, python-paced`);
872
+ console.log(`Tag: "${tag}" · per-row values → attributes bc_${campaign}_param1..${template.body_var_count}${tokens.button ? " + _button" : ""}`);
873
+ console.log(`Plan: upsert+tag ${workSet.length} contact(s) in ${Math.ceil(Math.max(1, workSet.length) / batchCap)} batch(es) → one python broadcast`);
874
+
875
+ // A prior fan-off means the tag already reached its audience — re-running
876
+ // resends to EVERY tagged contact (python cannot skip per row).
877
+ if (handoff && !opts.resend) {
878
+ console.log("");
879
+ console.log(`⚠ Campaign "${campaign}" ALREADY fanned out via python on ${handoff.at}${handoff.broadcast_id ? ` (broadcastId ${handoff.broadcast_id})` : ""}.`);
880
+ if (!commitStage) {
881
+ console.log(" A --commit re-run would resend to EVERY tagged contact (no per-row skip). Audit with bc status; re-fire failures with bc retry; pass --resend to deliberately resend to all.");
882
+ } else {
883
+ console.error(" Refusing to resend to the whole tag. Audit / re-fire failures with:");
884
+ console.error(` flowiq bc status ${orgId} ${handoff.broadcast_id || "<broadcast_id>"} [--failures]`);
885
+ console.error(` flowiq bc retry ${orgId} ${handoff.broadcast_id || "<broadcast_id>"} --commit`);
886
+ console.error(" To deliberately resend to EVERYONE tagged, pass --resend.");
887
+ process.exit(1);
888
+ }
889
+ }
890
+ // A pre-v0.6.0 per-row log means part of the audience was already reached
891
+ // row-by-row — the tag fan-out cannot skip them, so this campaign must be
892
+ // finished on the old engine (bc resume) or restarted under a fresh slug.
893
+ if (statusMap.size > 0) {
894
+ const sentAlready = [...statusMap.values()].filter((s) => s.status === "sent").length;
895
+ if (!commitStage) {
896
+ console.log("");
897
+ console.log(`⚠ This campaign has a pre-v0.6.0 per-row status log (${sentAlready} already sent). A --commit here would be refused — finish it with \`flowiq bc resume\`, or start a fresh --campaign.`);
898
+ } else {
899
+ console.error("");
900
+ console.error(`Refusing: campaign "${campaign}" has a pre-v0.6.0 per-row status log (${sentAlready} row(s) already sent).`);
901
+ console.error("The python fan-out sends to the WHOLE tag and cannot skip already-sent rows. Either:");
902
+ console.error(` flowiq bc resume ${orgId} --campaign ${campaign} --commit # finish on the old per-row engine`);
903
+ console.error(" …or re-run under a NEW --campaign slug (fresh tag, everyone in the CSV gets it).");
904
+ process.exit(1);
905
+ }
906
+ }
784
907
 
785
908
  if (!commitStage) {
786
909
  console.log("");
787
- console.log("DRY RUN — nothing was sent, no contacts written. Add --commit to send.");
910
+ console.log("DRY RUN — nothing was sent, no contacts written or tagged. Add --commit to send.");
788
911
  return;
789
912
  }
790
- if (!workSet.length) { console.log("Nothing to send — all rows are already sent/skipped."); return; }
913
+ if (!workSet.length) { console.log("Nothing to send — all rows were skipped."); return; }
791
914
 
792
- // 8. gate
915
+ // 8. gate (BEFORE any write — the upsert itself tags real contacts)
793
916
  if (!opts.yes) {
794
917
  console.log("");
795
- const answer = await ask(`Type the campaign name ("${campaign}") to send ${workSet.length} message(s): `);
796
- if (answer !== campaign) { console.log("Mismatch — aborted, nothing sent."); process.exit(0); }
918
+ const answer = await ask(`Type the campaign name ("${campaign}") to tag ${workSet.length} contact(s) and fire ONE python broadcast: `);
919
+ if (answer !== campaign) { console.log("Mismatch — aborted, nothing written, nothing sent."); process.exit(0); }
797
920
  }
798
921
 
799
- // 9. upsert
800
- if (!opts.skipUpsert && !(isResume && logHeader)) {
801
- try { await upsertContacts(orgId, workSet, Math.min(Number(opts.upsertBatch ?? CONTACT_BATCH_CAP) || CONTACT_BATCH_CAP, CONTACT_BATCH_CAP)); }
802
- catch (e) { console.error(e.message); process.exit(1); }
922
+ // 9. upsert: tag + per-row attributes (the fan-out's data plane)
923
+ try { await upsertContacts(orgId, buildCsvContactRows(workSet, campaign), batchCap); }
924
+ catch (e) { console.error(e.message); process.exit(1); }
925
+
926
+ // 10. python dry-run cross-check: what does the tag ACTUALLY resolve to?
927
+ const reqBody = (dryRun) => ({
928
+ action: "send-python", organization_id: orgId, tag, template_name: templateName,
929
+ body_parameters: tokens.body,
930
+ ...(tokens.button ? { button_parameters: { param1: tokens.button } } : {}),
931
+ ...(headerMedia ? { header_media: headerMedia } : {}),
932
+ dry_run: dryRun,
933
+ });
934
+ let pyTotal = null;
935
+ try {
936
+ const dry = await http.post("broadcast", reqBody(true));
937
+ pyTotal = dry.total_contacts_found ?? null;
938
+ } catch (e) {
939
+ console.error(`Python dry-run failed after tagging: ${e.message}${e.body?.error ? ` — ${e.body.error}` : ""}`);
940
+ console.error(`Nothing sent. The ${workSet.length} contact(s) ARE tagged "${tag}" — re-run to retry the fan-off.`);
941
+ process.exit(1);
942
+ }
943
+ if (pyTotal != null && pyTotal !== workSet.length) {
944
+ if (pyTotal > workSet.length) {
945
+ console.log(`⚠ Tag "${tag}" resolves to ${pyTotal} contact(s) — ${pyTotal - workSet.length} MORE than this CSV (tagged by an earlier run of this campaign?). They will ALSO receive the send.`);
946
+ if (!opts.yes) {
947
+ const extra = await ask(`Type YES to send to all ${pyTotal}: `);
948
+ if (extra !== "YES") { console.log(`Aborted — nothing sent. The tag still carries ${pyTotal} contact(s); use a fresh --campaign for a clean audience.`); process.exit(0); }
949
+ }
950
+ } else {
951
+ console.log(`Note: python resolves ${pyTotal} of ${workSet.length} tagged (the rest are opted-out/blocked server-side).`);
952
+ }
803
953
  }
804
954
 
805
- // 10-11. send loop + report (shared with the --tag pipeline)
806
- await executeSendLoop({
807
- orgId, campaign, templateName, workSet, statusMap, rate, headerMedia,
808
- skippedCount: skipped.length,
809
- writeMetaHeader: !logHeader,
810
- metaExtras: { csv_fingerprint: csv.header_fingerprint, total_rows: csv.rows.length, valid_rows: sendable.length },
955
+ // 11. fire — ONE call; python paces + tracks the rest
956
+ let out;
957
+ try { out = await http.post("broadcast", reqBody(false)); }
958
+ catch (e) { console.error(`Python broadcast failed: ${e.message}${e.body?.error ? ` — ${e.body.error}` : ""}`); process.exit(1); }
959
+ if (!logHeader) {
960
+ await appendLog(campaign, {
961
+ type: "meta", campaign, organization_id: orgId, template_name: templateName, engine: "python",
962
+ csv_fingerprint: csv.header_fingerprint, total_rows: csv.rows.length, valid_rows: sendable.length,
963
+ started_at: new Date().toISOString(),
964
+ });
965
+ }
966
+ await appendLog(campaign, {
967
+ type: "handoff", engine: "python", tag, broadcast_id: out.broadcastId ?? out.broadcast_id ?? null,
968
+ tagged: workSet.length, python_total: pyTotal, at: new Date().toISOString(),
811
969
  });
970
+ console.log("");
971
+ console.log(`✅ ${out.message || "Broadcast started"}${out.broadcastId ? ` · broadcastId ${out.broadcastId}` : ""}`);
972
+ console.log(` Python is sending in the background to tag "${tag}" (${pyTotal ?? workSet.length} contact(s)) and tracking it under that broadcast id.`);
973
+ console.log(` Delivery: flowiq bc status ${orgId} ${out.broadcastId || "<broadcast_id>"} [--failures]`);
974
+ console.log(` Failures: flowiq bc retry ${orgId} ${out.broadcastId || "<broadcast_id>"} --commit`);
812
975
  }
813
976
 
814
977
  /** The paced, write-ahead-logged per-row send loop + final report. */
@@ -0,0 +1,150 @@
1
+ // `flowiq hours log <org> --minutes N|--hours H --desc "…"` / `list <org>` /
2
+ // `summary`. The client-hours ledger: every org's package includes 10 hours of
3
+ // Flowapt work per calendar month, and every piece of client work gets logged
4
+ // against it (manually here, or automatically by Claude via the FlowIQ MCP).
5
+ // All writes happen server-side (/cli/hours) with the staff token.
6
+
7
+ import { http } from "../http.js";
8
+
9
+ const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
10
+ const MONTH_RE = /^\d{4}-(0[1-9]|1[0-2])$/;
11
+
12
+ function fmtHours(minutes) {
13
+ const h = minutes / 60;
14
+ return `${Number.isInteger(h) ? h : h.toFixed(1)}h`;
15
+ }
16
+
17
+ function requireOrg(orgId) {
18
+ if (!UUID_RE.test(orgId || "")) {
19
+ console.error(`Error: "${orgId}" is not a valid organization UUID (find it with \`flowiq org list <search>\`).`);
20
+ process.exit(1);
21
+ }
22
+ }
23
+
24
+ function requireMonth(month) {
25
+ if (month && !MONTH_RE.test(month)) {
26
+ console.error(`Error: --month must be YYYY-MM (got "${month}").`);
27
+ process.exit(1);
28
+ }
29
+ }
30
+
31
+ /** --minutes and --hours are alternatives; exactly one must yield an integer minute count. */
32
+ function resolveMinutes(opts) {
33
+ const hasMinutes = opts.minutes !== undefined;
34
+ const hasHours = opts.hours !== undefined;
35
+ if (hasMinutes === hasHours) {
36
+ console.error("Error: pass exactly one of --minutes <n> or --hours <h> (e.g. --hours 1.5).");
37
+ process.exit(1);
38
+ }
39
+ let minutes;
40
+ if (hasMinutes) {
41
+ minutes = Number(opts.minutes);
42
+ if (!Number.isInteger(minutes)) {
43
+ console.error(`Error: --minutes must be a whole number (got "${opts.minutes}"). Use --hours for fractions.`);
44
+ process.exit(1);
45
+ }
46
+ } else {
47
+ const h = Number(opts.hours);
48
+ if (!Number.isFinite(h)) {
49
+ console.error(`Error: --hours must be a number (got "${opts.hours}").`);
50
+ process.exit(1);
51
+ }
52
+ minutes = Math.round(h * 60);
53
+ }
54
+ if (minutes < 1 || minutes > 6000) {
55
+ console.error(`Error: the entry must be between 1 minute and 100 hours (got ${minutes} min).`);
56
+ process.exit(1);
57
+ }
58
+ return minutes;
59
+ }
60
+
61
+ export async function log(orgId, opts) {
62
+ requireOrg(orgId);
63
+ const minutes = resolveMinutes(opts);
64
+ const description = (opts.desc || "").trim();
65
+ if (!description) {
66
+ console.error('Error: --desc "what was done" is required.');
67
+ process.exit(1);
68
+ }
69
+ if (opts.date && !/^\d{4}-\d{2}-\d{2}$/.test(opts.date)) {
70
+ console.error(`Error: --date must be YYYY-MM-DD (got "${opts.date}").`);
71
+ process.exit(1);
72
+ }
73
+
74
+ let resp;
75
+ try {
76
+ resp = await http.post("hours", {
77
+ organization_id: orgId,
78
+ description,
79
+ minutes,
80
+ performed_by: opts.by || undefined,
81
+ session: opts.session || undefined,
82
+ // A bare date logs at noon UTC so it can never slip into a neighbouring
83
+ // day (or month) in any sane timezone rendering.
84
+ performed_at: opts.date ? `${opts.date}T12:00:00Z` : undefined,
85
+ });
86
+ } catch (e) {
87
+ console.error(`Log failed: ${e.message}`);
88
+ process.exit(1);
89
+ }
90
+
91
+ const used = resp.month_total_minutes;
92
+ const allowance = resp.allowance_hours * 60;
93
+ console.log(`Logged ${fmtHours(minutes)} for ${resp.organization.name}`);
94
+ console.log(` entry: ${resp.id}`);
95
+ console.log(` month: ${resp.month} — ${fmtHours(used)} of ${resp.allowance_hours}h package used${used > allowance ? ` ⚠ ${fmtHours(used - allowance)} OVER` : ` (${fmtHours(allowance - used)} left)`}`);
96
+ }
97
+
98
+ export async function list(orgId, opts) {
99
+ requireOrg(orgId);
100
+ requireMonth(opts.month);
101
+ let resp;
102
+ try {
103
+ resp = await http.get("hours", { organization_id: orgId, month: opts.month });
104
+ } catch (e) {
105
+ console.error(`List failed: ${e.message}`);
106
+ process.exit(1);
107
+ }
108
+ if (opts.json) {
109
+ console.log(JSON.stringify(resp, null, 2));
110
+ return;
111
+ }
112
+ const over = resp.remaining_minutes < 0;
113
+ console.log(`${resp.organization.name} — ${resp.month}`);
114
+ console.log(` package: ${resp.allowance_hours}h/month · used ${fmtHours(resp.total_minutes)} · ${over ? `⚠ ${fmtHours(-resp.remaining_minutes)} OVER` : `${fmtHours(resp.remaining_minutes)} left`}`);
115
+ if (!resp.entries.length) {
116
+ console.log(" (no entries this month)");
117
+ return;
118
+ }
119
+ console.log("");
120
+ for (const e of resp.entries) {
121
+ const when = String(e.performed_at).slice(0, 10);
122
+ const src = e.source === "manual" ? "ui" : e.source;
123
+ console.log(` ${when} ${fmtHours(e.minutes).padStart(6)} [${src}] ${e.performed_by} — ${e.description}`);
124
+ }
125
+ }
126
+
127
+ export async function summary(opts) {
128
+ requireMonth(opts.month);
129
+ let resp;
130
+ try {
131
+ resp = await http.get("hours", { summary: "1", month: opts.month });
132
+ } catch (e) {
133
+ console.error(`Summary failed: ${e.message}`);
134
+ process.exit(1);
135
+ }
136
+ if (opts.json) {
137
+ console.log(JSON.stringify(resp, null, 2));
138
+ return;
139
+ }
140
+ if (!resp.organizations.length) {
141
+ console.log(`(no hours logged for any org in ${resp.month})`);
142
+ return;
143
+ }
144
+ const allowance = resp.allowance_hours * 60;
145
+ console.log(`Client hours — ${resp.month} (package: ${resp.allowance_hours}h/month per org)\n`);
146
+ for (const o of resp.organizations) {
147
+ const flag = o.total_minutes > allowance ? ` ⚠ ${fmtHours(o.total_minutes - allowance)} OVER` : "";
148
+ console.log(` ${fmtHours(o.total_minutes).padStart(6)} (${String(o.entries).padStart(2)} entr${o.entries === 1 ? "y" : "ies"}) ${o.name || o.id}${flag}`);
149
+ }
150
+ }
@@ -97,8 +97,15 @@ export async function push(identifier, opts = {}) {
97
97
  try {
98
98
  resp = await http.post("messaging-webhooks", {
99
99
  organization_id: data.organization_id,
100
+ // auth_type / auth_config / headers / secret ride along deliberately:
101
+ // push is FULL-REPLACE (delete-all then insert), so dropping them here
102
+ // would silently strip every webhook's credentials on the next push.
100
103
  messaging_webhooks: data.messaging_webhooks.map((w) => ({
101
104
  url: w.url, type: w.type, platform: w.platform,
105
+ auth_type: w.auth_type ?? "none",
106
+ auth_config: w.auth_config ?? null,
107
+ headers: w.headers ?? null,
108
+ secret: w.secret ?? null,
102
109
  })),
103
110
  dry_run: !!opts.dryRun,
104
111
  });
package/src/index.js CHANGED
@@ -15,6 +15,7 @@ import * as webhooksCmd from "./commands/webhooks.js";
15
15
  import * as flowmodCmd from "./commands/flowmod.js";
16
16
  import * as groupsCmd from "./commands/groups.js";
17
17
  import * as pinboardCmd from "./commands/pinboard.js";
18
+ import * as hoursCmd from "./commands/hours.js";
18
19
  import * as templatesCmd from "./commands/templates.js";
19
20
  import * as orgCmd from "./commands/org.js";
20
21
  import * as agentConfigCmd from "./commands/agent-config.js";
@@ -191,6 +192,29 @@ export function run(argv) {
191
192
  .description("List every task in the DB (optionally filter: open | in_progress | done) to find an id")
192
193
  .action((status) => pinboardCmd.listRemote(status));
193
194
 
195
+ // hours (client-hours ledger — every org's package = 10h Flowapt work/month)
196
+ const hours = program.command("hours")
197
+ .description("Client-hours ledger: log + report Flowapt work against each org's 10h/month package");
198
+ hours.command("log <organization_id>")
199
+ .description("Log work done for a client org (exactly one of --minutes / --hours)")
200
+ .option("--minutes <n>", "duration in whole minutes")
201
+ .option("--hours <h>", "duration in hours (fractions fine, e.g. 1.5)")
202
+ .option("--desc <text>", "what was done, in plain language the client could read")
203
+ .option("--by <name>", "who did the work (default: your staff login email)")
204
+ .option("--session <name>", "session/task name the work happened in")
205
+ .option("--date <YYYY-MM-DD>", "when the work was done (default: now)")
206
+ .action((orgId, opts) => hoursCmd.log(orgId, opts));
207
+ hours.command("list <organization_id>")
208
+ .description("One org's entries + package standing for a month (default: current)")
209
+ .option("--month <YYYY-MM>", "which month to report")
210
+ .option("--json", "raw JSON output")
211
+ .action((orgId, opts) => hoursCmd.list(orgId, opts));
212
+ hours.command("summary")
213
+ .description("Cross-org month totals — every org with logged work, vs the 10h package")
214
+ .option("--month <YYYY-MM>", "which month to report")
215
+ .option("--json", "raw JSON output")
216
+ .action((opts) => hoursCmd.summary(opts));
217
+
194
218
  // templates (read WhatsApp templates from Meta; create via /cli/meta-templates)
195
219
  const templates = program.command("templates")
196
220
  .alias("tpl")
@@ -441,11 +465,12 @@ export function run(argv) {
441
465
  .option("--yes", "skip the type-the-campaign-name confirm gate (CI)")
442
466
  .option("--force-remap", "ignore the saved mapping and rebuild interactively")
443
467
  .option("--illegal-chars <mode>", "reject | strip — newlines/tabs/4+ spaces in values (Meta #100)", "reject")
444
- .option("--rate <n>", "max messages per second (hard cap 10)", "8")
468
+ .option("--rate <n>", "max messages per second — ONLY the pre-v0.6.0 per-row resume path; python paces fresh sends itself", "8")
445
469
  .option("--upsert-batch <n>", "contacts per upsert call (max 4000)", "4000")
446
- .option("--skip-upsert", "assume contacts already exist; skip the upsert stage")
447
- .option("--limit <n>", "only process the first N valid rows (smoke test)")
448
- .option("--resume", "continue from the existing status log instead of starting fresh")
470
+ .option("--skip-upsert", "pre-v0.6.0 resume only — ignored on fresh CSV sends (the upsert IS how per-row values reach python)")
471
+ .option("--limit <n>", "only process the first N valid rows (smoke-test under a THROWAWAY --campaign — a limited python fan-off still records the campaign as fired)")
472
+ .option("--resend", "re-fire a CSV campaign that already fanned out via python — resends to EVERY tagged contact, incl. those already reached (bc retry re-fires failures only)")
473
+ .option("--resume", "continue a pre-v0.6.0 per-row campaign from its status log (python campaigns have no per-row resume — use bc status / bc retry)")
449
474
  .option("--retry-failed", "with --resume: also re-attempt rows previously marked failed")
450
475
  .action((orgId, opts) => broadcastCmd.send(orgId, opts));
451
476
  broadcast.command("resume <organization_id>")