@happyvertical/garrula 0.1.2 → 0.1.4

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
@@ -42,6 +42,8 @@ The host hands in plain `SourceEvent` data; Garrula never polls GitHub.
42
42
 
43
43
  ```ts
44
44
  await garrula.ingestSourceEvent({ kind: "package-publish", id, title, url, changelogLine }, { tenantId });
45
+ // or draft text a caller supplies (never auto-approved; idempotent by key):
46
+ await garrula.createDraft({ text, claims, idempotencyKey, createdBy }, { tenantId });
45
47
  const [draft] = await garrula.listDrafts({ state: "draft" }, { tenantId });
46
48
  await garrula.approve(draft.id, { tenantId });
47
49
  await garrula.publishDue({ tenantId });
package/dist/Garrula.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Agent, AgentAdminRoute, AgentOptions, AgentUISlots } from '@happyvertical/smrt-agents';
2
2
  import { GarrulaConfig } from './config.js';
3
3
  import { OutboundStores } from './outbound/service.js';
4
- import { DraftClaim, DraftingAI, IngestResult, OutboundDraft, OutboundPostKind, OutboundSettings, OutboundSettingsPatch, OutboundState, PublishDueResult, SourceEvent, SpendSummary } from './outbound/types.js';
4
+ import { CreateDraftInput, CreateDraftResult, DraftClaim, DraftingAI, IngestResult, OutboundDraft, OutboundPostKind, OutboundSettings, OutboundSettingsPatch, OutboundState, PublishDueResult, SourceEvent, SpendSummary } from './outbound/types.js';
5
5
  import { AnalyticsSyncResult, ArticleVideoPostRequest, GarrulaCollections, GarrulaHost, GarrulaSocialPost, PublishPostOptions, PublishPostResult, SocialFactory, StoryLinkPostRequest } from './types.js';
6
6
  export interface GarrulaOptions extends AgentOptions {
7
7
  collections?: GarrulaCollections;
@@ -73,6 +73,15 @@ export declare class Garrula extends Agent {
73
73
  ingestSourceEvent(event: SourceEvent, options?: {
74
74
  tenantId?: string;
75
75
  }): Promise<IngestResult>;
76
+ /**
77
+ * Create a pending-approval draft from supplied text and evidence. Tenant
78
+ * scoped, idempotent by `idempotencyKey`, records `createdBy`, and never
79
+ * approves or publishes: a person must approve it and its claims need
80
+ * evidence before it can publish. The host authorizes the caller.
81
+ */
82
+ createDraft(input: CreateDraftInput, options?: {
83
+ tenantId?: string;
84
+ }): Promise<CreateDraftResult>;
76
85
  listDrafts(filter?: {
77
86
  state?: OutboundState;
78
87
  kind?: OutboundPostKind;
package/dist/index.d.ts CHANGED
@@ -7,4 +7,4 @@ export { DEFAULT_OUTBOUND_RATES, FALLBACK_X_RATES, type PlatformRates, } from '.
7
7
  export { createOutboundStores, OutboundService, type OutboundStores } from './outbound/service.js';
8
8
  export { DEFAULT_AUTONOMY, DEFAULT_SOURCE_RULES } from './outbound/sources.js';
9
9
  export { OUTBOUND_TRANSITIONS, canTransition } from './outbound/state.js';
10
- export { type AutonomyMode, type ClaimEvidence, type DraftClaim, type DraftingAI, type IngestResult, type LinkPolicy, type OutboundDraft, type OutboundPostKind, type OutboundRefusal, type OutboundRefusalCode, OutboundError, type OutboundSettings, type OutboundSettingsPatch, type OutboundState, type PublishDueResult, type SourceEvent, type SourceEventKind, type SourceRule, type SpendEntryView, type SpendSummary, } from './outbound/types.js';
10
+ export { type AutonomyMode, type ClaimEvidence, type CreateDraftInput, type CreateDraftResult, type DraftClaim, type DraftingAI, type IngestResult, type LinkPolicy, type OutboundDraft, type OutboundPostKind, type OutboundRefusal, type OutboundRefusalCode, type OutboundSourceKind, OutboundError, type OutboundSettings, type OutboundSettingsPatch, type OutboundState, type PublishDueResult, type SourceEvent, type SourceEventKind, type SourceRule, type SpendEntryView, type SpendSummary, } from './outbound/types.js';
package/dist/index.js CHANGED
@@ -380,6 +380,8 @@ var GarrulaOutboundPost = class GarrulaOutboundPost extends SmrtObject {
380
380
  sourceKind = "package-publish";
381
381
  sourceUrl = null;
382
382
  claims = [];
383
+ /** Who supplied a manual draft (`createDraft`); null for ingested drafts. */
384
+ createdBy = null;
383
385
  approvedBy = null;
384
386
  rejectedReason = null;
385
387
  constructor(options = {}) {
@@ -392,6 +394,7 @@ var GarrulaOutboundPost = class GarrulaOutboundPost extends SmrtObject {
392
394
  if (options.sourceKind !== void 0) this.sourceKind = options.sourceKind;
393
395
  if (options.sourceUrl !== void 0) this.sourceUrl = options.sourceUrl;
394
396
  if (options.claims !== void 0) this.claims = options.claims;
397
+ if (options.createdBy !== void 0) this.createdBy = options.createdBy;
395
398
  if (options.approvedBy !== void 0) this.approvedBy = options.approvedBy;
396
399
  if (options.rejectedReason !== void 0) this.rejectedReason = options.rejectedReason;
397
400
  }
@@ -861,6 +864,108 @@ var OutboundService = class {
861
864
  };
862
865
  }));
863
866
  }
867
+ /**
868
+ * Create a pending-approval draft from text and evidence supplied by the
869
+ * host's caller. Tenant scoped and idempotent by `idempotencyKey`: a repeat
870
+ * returns the original drafts. It never approves, schedules or publishes, and
871
+ * ignores the autonomy settings; a person must still approve it.
872
+ */
873
+ createDraft(tenantId, input) {
874
+ assertAmbientTenant(tenantId);
875
+ if (!input || typeof input !== "object") throw new OutboundError("invalid", "A draft needs text, claims, a key and a creator.");
876
+ const key = String(input.idempotencyKey ?? "").trim();
877
+ const createdBy = String(input?.createdBy ?? "").trim();
878
+ const text = String(input?.text ?? "").trim();
879
+ if (!key || key.length > 200) throw new OutboundError("invalid", "A draft needs an idempotency key of at most 200 characters.");
880
+ if (!createdBy) throw new OutboundError("invalid", "A draft needs a creator.");
881
+ if (!text) throw new OutboundError("invalid", "The post text cannot be empty.");
882
+ const max = this.configFn().outboundMaxLength;
883
+ if (text.length > max) throw new OutboundError("invalid", `The post text is longer than ${max} characters.`);
884
+ const kind = input.kind ?? "release";
885
+ if (!OUTBOUND_POST_KINDS.includes(kind)) throw new OutboundError("invalid", "Unknown post kind.");
886
+ if (!Array.isArray(input.claims)) throw new OutboundError("invalid", "Claims must be a list.");
887
+ const claims = input.claims.map((claim) => ({
888
+ text: String(claim?.text ?? "").trim(),
889
+ evidence: (Array.isArray(claim?.evidence) ? claim.evidence : []).map((item) => ({
890
+ type: item?.type === "changelog" ? "changelog" : "url",
891
+ ref: String(item?.ref ?? "").trim()
892
+ }))
893
+ }));
894
+ const linkUrl = input.linkUrl?.trim() || null;
895
+ if (linkUrl && !/^https?:\/\/\S+$/i.test(linkUrl)) throw new OutboundError("invalid", "linkUrl must be an http(s) URL.");
896
+ return withLock(tenantId, () => withTenant({ tenantId }, async () => {
897
+ const ledgerKey = `manual:${key}`;
898
+ const settings = this.toSettings(tenantId, await this.findSettingsRow(tenantId));
899
+ const seen = (await this.stores.events.list({ where: {
900
+ tenantId,
901
+ eventKey: ledgerKey
902
+ } })).find((row) => row.tenantId === tenantId);
903
+ if (seen) {
904
+ const drafts = [];
905
+ for (const id of seen.outboundPostIds ?? []) drafts.push(await this.view(tenantId, await this.requireRow(tenantId, id), settings));
906
+ return {
907
+ outcome: "duplicate",
908
+ drafts
909
+ };
910
+ }
911
+ const accounts = await this.targetAccounts(tenantId, settings);
912
+ if (accounts.length === 0) return { outcome: "no-accounts" };
913
+ const drafts = [];
914
+ const ids = [];
915
+ const covered = /* @__PURE__ */ new Set();
916
+ const earlier = (await this.stores.posts.list({ where: {
917
+ tenantId,
918
+ sourceKind: "manual",
919
+ sourceEventId: key
920
+ } })).filter((row) => row.tenantId === tenantId);
921
+ for (const row of earlier) {
922
+ const view = await this.view(tenantId, row, settings);
923
+ covered.add(view.accountId);
924
+ ids.push(row.id);
925
+ drafts.push(view);
926
+ }
927
+ for (const account of accounts) {
928
+ if (covered.has(account.id)) continue;
929
+ const post = await this.social.socialPosts.createDraft({
930
+ socialAccountId: account.id,
931
+ postType: "text",
932
+ title: `${OUTBOUND_POST_MARKER}manual:${key}`,
933
+ description: text,
934
+ linkUrl,
935
+ tenantId
936
+ });
937
+ let row;
938
+ try {
939
+ row = await this.stores.posts.create({
940
+ tenantId,
941
+ socialPostId: post.id,
942
+ state: "draft",
943
+ kind,
944
+ sourceEventId: key,
945
+ sourceKind: "manual",
946
+ sourceUrl: linkUrl,
947
+ claims,
948
+ createdBy
949
+ });
950
+ } catch (error) {
951
+ await this.discardSocialDraft(post);
952
+ throw error;
953
+ }
954
+ ids.push(row.id);
955
+ drafts.push(await this.view(tenantId, row, settings));
956
+ }
957
+ await this.stores.events.create({
958
+ tenantId,
959
+ eventKey: ledgerKey,
960
+ outcome: "drafted",
961
+ outboundPostIds: ids
962
+ });
963
+ return {
964
+ outcome: "drafted",
965
+ drafts
966
+ };
967
+ }));
968
+ }
864
969
  async targetAccounts(tenantId, settings) {
865
970
  return (await this.social.socialAccounts.findReady()).filter((account) => account.id && account.tenantId === tenantId && (settings.accountIds.length === 0 || settings.accountIds.includes(account.id)));
866
971
  }
@@ -894,6 +999,7 @@ var OutboundService = class {
894
999
  linkUrl: policy === "include" ? post?.linkUrl ?? row.sourceUrl ?? null : null,
895
1000
  sourceEventId: row.sourceEventId,
896
1001
  sourceKind: row.sourceKind,
1002
+ createdBy: row.createdBy ?? null,
897
1003
  scheduledAt: post?.scheduledAt ? new Date(post.scheduledAt) : null,
898
1004
  publishedUrl: post?.platformUrl ?? null,
899
1005
  error: post?.errorMessage ?? null,
@@ -1433,6 +1539,16 @@ var Garrula = class Garrula extends Agent {
1433
1539
  const { service, tenantId } = this.outboundFor(options);
1434
1540
  return service.ingestSourceEvent(tenantId, event);
1435
1541
  }
1542
+ /**
1543
+ * Create a pending-approval draft from supplied text and evidence. Tenant
1544
+ * scoped, idempotent by `idempotencyKey`, records `createdBy`, and never
1545
+ * approves or publishes: a person must approve it and its claims need
1546
+ * evidence before it can publish. The host authorizes the caller.
1547
+ */
1548
+ async createDraft(input, options) {
1549
+ const { service, tenantId } = this.outboundFor(options);
1550
+ return service.createDraft(tenantId, input);
1551
+ }
1436
1552
  async listDrafts(filter = {}, options) {
1437
1553
  const { service, tenantId } = this.outboundFor(options);
1438
1554
  return service.listDrafts(tenantId, filter);