@sellable/mcp 0.1.503 → 0.1.505

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.
@@ -1,7 +1,16 @@
1
+ import { getApi, SellableApiError } from "../api.js";
2
+ import { startPrepareCampaignMessages } from "./campaign-message-preparation.js";
3
+ import { confirmLeadList, importLeads, searchSignals, selectPromisingPosts, } from "./leads.js";
4
+ import { markProviderPromptLoaded } from "./provider-preflight.js";
1
5
  import { getRefillTargetPlan } from "./refill-target-plan.js";
2
6
  import { refreshPaidInmailCredits } from "./senders.js";
3
7
  import { createWorkspaceContext, normalizeExplicitWorkspaceId, } from "./workspace-context.js";
4
8
  const DEFAULT_SCHEDULER_FORWARD_HOURS = 48;
9
+ const PAID_INMAIL_REFRESH_MAX_ATTEMPTS = 3;
10
+ const PAID_INMAIL_REFRESH_RETRY_DELAY_MS = Number(process.env.SELLABLE_MCP_PAID_REFRESH_RETRY_DELAY_MS ?? "1000");
11
+ const SIGNAL_DISCOVERY_MIN_REFILL_POSTS = 3;
12
+ const SIGNAL_DISCOVERY_MAX_REFILL_POSTS = 10;
13
+ const SIGNAL_DISCOVERY_TARGET_ROWS_PER_POST = 150;
5
14
  function normalizeStrings(values) {
6
15
  if (!Array.isArray(values))
7
16
  return [];
@@ -11,6 +20,11 @@ function normalizeStrings(values) {
11
20
  .filter(Boolean)),
12
21
  ];
13
22
  }
23
+ function sleep(ms) {
24
+ if (!Number.isFinite(ms) || ms <= 0)
25
+ return Promise.resolve();
26
+ return new Promise((resolve) => setTimeout(resolve, ms));
27
+ }
14
28
  function normalizeHorizonSendDays(value) {
15
29
  if (value === undefined || value === null)
16
30
  return null;
@@ -56,10 +70,24 @@ function normalizeTargetDate(value) {
56
70
  }
57
71
  return trimmed;
58
72
  }
73
+ function userAddedRowsLimitPayloadFromError(error) {
74
+ if (!(error instanceof SellableApiError) || error.status !== 400) {
75
+ return null;
76
+ }
77
+ try {
78
+ const parsed = JSON.parse(error.body);
79
+ if (parsed?.code !== "USER_ADDED_ROWS_LIMIT_EXCEEDED")
80
+ return null;
81
+ return parsed;
82
+ }
83
+ catch {
84
+ return null;
85
+ }
86
+ }
59
87
  export const refillSendsToolDefinitions = [
60
88
  {
61
89
  name: "refill_sends",
62
- description: "Typed command entrypoint for Sellable refill sends. Accepts --yolo semantics and optional sender selectors, then returns the bounded execution contract for the skill-led refill workflow. In --yolo, it may refresh stale paid InMail credit cache facts for exact selected senders once, then rerun and return the post-refresh target plan. This tool does not mutate campaigns, import leads, approve messages, schedule sends, launch, archive, delete, or write scheduler rows.",
90
+ description: "Typed command entrypoint for Sellable refill sends. Accepts --yolo semantics and optional sender selectors, then returns the bounded execution contract for the skill-led refill workflow. In --yolo, it may refresh stale paid InMail credit cache facts once, or execute exactly one safe primitive from target.globalActionQueue[0] (bounded existing-row preparation, bounded same-source copy, bounded generated-message approval, or read-only wait) and reread. It does not run unbounded approval, lower paid InMail thresholds, switch source families, create campaigns, schedule sends, launch, archive, delete, or write scheduler rows.",
63
91
  inputSchema: {
64
92
  type: "object",
65
93
  properties: {
@@ -202,9 +230,7 @@ export function refillSendsCommand(input = {}) {
202
230
  firstOperationalSteps: [
203
231
  'Load get_subskill_prompt({ subskillName: "refill-sends-workflow" }) before any product operation.',
204
232
  "A skill cannot create or invoke /goal by itself. If this refill is already running inside an active Codex goal, keep that goal open until every selected sender lane is horizon-filled by projected coverage (sent + scheduled), Christian explicitly stops/statuses the run, or a concrete non-scheduler blocker appears.",
205
- `Call get_refill_target_plan({ intent: "${intent}"${workspaceId ? `, workspaceId: "${workspaceId}"` : ""}${input.campaignId ? `, campaignId: "${input.campaignId}"` : ""}${input.tableId ? `, tableId: "${input.tableId}"` : ""}${senderIds.length > 0
206
- ? `, senderIds: ${JSON.stringify(senderIds)}`
207
- : ""}${senderNames.length > 0
233
+ `Call get_refill_target_plan({ intent: "${intent}"${workspaceId ? `, workspaceId: "${workspaceId}"` : ""}${input.campaignId ? `, campaignId: "${input.campaignId}"` : ""}${input.tableId ? `, tableId: "${input.tableId}"` : ""}${senderIds.length > 0 ? `, senderIds: ${JSON.stringify(senderIds)}` : ""}${senderNames.length > 0
208
234
  ? `, senderNames: ${JSON.stringify(senderNames)}`
209
235
  : ""}${senders.length > 0 ? `, senders: ${JSON.stringify(senders)}` : ""}${targetDate
210
236
  ? `, targetDate: "${targetDate}"`
@@ -234,14 +260,14 @@ export function refillSendsCommand(input = {}) {
234
260
  "Treat current dashboard-active PAUSED campaign-backed sequence campaigns as start-eligible candidates: read refill state before deciding whether to prep, approve, start, or skip.",
235
261
  "Fresh reread get_campaign_refill_state immediately before any import, prep, approval, or horizon-fill mutation.",
236
262
  "Maintain a target-window saturation ledger per selected sender from get_refill_target_plan: selected days, gross capacity, actual sent counts, future scheduler-owned scheduled counts, projected counts, ready-to-schedule buffer, remaining projected gap, paid InMail threshold feasibility, targetShapeRevision, stateRevision, and next MCP primitive.",
237
- "In --yolo, this tool automatically maintains a run-local refreshedPaidInmailSenderIds set: when the first target plan returns refresh_paid_inmail_credits candidates for selected paid-InMail lanes, it refreshes each exact sender at most once, reruns get_refill_target_plan, and returns the post-refresh targetPlan before any prep/import/approval/start action is chosen.",
263
+ "In --yolo, this tool automatically maintains a run-local refreshedPaidInmailSenderIds set: when the first target plan returns refresh_paid_inmail_credits candidates for selected paid-InMail lanes, it refreshes each exact sender at most once, reruns get_refill_target_plan, and returns the post-refresh targetPlan before any prep/source-copy/bounded-approval/read-only wait action is chosen.",
238
264
  "Do not present paid-credit refresh as the next operator action after refill_sends returns autoPaidInmailRefresh and the post-refresh targetPlan.",
239
265
  "If paid InMail feasibility remains below threshold after that automatic refresh, report the exact sender/campaign/table/column threshold action or same-campaign connection fallback; --yolo must not lower paid InMail thresholds or create campaigns.",
240
266
  "In --yolo or after one Accept, continue through every safe selected sender/campaign action covered by the rendered target packet; sent/scheduled-count progress changes stateRevision and should continue while targetShapeRevision is stable.",
241
267
  "Do not complete a fill/schedule request until a final get_refill_target_plan or refill-state reread proves projected coverage (sent + scheduled) fills the scheduler-forward target window. Treat awaiting_scheduler_after_ready_buffer as loaded, awaiting scheduler when ready buffer covers the gap; do not import/prep more rows in that state, and keep polling unless Christian stops/statuses the run or a concrete non-scheduler blocker such as paid_inmail_below_threshold appears.",
242
268
  ],
243
269
  approvalContract: yolo
244
- ? "Auto-accept only the rendered bounded refill target packet after get_refill_target_plan and fresh reread. Execute one globally ranked primitive from target.globalActionQueue at a time, then rerun get_refill_target_plan before taking another action. Continue through every safe apply/prep/start action inside that packet, including start_campaign only for exact selected PAUSED campaign-backed sequence refill targets named in the packet. After each action, rerun get_refill_target_plan; keep going while targetShapeRevision is stable and projected coverage (sent + scheduled) progresses toward the target, even though stateRevision changes. Ready-to-schedule rows are buffer, not completion. If ready buffer covers the projected gap, use only persistent read-only scheduler wait/reread and keep the run open until projected coverage fills or Christian stops/statuses it. Stop immediately if targetShapeRevision changes because sender set, route, ids, caps, dates, blockers, action class, paid InMail threshold feasibility, or side-effect class drifts. --yolo does not authorize lowering paid InMail thresholds or creating connection campaigns."
270
+ ? "Auto-accept only the rendered bounded refill target packet after get_refill_target_plan and fresh reread. Execute one globally ranked primitive from target.globalActionQueue at a time, then rerun get_refill_target_plan before taking another action. Continue through every safe prep/source-copy/bounded-approval/read-only wait action inside that packet. Unbounded approval, start_campaign, source-family switches, threshold changes, and campaign creation require their own explicit gates. After each safe action, rerun get_refill_target_plan; keep going while targetShapeRevision is stable and projected coverage (sent + scheduled) progresses toward the target, even though stateRevision changes. Ready-to-schedule rows are buffer, not completion. If ready buffer covers the projected gap, use only persistent read-only scheduler wait/reread and keep the run open until projected coverage fills or Christian stops/statuses it. Stop immediately if targetShapeRevision changes because sender set, route, ids, caps, dates, blockers, action class, paid InMail threshold feasibility, or side-effect class drifts. --yolo does not authorize lowering paid InMail thresholds or creating connection campaigns."
245
271
  : hasSenderSelectors
246
272
  ? "Before mutation, post the full bounded refill packet in normal chat as Markdown, including workspace, sender scope, campaign table, exact ids, caps/dates, gross target, sent count, scheduled count, projected count, ready buffer, remaining projected gap, paid InMail threshold feasibility, targetShapeRevision/stateRevision, side effects, forbidden actions, and stop condition. Then ask the final Accept/Decline structured approval question with a compact body that refers back to the posted packet instead of duplicating it. Only ask when get_refill_target_plan reports a positive remaining projected/ready gap. If target is complete by projected coverage, no-op without approval. If ready buffer covers the projected gap, run only persistent read-only scheduler wait/reread and keep the run open until projected coverage fills or Christian stops/statuses it. Threshold changes and campaign creation need separate explicit approval."
247
273
  : "Ask which eligible enrolled senders to refill first, then, before mutation, post the full bounded refill packet in normal chat as Markdown and ask the final Accept/Decline structured approval question with a compact body that refers back to the posted packet.",
@@ -326,6 +352,45 @@ function recordValue(value) {
326
352
  function stringValue(value) {
327
353
  return typeof value === "string" && value.trim() ? value.trim() : null;
328
354
  }
355
+ function numberValue(value) {
356
+ return typeof value === "number" && Number.isFinite(value) ? value : null;
357
+ }
358
+ function stringArray(value) {
359
+ if (!Array.isArray(value))
360
+ return [];
361
+ return value.filter((item) => typeof item === "string");
362
+ }
363
+ function prepareRowSelectorValue(value) {
364
+ const selector = recordValue(value);
365
+ const type = stringValue(selector?.type);
366
+ if (type !== "needsEnrichment" &&
367
+ type !== "needsApproval" &&
368
+ type !== "needsGeneratedMessage" &&
369
+ type !== "reviewBatch" &&
370
+ type !== "staleGeneratedMessages") {
371
+ return undefined;
372
+ }
373
+ const limit = numberValue(selector?.limit);
374
+ return {
375
+ type,
376
+ ...(limit && limit > 0 ? { limit: Math.floor(limit) } : {}),
377
+ };
378
+ }
379
+ function uniqueStrings(values) {
380
+ const seen = new Set();
381
+ const result = [];
382
+ for (const value of values) {
383
+ const normalized = typeof value === "string" ? value.trim() : "";
384
+ if (!normalized)
385
+ continue;
386
+ const key = normalized.toLowerCase();
387
+ if (seen.has(key))
388
+ continue;
389
+ seen.add(key);
390
+ result.push(normalized);
391
+ }
392
+ return result;
393
+ }
329
394
  function paidRefreshActionFrom(value) {
330
395
  const action = recordValue(value);
331
396
  if (!action || action.type !== "refresh_paid_inmail_credits")
@@ -373,6 +438,493 @@ function collectPaidInmailRefreshActions(plan) {
373
438
  add(action);
374
439
  return [...bySender.values()];
375
440
  }
441
+ function firstGlobalAction(plan) {
442
+ const root = recordValue(plan);
443
+ const target = recordValue(root?.target);
444
+ const globalActionQueue = Array.isArray(target?.globalActionQueue)
445
+ ? target.globalActionQueue
446
+ : [];
447
+ const [first] = globalActionQueue;
448
+ return recordValue(first);
449
+ }
450
+ function actionIds(action) {
451
+ return recordValue(action.ids) ?? {};
452
+ }
453
+ function actionToolInput(action) {
454
+ return recordValue(action.toolInput) ?? {};
455
+ }
456
+ function actionCampaignId(action) {
457
+ const ids = actionIds(action);
458
+ const toolInput = actionToolInput(action);
459
+ return (stringValue(toolInput.campaignId) ??
460
+ stringValue(toolInput.campaignOfferId) ??
461
+ stringValue(ids.campaignId) ??
462
+ stringValue(action.campaignId));
463
+ }
464
+ function actionTableId(action) {
465
+ const ids = actionIds(action);
466
+ const toolInput = actionToolInput(action);
467
+ return (stringValue(toolInput.tableId) ??
468
+ stringValue(ids.tableId) ??
469
+ stringValue(action.tableId));
470
+ }
471
+ function actionSourceLeadListId(action) {
472
+ const ids = actionIds(action);
473
+ const toolInput = actionToolInput(action);
474
+ return (stringValue(toolInput.sourceLeadListId) ??
475
+ stringValue(ids.sourceLeadListId) ??
476
+ stringValue(action.sourceLeadListId));
477
+ }
478
+ function actionSenderId(action) {
479
+ const ids = actionIds(action);
480
+ const toolInput = actionToolInput(action);
481
+ return (stringValue(toolInput.senderId) ??
482
+ stringValue(ids.senderId) ??
483
+ stringValue(action.senderId));
484
+ }
485
+ function actionActionType(action) {
486
+ const ids = actionIds(action);
487
+ const toolInput = actionToolInput(action);
488
+ return (stringValue(toolInput.actionType) ??
489
+ stringValue(toolInput.selectedLane) ??
490
+ stringValue(ids.actionType) ??
491
+ stringValue(action.actionType) ??
492
+ stringValue(action.selectedLane));
493
+ }
494
+ function actionSourceFingerprint(action) {
495
+ const ids = actionIds(action);
496
+ const toolInput = actionToolInput(action);
497
+ return (stringValue(toolInput.sourceFingerprint) ??
498
+ stringValue(ids.sourceFingerprint) ??
499
+ stringValue(action.sourceFingerprint));
500
+ }
501
+ function actionLeadSourceProvider(action) {
502
+ const ids = actionIds(action);
503
+ const toolInput = actionToolInput(action);
504
+ return (stringValue(toolInput.leadSourceProvider) ??
505
+ stringValue(toolInput.provider) ??
506
+ stringValue(ids.leadSourceProvider) ??
507
+ stringValue(action.leadSourceProvider));
508
+ }
509
+ function normalizedSignalKeyword(value) {
510
+ if (typeof value !== "string")
511
+ return null;
512
+ const keyword = value.trim();
513
+ if (!keyword)
514
+ return null;
515
+ if (/^(https?:\/\/|www\.|linkedin\.com\/|\/?in\/)/i.test(keyword)) {
516
+ return null;
517
+ }
518
+ return keyword;
519
+ }
520
+ function keywordsFromSignalTabs(tabs) {
521
+ const selectedKeywords = [];
522
+ const fallbackKeywords = [];
523
+ for (const tab of tabs) {
524
+ const keyword = normalizedSignalKeyword(tab.keyword);
525
+ if (!keyword)
526
+ continue;
527
+ const selected = (tab.posts ?? []).some((post) => post.isSelected === true);
528
+ if (selected) {
529
+ selectedKeywords.push(keyword);
530
+ }
531
+ else {
532
+ fallbackKeywords.push(keyword);
533
+ }
534
+ }
535
+ return uniqueStrings([...selectedKeywords, ...fallbackKeywords]).slice(0, 5);
536
+ }
537
+ function selectedPostIdsFromSignalTabs(tabs) {
538
+ return uniqueStrings(tabs.flatMap((tab) => (tab.posts ?? [])
539
+ .filter((post) => post.isSelected === true)
540
+ .map((post) => post.id)));
541
+ }
542
+ function postIdsFromSignalSearch(summary, maxPosts, options = {}) {
543
+ const excludedPostIds = new Set((options.excludePostIds ?? []).map((postId) => postId.toLowerCase()));
544
+ const recommendedPostIds = stringArray(summary?.recommendedPostIds);
545
+ const topPostIds = Array.isArray(summary?.topPosts)
546
+ ? summary.topPosts
547
+ .map((post) => post && typeof post === "object"
548
+ ? stringValue(post.id)
549
+ : null)
550
+ .filter((id) => Boolean(id))
551
+ : [];
552
+ return uniqueStrings([...recommendedPostIds, ...topPostIds])
553
+ .filter((postId) => !excludedPostIds.has(postId.toLowerCase()))
554
+ .slice(0, Math.max(1, maxPosts));
555
+ }
556
+ function maxSignalDiscoveryPostsForSourceLimit(sourceRowLimit) {
557
+ return Math.min(SIGNAL_DISCOVERY_MAX_REFILL_POSTS, Math.max(SIGNAL_DISCOVERY_MIN_REFILL_POSTS, Math.ceil(sourceRowLimit / SIGNAL_DISCOVERY_TARGET_ROWS_PER_POST)));
558
+ }
559
+ function refillPrepareRequestHash(params) {
560
+ return [
561
+ "refill_sends",
562
+ "prepare_messages",
563
+ params.campaignId,
564
+ params.tableId ?? "no-table",
565
+ actionSenderId(params.action) ?? "all-senders",
566
+ actionSourceLeadListId(params.action) ?? "no-source-list",
567
+ actionActionType(params.action) ?? "no-action-type",
568
+ params.approvalMode,
569
+ params.rowSelector
570
+ ? `${params.rowSelector.type}:${params.rowSelector.limit ?? "no-limit"}`
571
+ : "no-row-selector",
572
+ ].join(":");
573
+ }
574
+ function boundedApprovalLimit(action) {
575
+ const toolInput = actionToolInput(action);
576
+ const rowSelector = recordValue(toolInput.rowSelector);
577
+ const selectorLimit = numberValue(rowSelector?.limit);
578
+ const inputLimit = numberValue(toolInput.limit);
579
+ const limit = selectorLimit ?? inputLimit;
580
+ if (!limit || limit <= 0)
581
+ return null;
582
+ return Math.floor(limit);
583
+ }
584
+ async function approveGeneratedMessagesBatch(action) {
585
+ const tableId = actionTableId(action);
586
+ const toolInput = actionToolInput(action);
587
+ const columnId = stringValue(toolInput.columnId) ?? stringValue(actionIds(action).columnId);
588
+ const limit = boundedApprovalLimit(action);
589
+ if (!tableId || !limit) {
590
+ return {
591
+ status: "refused",
592
+ refusalReason: "approve_messages action is missing tableId or a bounded rowSelector.limit",
593
+ };
594
+ }
595
+ const api = getApi();
596
+ const result = await api.post("/api/v3/workflow-tables/cells/approve-batch", {
597
+ tableId,
598
+ ...(columnId ? { columnId } : {}),
599
+ limit,
600
+ scope: "generated_unapproved",
601
+ });
602
+ return { status: "executed_and_reread", result };
603
+ }
604
+ async function continueSignalDiscoverySource(action) {
605
+ const campaignOfferId = actionCampaignId(action);
606
+ const sourceLeadListId = actionSourceLeadListId(action);
607
+ const sourceFingerprint = actionSourceFingerprint(action);
608
+ const toolInput = actionToolInput(action);
609
+ const sourceRowLimit = Math.min(1500, Math.max(100, Math.floor(numberValue(toolInput.sourceRowLimit) ??
610
+ numberValue(toolInput.targetRows) ??
611
+ numberValue(action.targetRows) ??
612
+ 100)));
613
+ const maxPostsToScrape = maxSignalDiscoveryPostsForSourceLimit(sourceRowLimit);
614
+ if (!campaignOfferId || !sourceLeadListId || !sourceFingerprint) {
615
+ return {
616
+ status: "refused",
617
+ refusalReason: "continue_signal_discovery_source action is missing campaignOfferId, sourceLeadListId, or sourceFingerprint",
618
+ };
619
+ }
620
+ const requestedProvider = actionLeadSourceProvider(action);
621
+ if (requestedProvider &&
622
+ requestedProvider !== "signal-discovery" &&
623
+ requestedProvider !== "campaign-tracked-post") {
624
+ return {
625
+ status: "refused",
626
+ refusalReason: "continue_signal_discovery_source can only run for Signal Discovery source families",
627
+ };
628
+ }
629
+ const api = getApi();
630
+ const campaign = await api.get(`/api/v2/campaign-offers/${campaignOfferId}`);
631
+ if (campaign.leadSourceProvider !== "signal-discovery" &&
632
+ campaign.leadSourceProvider !== "campaign-tracked-post") {
633
+ return {
634
+ status: "refused",
635
+ refusalReason: "campaign leadSourceProvider is not Signal Discovery; refusing same-source continuation",
636
+ };
637
+ }
638
+ if (campaign.selectedLeadListId &&
639
+ campaign.selectedLeadListId !== sourceLeadListId) {
640
+ return {
641
+ status: "refused",
642
+ refusalReason: "campaign selectedLeadListId changed since the plan packet; rerun get_refill_target_plan before source continuation",
643
+ };
644
+ }
645
+ const sourceMeta = await api.get(`/api/v3/workflow-tables/${sourceLeadListId}?mode=meta`);
646
+ const sourceConfig = sourceMeta.table?.config ?? null;
647
+ const headlineICPCriteria = stringArray(sourceConfig?.headlineICPCriteria).length > 0
648
+ ? stringArray(sourceConfig?.headlineICPCriteria)
649
+ : stringArray(sourceConfig?.rubricGuidelines);
650
+ const tabsResponse = await api.get(`/api/v3/campaigns/${campaignOfferId}/signal-discovery/tabs`);
651
+ const signalTabs = tabsResponse.tabs ?? [];
652
+ const keywords = keywordsFromSignalTabs(signalTabs);
653
+ const excludedPostIds = selectedPostIdsFromSignalTabs(signalTabs);
654
+ if (keywords.length === 0) {
655
+ return {
656
+ status: "refused",
657
+ refusalReason: "no reusable Signal Discovery keywords were found on the campaign tabs",
658
+ };
659
+ }
660
+ markProviderPromptLoaded({
661
+ provider: "signal-discovery",
662
+ campaignOfferId,
663
+ });
664
+ const searchSummary = await searchSignals({
665
+ type: "keywords",
666
+ keywords: keywords.map((keyword) => ({
667
+ keyword,
668
+ source: "refill-sends-source-continuation",
669
+ })),
670
+ campaignOfferId,
671
+ currentStep: null,
672
+ headlineICPCriteria,
673
+ rubricGuidelines: headlineICPCriteria,
674
+ confirmed: true,
675
+ limit: 50,
676
+ });
677
+ const selectedPostIds = postIdsFromSignalSearch(searchSummary, maxPostsToScrape, { excludePostIds: excludedPostIds });
678
+ if (selectedPostIds.length === 0) {
679
+ return {
680
+ status: "refused",
681
+ refusalReason: "Signal Discovery search returned no new recommended posts to continue the source",
682
+ result: { keywords, excludedPostIds, searchSummary },
683
+ };
684
+ }
685
+ const selectionResult = await selectPromisingPosts({
686
+ campaignOfferId,
687
+ selections: selectedPostIds.map((postId) => ({
688
+ postId,
689
+ reason: "refill_sends same-source continuation: recent post from the campaign's existing Signal Discovery keyword family",
690
+ })),
691
+ headlineICPCriteria,
692
+ currentStep: null,
693
+ selectionMode: "replace",
694
+ scrapePlanMode: "all-selected",
695
+ });
696
+ if (selectionResult.success === false) {
697
+ return {
698
+ status: "refused",
699
+ refusalReason: stringValue(selectionResult.message) ??
700
+ "select_promising_posts did not select any posts",
701
+ result: { keywords, selectedPostIds, selectionResult },
702
+ };
703
+ }
704
+ const importResult = await importLeads({
705
+ campaignOfferId,
706
+ provider: "signal-discovery",
707
+ sourceLeadListId,
708
+ currentStep: null,
709
+ headlineICPCriteria,
710
+ rubricGuidelines: headlineICPCriteria,
711
+ confirmed: true,
712
+ maxPostsToScrape: selectedPostIds.length,
713
+ allowInvalidSignalPosts: true,
714
+ });
715
+ const importRecord = recordValue(importResult);
716
+ if (importRecord?.error) {
717
+ return {
718
+ status: "refused",
719
+ refusalReason: stringValue(importRecord.message) ??
720
+ `Signal Discovery import returned ${String(importRecord.error)}`,
721
+ result: {
722
+ keywords,
723
+ excludedPostIds,
724
+ selectedPostIds,
725
+ selectionResult,
726
+ importResult,
727
+ },
728
+ };
729
+ }
730
+ return {
731
+ status: "executed_and_reread",
732
+ result: {
733
+ provider: "signal-discovery",
734
+ campaignOfferId,
735
+ previousSourceLeadListId: sourceLeadListId,
736
+ sourceFingerprint,
737
+ keywords,
738
+ excludedPostIds,
739
+ selectedPostIds,
740
+ sourceRowLimit,
741
+ maxPostsToScrape: selectedPostIds.length,
742
+ searchSummary,
743
+ selectionResult,
744
+ importResult,
745
+ },
746
+ };
747
+ }
748
+ async function refreshPaidInmailCreditsWithRetry(senderId, workspaceId) {
749
+ const errors = [];
750
+ for (let attempt = 1; attempt <= PAID_INMAIL_REFRESH_MAX_ATTEMPTS; attempt += 1) {
751
+ try {
752
+ const receipt = await refreshPaidInmailCredits({
753
+ senderId,
754
+ ...(workspaceId ? { workspaceId } : {}),
755
+ });
756
+ return { receipt, attempts: attempt, errors };
757
+ }
758
+ catch (error) {
759
+ errors.push(error instanceof Error ? error.message : String(error));
760
+ if (attempt < PAID_INMAIL_REFRESH_MAX_ATTEMPTS) {
761
+ await sleep(PAID_INMAIL_REFRESH_RETRY_DELAY_MS);
762
+ }
763
+ }
764
+ }
765
+ return {
766
+ receipt: null,
767
+ attempts: PAID_INMAIL_REFRESH_MAX_ATTEMPTS,
768
+ errors,
769
+ };
770
+ }
771
+ async function executeOneYoloPrimitive(action) {
772
+ if (!action)
773
+ return { status: "no_action" };
774
+ if (action.yoloEligible === false) {
775
+ return {
776
+ status: "refused",
777
+ refusalReason: "first global action is not yolo eligible",
778
+ };
779
+ }
780
+ switch (action.type) {
781
+ case "wait_for_scheduler":
782
+ case "wait_for_active_work":
783
+ case "wait_for_source_import":
784
+ return { status: "read_only_reread", result: { waited: true } };
785
+ case "prepare_messages": {
786
+ const campaignId = actionCampaignId(action);
787
+ const tableId = actionTableId(action) ?? undefined;
788
+ const toolInput = actionToolInput(action);
789
+ const targetPreparedMessages = numberValue(toolInput.targetPreparedMessages);
790
+ const rowSelector = prepareRowSelectorValue(toolInput.rowSelector);
791
+ const approvalMode = toolInput.approvalMode === "approve" ? "approve" : "mark_ready";
792
+ if (!campaignId ||
793
+ !targetPreparedMessages ||
794
+ targetPreparedMessages <= 0) {
795
+ return {
796
+ status: "refused",
797
+ refusalReason: "prepare_messages action is missing campaignId or bounded targetPreparedMessages",
798
+ };
799
+ }
800
+ const result = await startPrepareCampaignMessages({
801
+ campaignId,
802
+ tableId,
803
+ targetPreparedMessages,
804
+ maxRowsToCheck: numberValue(toolInput.maxRowsToCheck) ?? 300,
805
+ approvalMode,
806
+ rowSelector,
807
+ autoContinue: true,
808
+ disableLowPassRateStop: true,
809
+ senderId: actionSenderId(action) ?? undefined,
810
+ actionType: actionActionType(action) ?? undefined,
811
+ requestHash: refillPrepareRequestHash({
812
+ action,
813
+ campaignId,
814
+ tableId,
815
+ approvalMode,
816
+ rowSelector,
817
+ }),
818
+ requestSource: "refill_sends",
819
+ });
820
+ return { status: "executed_and_reread", result };
821
+ }
822
+ case "copy_selected_source_rows": {
823
+ const campaignOfferId = actionCampaignId(action);
824
+ const sourceLeadListId = actionSourceLeadListId(action);
825
+ const toolInput = actionToolInput(action);
826
+ const sourceRowIds = stringArray(toolInput.sourceRowIds);
827
+ const sourceRowLimit = numberValue(toolInput.sourceRowLimit);
828
+ const sourceFingerprint = stringValue(toolInput.sourceFingerprint) ??
829
+ stringValue(actionIds(action).sourceFingerprint);
830
+ if (!campaignOfferId || !sourceLeadListId || !sourceFingerprint) {
831
+ return {
832
+ status: "refused",
833
+ refusalReason: "copy_selected_source_rows action is missing campaignOfferId, sourceLeadListId, or sourceFingerprint",
834
+ };
835
+ }
836
+ if (sourceRowIds.length === 0 &&
837
+ (!sourceRowLimit || sourceRowLimit <= 0)) {
838
+ return {
839
+ status: "refused",
840
+ refusalReason: "copy_selected_source_rows action is missing exact sourceRowIds or a bounded sourceRowLimit",
841
+ };
842
+ }
843
+ const copyInput = {
844
+ campaignOfferId,
845
+ sourceLeadListId,
846
+ currentStep: null,
847
+ confirmed: true,
848
+ sourceRowIds: sourceRowIds.length > 0 ? sourceRowIds : undefined,
849
+ sourceRowLimit: sourceRowIds.length > 0 ? undefined : (sourceRowLimit ?? undefined),
850
+ reviewBatchLimit: sourceRowIds.length > 0
851
+ ? sourceRowIds.length
852
+ : (sourceRowLimit ?? undefined),
853
+ };
854
+ let result;
855
+ try {
856
+ result = await confirmLeadList(copyInput);
857
+ }
858
+ catch (error) {
859
+ const rowLimitPayload = userAddedRowsLimitPayloadFromError(error);
860
+ if (!rowLimitPayload)
861
+ throw error;
862
+ const remainingRows = typeof rowLimitPayload.remainingRows === "number" &&
863
+ Number.isFinite(rowLimitPayload.remainingRows)
864
+ ? Math.floor(rowLimitPayload.remainingRows)
865
+ : 0;
866
+ if (remainingRows <= 0) {
867
+ return {
868
+ status: "refused",
869
+ refusalReason: rowLimitPayload.error ??
870
+ "selected campaign table is at the workflow row limit",
871
+ };
872
+ }
873
+ const retrySourceRowIds = sourceRowIds.length > 0 ? sourceRowIds.slice(0, remainingRows) : [];
874
+ const retrySourceRowLimit = retrySourceRowIds.length > 0
875
+ ? undefined
876
+ : Math.min(sourceRowLimit ?? remainingRows, remainingRows);
877
+ const retryReviewBatchLimit = retrySourceRowIds.length > 0
878
+ ? retrySourceRowIds.length
879
+ : retrySourceRowLimit;
880
+ if (!retryReviewBatchLimit || retryReviewBatchLimit <= 0) {
881
+ return {
882
+ status: "refused",
883
+ refusalReason: rowLimitPayload.error ??
884
+ "selected campaign table cannot accept more workflow rows",
885
+ };
886
+ }
887
+ const retryResult = await confirmLeadList({
888
+ ...copyInput,
889
+ sourceRowIds: retrySourceRowIds.length > 0 ? retrySourceRowIds : undefined,
890
+ sourceRowLimit: retrySourceRowIds.length > 0 ? undefined : retrySourceRowLimit,
891
+ reviewBatchLimit: retryReviewBatchLimit,
892
+ });
893
+ result =
894
+ retryResult && typeof retryResult === "object"
895
+ ? {
896
+ ...retryResult,
897
+ rowLimitRetry: {
898
+ code: rowLimitPayload.code,
899
+ maxRows: rowLimitPayload.maxRows,
900
+ currentRows: rowLimitPayload.currentRows,
901
+ requestedRows: rowLimitPayload.requestedRows,
902
+ remainingRows,
903
+ retriedRows: retryReviewBatchLimit,
904
+ },
905
+ }
906
+ : {
907
+ result: retryResult,
908
+ rowLimitRetry: {
909
+ code: rowLimitPayload.code,
910
+ remainingRows,
911
+ retriedRows: retryReviewBatchLimit,
912
+ },
913
+ };
914
+ }
915
+ return { status: "executed_and_reread", result };
916
+ }
917
+ case "continue_signal_discovery_source":
918
+ return continueSignalDiscoverySource(action);
919
+ case "approve_messages":
920
+ return approveGeneratedMessagesBatch(action);
921
+ default:
922
+ return {
923
+ status: "refused",
924
+ refusalReason: `action ${String(action.type)} is not a safe yolo primitive`,
925
+ };
926
+ }
927
+ }
376
928
  export async function executeRefillSendsCommand(input = {}) {
377
929
  const yolo = input.yolo === true;
378
930
  const executionMode = input.executionMode ?? (yolo ? "yolo" : "manual");
@@ -401,6 +953,12 @@ export async function executeRefillSendsCommand(input = {}) {
401
953
  failedPaidInmailRefreshes: [],
402
954
  note: "Automatic paid InMail credit refresh only runs for --yolo refill_sends calls.",
403
955
  },
956
+ yoloExecution: {
957
+ enabled: false,
958
+ status: "not_run_without_yolo",
959
+ selectedAction: null,
960
+ targetPlanReread: false,
961
+ },
404
962
  };
405
963
  }
406
964
  const workspaceId = workspaceContext && workspaceContext.ok
@@ -413,32 +971,45 @@ export async function executeRefillSendsCommand(input = {}) {
413
971
  const failedPaidInmailRefreshes = [];
414
972
  const refreshReceipts = [];
415
973
  for (const action of refreshActions) {
416
- try {
417
- const receipt = await refreshPaidInmailCredits({
418
- senderId: action.senderId,
419
- ...(workspaceId ? { workspaceId } : {}),
420
- });
974
+ const refreshResult = await refreshPaidInmailCreditsWithRetry(action.senderId, workspaceId ?? undefined);
975
+ if (refreshResult.receipt) {
421
976
  refreshedPaidInmailSenderIds.push(action.senderId);
422
977
  refreshReceipts.push({
423
978
  senderId: action.senderId,
424
979
  actionKey: action.actionKey ?? null,
425
- receipt,
980
+ attempts: refreshResult.attempts,
981
+ retryErrors: refreshResult.errors,
982
+ receipt: refreshResult.receipt,
426
983
  });
427
984
  }
428
- catch (error) {
985
+ else {
429
986
  failedPaidInmailRefreshes.push({
430
987
  senderId: action.senderId,
431
- error: error instanceof Error ? error.message : String(error),
988
+ attempts: refreshResult.attempts,
989
+ error: refreshResult.errors[refreshResult.errors.length - 1] ??
990
+ "paid InMail credit refresh failed",
991
+ errors: refreshResult.errors,
432
992
  });
433
993
  }
434
994
  }
435
995
  const targetPlan = refreshActions.length > 0
436
996
  ? await getRefillTargetPlan(targetPlanInput)
437
997
  : targetPlanBeforePaidRefresh;
998
+ const selectedAction = refreshActions.length > 0 ? null : firstGlobalAction(targetPlan);
999
+ const primitiveAttempt = refreshActions.length > 0
1000
+ ? null
1001
+ : await executeOneYoloPrimitive(selectedAction);
1002
+ const shouldRereadAfterPrimitive = primitiveAttempt?.status === "executed_and_reread" ||
1003
+ primitiveAttempt?.status === "read_only_reread";
1004
+ const postActionTargetPlan = shouldRereadAfterPrimitive
1005
+ ? await getRefillTargetPlan(targetPlanInput)
1006
+ : null;
1007
+ const finalTargetPlan = postActionTargetPlan ?? targetPlan;
438
1008
  return {
439
1009
  ...command,
440
- targetPlan,
1010
+ targetPlan: finalTargetPlan,
441
1011
  targetPlanBeforePaidRefresh: refreshActions.length > 0 ? targetPlanBeforePaidRefresh : null,
1012
+ targetPlanBeforeYoloPrimitive: refreshActions.length === 0 && postActionTargetPlan ? targetPlan : null,
442
1013
  autoPaidInmailRefresh: {
443
1014
  enabled: true,
444
1015
  status: refreshActions.length === 0
@@ -454,8 +1025,26 @@ export async function executeRefillSendsCommand(input = {}) {
454
1025
  workspaceId,
455
1026
  workspaceResolution: workspaceId ? "explicit" : "active_config",
456
1027
  note: refreshActions.length > 0
457
- ? "refill_sends refreshed stale paid InMail credit facts internally, once per sender, then returned the post-refresh targetPlan."
1028
+ ? "refill_sends refreshed stale paid InMail credit facts internally with bounded retries, once per sender, then returned the post-refresh targetPlan."
458
1029
  : "No stale or missing paid InMail credit refresh candidates were present in the initial target plan.",
459
1030
  },
1031
+ yoloExecution: refreshActions.length > 0
1032
+ ? {
1033
+ enabled: true,
1034
+ status: "skipped_after_paid_refresh",
1035
+ selectedAction: null,
1036
+ targetPlanReread: true,
1037
+ }
1038
+ : {
1039
+ enabled: true,
1040
+ status: primitiveAttempt?.status ?? "no_action",
1041
+ selectedAction,
1042
+ result: primitiveAttempt?.result,
1043
+ targetPlanReread: shouldRereadAfterPrimitive,
1044
+ refusalReason: primitiveAttempt?.refusalReason,
1045
+ postActionFirstAction: postActionTargetPlan
1046
+ ? firstGlobalAction(postActionTargetPlan)
1047
+ : null,
1048
+ },
460
1049
  };
461
1050
  }