@happyvertical/garrula 0.0.0-stage → 0.1.1
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/AGENTS.md +71 -0
- package/CLAUDE.md +1 -0
- package/README.md +117 -2
- package/dist/Garrula.d.ts +240 -0
- package/dist/config.d.ts +49 -0
- package/dist/credentials.d.ts +57 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +2135 -0
- package/dist/index.js.map +1 -0
- package/dist/manifest.json +2217 -0
- package/dist/outbound/drafting.d.ts +23 -0
- package/dist/outbound/models.d.ts +108 -0
- package/dist/outbound/rates.d.ts +35 -0
- package/dist/outbound/service.d.ts +127 -0
- package/dist/outbound/sources.d.ts +20 -0
- package/dist/outbound/state.d.ts +6 -0
- package/dist/outbound/types.d.ts +165 -0
- package/dist/smrt-knowledge.json +1161 -0
- package/dist/types.d.ts +199 -0
- package/dist/ui/admin/ActivitySpendPanel.svelte +340 -0
- package/dist/ui/admin/ActivitySpendPanel.svelte.d.ts +3 -0
- package/dist/ui/admin/AdminRoot.svelte +70 -0
- package/dist/ui/admin/AdminRoot.svelte.d.ts +9 -0
- package/dist/ui/admin/DraftCard.svelte +359 -0
- package/dist/ui/admin/DraftCard.svelte.d.ts +17 -0
- package/dist/ui/admin/PersonaPolicyPanel.svelte +177 -0
- package/dist/ui/admin/PersonaPolicyPanel.svelte.d.ts +3 -0
- package/dist/ui/admin/QueuePanel.svelte +192 -0
- package/dist/ui/admin/QueuePanel.svelte.d.ts +3 -0
- package/dist/ui/admin/SettingsPanel.svelte +338 -0
- package/dist/ui/admin/SettingsPanel.svelte.d.ts +4 -0
- package/dist/ui/admin/SourcesPanel.svelte +196 -0
- package/dist/ui/admin/SourcesPanel.svelte.d.ts +3 -0
- package/dist/ui/admin/StatusRegion.svelte +41 -0
- package/dist/ui/admin/StatusRegion.svelte.d.ts +7 -0
- package/dist/ui/admin/access.d.ts +23 -0
- package/dist/ui/admin/access.js +30 -0
- package/dist/ui/admin/format.d.ts +9 -0
- package/dist/ui/admin/format.js +39 -0
- package/dist/ui/admin/index.d.ts +12 -0
- package/dist/ui/admin/index.js +17 -0
- package/dist/ui/admin/nav.d.ts +2 -0
- package/dist/ui/admin/nav.js +7 -0
- package/dist/ui/types.d.ts +86 -0
- package/dist/ui/types.js +63 -0
- package/docs/manifest-routing.md +41 -0
- package/package.json +111 -4
package/AGENTS.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Repository Agent Instructions
|
|
2
|
+
|
|
3
|
+
<!-- hv-managed-policy:start revision=1.0.0 sha256=2c2f4d048293cab2fc7f8c636eee474c0386c13a9c7bbf2c535c5ada47d1d6e5 -->
|
|
4
|
+
|
|
5
|
+
## Shared development kernel
|
|
6
|
+
|
|
7
|
+
- Be concise. Load detailed SOP skills only when the task triggers them.
|
|
8
|
+
- Read the repository's `.agents/project.yaml` and nearest `AGENTS.md` files before work.
|
|
9
|
+
- Use `implement` by default for accepted issue implementation.
|
|
10
|
+
- Tracked implementation work is complete when documented validation is green, `review-cycle` has passed, the claim is handed off, and a ready-for-review pull request exists; do this unprompted, even where harness defaults wait for a user request. Before editing untracked requested work, create and claim its issue, or — patch-class only — record it on this session's open patch train; work the user explicitly scopes as a throwaway spike is exempt: it ends at its report and never enters the commit, push, or PR lifecycle.
|
|
11
|
+
- Claim an issue before editing it: add `agent: implementation` and post one claim comment naming your runtime, session, and branch. Do not take an issue another session holds with activity in the last 24 hours without a handoff. Any agent may assign work to another agent with a `dispatch: <runtime>` label and an instruction comment; the receiving agent claims it.
|
|
12
|
+
- Patch-class work — small bug, doc, and improvement changes with no schema, contract, dependency, or breaking change — may bundle as one patch train on one branch and pull request with one commit per item. Other work stays one issue per pull request. An incidental patch-class fix of ten lines or fewer near files under edit ships in the same pull request as its own commit, listed under `Drive-by fixes` in the PR description; other findings go to the tracker.
|
|
13
|
+
- Hand off intentionally: when done, blocked, or stopping, update your claim comment with the outcome and next step and remove `agent: implementation`. Never delete claim history.
|
|
14
|
+
- Open pull requests only when reviewable, never as drafts, and keep them ready for review. Watch a ready PR until it is mergeable — no base conflicts, no unresolved review threads, the repository's required checks green, its required approvals satisfied — or report a concrete blocker.
|
|
15
|
+
- Incomplete work remains ready with `status: blocked` and a concrete handoff. Review agents do not claim implementation.
|
|
16
|
+
- Agents do not merge unless explicitly authorized in the current session, and then only when the repository's own required checks and approvals pass.
|
|
17
|
+
- Run documented validation and update affected docs before shipping.
|
|
18
|
+
- Token efficiency: risk defaults to standard, high needs a named trigger; after the first final pass only accepted blockers reopen edits; after six passes, ask the user before more; wait outside the implementer.
|
|
19
|
+
- Preserve unrelated work. Never expose or retain secrets.
|
|
20
|
+
- Use repository Hindsight memory for durable, provenance-linked knowledge; do not store transient logs or duplicate canonical docs.
|
|
21
|
+
- Shared SOPs and portable skills come from the designated control-plane repository. Repositories choose their own technology and may add stricter local rules.
|
|
22
|
+
|
|
23
|
+
<!-- hv-managed-policy:end -->
|
|
24
|
+
|
|
25
|
+
## Repository-specific guidance
|
|
26
|
+
|
|
27
|
+
# Garrula
|
|
28
|
+
|
|
29
|
+
Garrula is the reusable social-media publishing agent for SMRT applications,
|
|
30
|
+
published as `@happyvertical/garrula`. It drafts posts for a tenant's ready
|
|
31
|
+
`smrt-social` accounts, publishes due posts (claiming each post atomically so it
|
|
32
|
+
is never sent twice), refreshes OAuth tokens, and records analytics.
|
|
33
|
+
|
|
34
|
+
## Setup and validation
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pnpm install --frozen-lockfile
|
|
38
|
+
pnpm validate
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Use Node and pnpm versions from `package.json`. Run the narrowest relevant check
|
|
42
|
+
first (`pnpm test`, `pnpm typecheck`), then `pnpm validate` before a pull
|
|
43
|
+
request. Tests are in process only (SQLite via `@happyvertical/sql`, fake social
|
|
44
|
+
adapters); never start containers or call real platform APIs.
|
|
45
|
+
|
|
46
|
+
## Architecture boundaries
|
|
47
|
+
|
|
48
|
+
- Account, post, and snapshot persistence belongs to `@happyvertical/smrt-social`;
|
|
49
|
+
platform calls belong to `@happyvertical/social`. Garrula orchestrates them.
|
|
50
|
+
- Tenant comes from the request's `tenantId` or the surrounding `withTenant`
|
|
51
|
+
context, never from a global; the only cross-tenant entry points are `run()`
|
|
52
|
+
and an unscoped `syncAnalytics()` (the background job), which enter each
|
|
53
|
+
post's own tenant before touching it.
|
|
54
|
+
- Anything specific to one application (target platforms, wording, refresh
|
|
55
|
+
timing, tenant lifecycle, media storage) is host configuration
|
|
56
|
+
(`GarrulaConfig`, `GarrulaHost`) with neutral defaults, not code.
|
|
57
|
+
- The `@smrt()` decorator keeps `api` closed; see `docs/manifest-routing.md`
|
|
58
|
+
before changing it.
|
|
59
|
+
- All `@happyvertical/smrt-*` packages share one exact version; bump them
|
|
60
|
+
together. `@happyvertical/social` is pinned exactly.
|
|
61
|
+
|
|
62
|
+
## Package surfaces
|
|
63
|
+
|
|
64
|
+
- `.`, `./admin`, `./admin/*.svelte` and `./manifest` are the public exports;
|
|
65
|
+
validate them with `pnpm verify:pack`.
|
|
66
|
+
- Admin UI is host-agnostic: it takes an API client and base paths as props and
|
|
67
|
+
owns no application routing.
|
|
68
|
+
- Releases are generated by the merge workflow (`pnpm changeset:auto`); do not
|
|
69
|
+
hand-write changesets. The package stays on the 0.x line.
|
|
70
|
+
- `CLAUDE.md`, `GEMINI.md`, and Copilot instructions are generated adapters;
|
|
71
|
+
update this file for durable repository guidance.
|
package/CLAUDE.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
@AGENTS.md
|
package/README.md
CHANGED
|
@@ -1,3 +1,118 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @happyvertical/garrula
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Social-media publishing agent for SMRT applications: drafts posts from your
|
|
4
|
+
content, publishes due posts to YouTube, X, Threads and Facebook, keeps account
|
|
5
|
+
sign-ins fresh, and records post analytics. Every operation runs in a tenant;
|
|
6
|
+
the tenant comes from the request or the surrounding `withTenant` context, never
|
|
7
|
+
from a global.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pnpm add @happyvertical/garrula
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Requires Node 24.22+, `svelte` 5 for the admin UI, and the `@happyvertical/smrt-*`
|
|
16
|
+
family at the version this package pins.
|
|
17
|
+
|
|
18
|
+
## Use
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { Garrula, setGarrulaConfig, setGarrulaHost } from "@happyvertical/garrula";
|
|
22
|
+
|
|
23
|
+
setGarrulaConfig({ storyLinkTargets: ["facebook", "x"], tenantLabel: "newsroom" });
|
|
24
|
+
setGarrulaHost({
|
|
25
|
+
canPublish: async (tenantId) => billing.isActive(tenantId),
|
|
26
|
+
resolveMediaUrl: async (post) => signedUrlFor(post),
|
|
27
|
+
onPublishFailed: async (post, error) => notify(post.createdByUserId, error),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const garrula = await new Garrula({ db }).initialize();
|
|
31
|
+
await withTenant({ tenantId }, () =>
|
|
32
|
+
garrula.createStoryLinkPosts({ contentId, title, url }),
|
|
33
|
+
);
|
|
34
|
+
await garrula.run(); // publish everything due, across tenants
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Outbound release posts
|
|
38
|
+
|
|
39
|
+
Garrula turns release and changelog events into cited, approved posts under a
|
|
40
|
+
per-tenant spend cap and pause switch (outbound only; no mentions or replies).
|
|
41
|
+
The host hands in plain `SourceEvent` data; Garrula never polls GitHub.
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
await garrula.ingestSourceEvent({ kind: "package-publish", id, title, url, changelogLine }, { tenantId });
|
|
45
|
+
const [draft] = await garrula.listDrafts({ state: "draft" }, { tenantId });
|
|
46
|
+
await garrula.approve(draft.id, { tenantId });
|
|
47
|
+
await garrula.publishDue({ tenantId });
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
See [docs/outbound.md](docs/outbound.md) for rules, states, link policy, cost
|
|
51
|
+
and the API.
|
|
52
|
+
|
|
53
|
+
## Exports
|
|
54
|
+
|
|
55
|
+
| Export | Contents |
|
|
56
|
+
| --- | --- |
|
|
57
|
+
| `.` | `Garrula`, `setGarrulaHost`, `setGarrulaConfig`, config and types |
|
|
58
|
+
| `./admin` | `AdminRoot` (default), `QueuePanel`, `SettingsPanel`, `PersonaPolicyPanel`, `SourcesPanel`, `ActivitySpendPanel`, `navItems`, `createAPIClient`, the `GarrulaAdminClient` types and the access context helpers |
|
|
59
|
+
| `./admin/*.svelte` | each panel on its own (`AdminRoot` and the five panels) for hosts that place panels in their own navigation |
|
|
60
|
+
| `./manifest` | the generated SMRT manifest |
|
|
61
|
+
|
|
62
|
+
Whether a host can route the actions from the manifest is in
|
|
63
|
+
[docs/manifest-routing.md](docs/manifest-routing.md).
|
|
64
|
+
|
|
65
|
+
## Configuration
|
|
66
|
+
|
|
67
|
+
`GarrulaConfig` (all optional; set once with `setGarrulaConfig` or per instance
|
|
68
|
+
with `new Garrula({ garrula: {...} })`):
|
|
69
|
+
|
|
70
|
+
| Key | Default | Meaning |
|
|
71
|
+
| --- | --- | --- |
|
|
72
|
+
| `storyLinkTargets` | `facebook, x, threads` | platforms for link drafts without `targets` |
|
|
73
|
+
| `articleVideoTargets` | `youtube, x, threads, facebook` | platforms for video drafts without `targets` |
|
|
74
|
+
| `defaultPublishMode` | `dry_run` | mode for accounts that record none |
|
|
75
|
+
| `tenantLabel` | `organization` | what a tenant is called in messages |
|
|
76
|
+
| `socialSettingsLocation` | `Settings → Social` | where people reconnect an account |
|
|
77
|
+
| `outboundRates` | X: post $0.015, with URL $0.20, reply $0.01 | fallback prices when the adapter reports no cost (temporary, happyvertical.com#518) |
|
|
78
|
+
| `outboundDefaultMonthlyCapUsd` | `5` | cap of a tenant that set none; a cap always applies |
|
|
79
|
+
| `outboundMaxLength` | `280` | longest draft text |
|
|
80
|
+
| `accessTokenRefreshMarginMs` | 5 minutes | OAuth 2 refresh lead time |
|
|
81
|
+
| `threadsRefreshWindowMs` | 7 days | Threads long-lived token refresh window |
|
|
82
|
+
|
|
83
|
+
## Develop
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
pnpm install
|
|
87
|
+
pnpm validate # lint, typecheck, test (node + jsdom component tests), build, verify:pack
|
|
88
|
+
pnpm dev # admin panels against a fake tenant and an in-memory client
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Tests run in process (SQLite through `@happyvertical/sql`, fake social adapters);
|
|
92
|
+
no containers or real platform APIs.
|
|
93
|
+
|
|
94
|
+
## Admin UI
|
|
95
|
+
|
|
96
|
+
The panels are host-agnostic and built on `@happyvertical/smrt-ui`. Each takes
|
|
97
|
+
`apiClient`, `tenant` and `permissions` as props (or from
|
|
98
|
+
`setGarrulaAdminContext`, which `AdminRoot` sets for its children).
|
|
99
|
+
|
|
100
|
+
- `GarrulaAdminClient` is supplied by the host: `listDrafts`, `editDraft`,
|
|
101
|
+
`approveDraft`, `rejectDraft`, `retryDraft`, `scheduleDraft`, `getSettings`,
|
|
102
|
+
`updateSettings`, `getSpend` (the `Garrula` public API bound to the tenant),
|
|
103
|
+
plus the social-account and post calls. The package owns no outbound
|
|
104
|
+
endpoints; `createAPIClient(baseUrl, outbound)` only covers the social routes.
|
|
105
|
+
Dates may arrive as ISO strings.
|
|
106
|
+
- `permissions` is a set of `read`, `approve` (edit, approve, reject, retry),
|
|
107
|
+
`publish` (schedule, sync analytics, pause) and `configure` (accounts, cap,
|
|
108
|
+
links, persona, autonomy, sources; also pause). Actions the person lacks are
|
|
109
|
+
hidden or disabled. This is a convenience only: the host and the Garrula API
|
|
110
|
+
enforce the same rules. With nothing supplied everything is allowed;
|
|
111
|
+
`readonly` narrows to `read`.
|
|
112
|
+
- The queue shows each draft's evidence (claim, then PR URL or changelog
|
|
113
|
+
line). A draft with an uncited claim is flagged and cannot be approved or
|
|
114
|
+
retried.
|
|
115
|
+
|
|
116
|
+
`pnpm dev` serves `dev/` (a seeded "Demo Studio" tenant, a permission toggle
|
|
117
|
+
and a dark-mode toggle). Component tests are in `tests/svelte` and run with
|
|
118
|
+
`pnpm test:svelte`.
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { Agent, AgentAdminRoute, AgentOptions, AgentUISlots } from '@happyvertical/smrt-agents';
|
|
2
|
+
import { GarrulaConfig } from './config.js';
|
|
3
|
+
import { OutboundStores } from './outbound/service.js';
|
|
4
|
+
import { DraftClaim, DraftingAI, IngestResult, OutboundDraft, OutboundPostKind, OutboundSettings, OutboundSettingsPatch, OutboundState, PublishDueResult, SourceEvent, SpendSummary } from './outbound/types.js';
|
|
5
|
+
import { AnalyticsSyncResult, ArticleVideoPostRequest, GarrulaCollections, GarrulaHost, GarrulaSocialPost, PublishPostOptions, PublishPostResult, SocialFactory, StoryLinkPostRequest } from './types.js';
|
|
6
|
+
export interface GarrulaOptions extends AgentOptions {
|
|
7
|
+
collections?: GarrulaCollections;
|
|
8
|
+
socialFactory?: SocialFactory;
|
|
9
|
+
/** Overrides the host hooks registered with {@link setGarrulaHost}. */
|
|
10
|
+
host?: GarrulaHost;
|
|
11
|
+
/** Overrides the configuration registered with {@link setGarrulaConfig}. */
|
|
12
|
+
garrula?: Partial<GarrulaConfig>;
|
|
13
|
+
/**
|
|
14
|
+
* Model client for drafting release posts (default: the agent's own `ai`).
|
|
15
|
+
* Tests pass a fake.
|
|
16
|
+
*/
|
|
17
|
+
draftingAI?: DraftingAI;
|
|
18
|
+
/** Outbound persistence; created from the agent's database on initialize. */
|
|
19
|
+
outboundStores?: OutboundStores;
|
|
20
|
+
/**
|
|
21
|
+
* TEST ONLY. Skips the outbound gate (pause switch, spend cap, approval,
|
|
22
|
+
* evidence) when no outbound stores are available. Without it, `initialize()`
|
|
23
|
+
* throws if the gate cannot be built, so a host never publishes ungated by
|
|
24
|
+
* accident.
|
|
25
|
+
*/
|
|
26
|
+
unsafeDisableOutboundGate?: boolean;
|
|
27
|
+
/** Clock for spend months and due times (tests). */
|
|
28
|
+
now?: () => Date;
|
|
29
|
+
}
|
|
30
|
+
export declare function setGarrulaHost(host: GarrulaHost): void;
|
|
31
|
+
export declare function setGarrulaConfig(config: Partial<GarrulaConfig>): void;
|
|
32
|
+
/** Why a post was not sent because its tenant may not publish (default wording). */
|
|
33
|
+
export declare const GARRULA_TENANT_BLOCKED_MESSAGE: string;
|
|
34
|
+
/** Why a post was not sent because another publish already has it. */
|
|
35
|
+
export declare const GARRULA_POST_BUSY_MESSAGE = "This post is already being posted or is no longer waiting to be posted.";
|
|
36
|
+
/** Why a post was not sent because its account is switched off. */
|
|
37
|
+
export declare const GARRULA_ACCOUNT_INACTIVE_MESSAGE = "This social account is switched off, so nothing was posted.";
|
|
38
|
+
export declare class Garrula extends Agent {
|
|
39
|
+
static uiSlots: AgentUISlots;
|
|
40
|
+
static adminRoutes: AgentAdminRoute[];
|
|
41
|
+
private socialAccounts;
|
|
42
|
+
private socialPosts;
|
|
43
|
+
private analyticsSnapshots;
|
|
44
|
+
private socialFactory?;
|
|
45
|
+
private hostOverride?;
|
|
46
|
+
private configOverride?;
|
|
47
|
+
private draftingAI?;
|
|
48
|
+
private outboundStores?;
|
|
49
|
+
private unsafeDisableOutboundGate;
|
|
50
|
+
private outbound?;
|
|
51
|
+
private nowFn;
|
|
52
|
+
/** The Agent base's persisted config; Garrula's own settings are {@link garrulaConfig}. */
|
|
53
|
+
protected config: Record<string, never>;
|
|
54
|
+
constructor(options?: GarrulaOptions);
|
|
55
|
+
initialize(): Promise<this>;
|
|
56
|
+
private get hostHooks();
|
|
57
|
+
/** The host's configuration over the neutral defaults. */
|
|
58
|
+
get garrulaConfig(): GarrulaConfig;
|
|
59
|
+
/**
|
|
60
|
+
* Publish every post that is due (scheduled time reached, or approved).
|
|
61
|
+
* Posts of an organization that may not publish right now are left as they
|
|
62
|
+
* are and picked up by a later run.
|
|
63
|
+
*/
|
|
64
|
+
run(): Promise<void>;
|
|
65
|
+
createStoryLinkPosts(request: StoryLinkPostRequest): Promise<GarrulaSocialPost[]>;
|
|
66
|
+
createArticleVideoPosts(request: ArticleVideoPostRequest): Promise<GarrulaSocialPost[]>;
|
|
67
|
+
private outboundFor;
|
|
68
|
+
/**
|
|
69
|
+
* Offer a release/changelog event as a post candidate. Events that match no
|
|
70
|
+
* enabled source rule, or were seen before, produce nothing; a match is
|
|
71
|
+
* drafted in the tenant's persona with a cited claim list.
|
|
72
|
+
*/
|
|
73
|
+
ingestSourceEvent(event: SourceEvent, options?: {
|
|
74
|
+
tenantId?: string;
|
|
75
|
+
}): Promise<IngestResult>;
|
|
76
|
+
listDrafts(filter?: {
|
|
77
|
+
state?: OutboundState;
|
|
78
|
+
kind?: OutboundPostKind;
|
|
79
|
+
limit?: number;
|
|
80
|
+
}, options?: {
|
|
81
|
+
tenantId?: string;
|
|
82
|
+
}): Promise<OutboundDraft[]>;
|
|
83
|
+
/** Edit text, claims or link. An edit withdraws approval (back to draft). */
|
|
84
|
+
editDraft(id: string, patch: {
|
|
85
|
+
text?: string;
|
|
86
|
+
claims?: DraftClaim[];
|
|
87
|
+
linkUrl?: string | null;
|
|
88
|
+
}, options?: {
|
|
89
|
+
tenantId?: string;
|
|
90
|
+
}): Promise<OutboundDraft>;
|
|
91
|
+
approve(id: string, options?: {
|
|
92
|
+
tenantId?: string;
|
|
93
|
+
approvedBy?: string;
|
|
94
|
+
}): Promise<OutboundDraft>;
|
|
95
|
+
reject(id: string, reason?: string, options?: {
|
|
96
|
+
tenantId?: string;
|
|
97
|
+
}): Promise<OutboundDraft>;
|
|
98
|
+
/** Send a failed post back to approved so the next run publishes it again. */
|
|
99
|
+
retry(id: string, options?: {
|
|
100
|
+
tenantId?: string;
|
|
101
|
+
}): Promise<OutboundDraft>;
|
|
102
|
+
schedule(id: string, at: Date, options?: {
|
|
103
|
+
tenantId?: string;
|
|
104
|
+
}): Promise<OutboundDraft>;
|
|
105
|
+
/**
|
|
106
|
+
* Publish the tenant's approved posts whose time has come (scheduled time
|
|
107
|
+
* reached, or approved without one). Each goes through the same gate as any
|
|
108
|
+
* publish: a pause or spend cap refuses it without calling the platform.
|
|
109
|
+
*/
|
|
110
|
+
publishDue(options?: {
|
|
111
|
+
tenantId?: string;
|
|
112
|
+
now?: Date;
|
|
113
|
+
}): Promise<PublishDueResult[]>;
|
|
114
|
+
/** Publish one approved or scheduled draft now. */
|
|
115
|
+
publishNow(id: string, options?: {
|
|
116
|
+
tenantId?: string;
|
|
117
|
+
}): Promise<PublishDueResult>;
|
|
118
|
+
private publishOutboundPost;
|
|
119
|
+
getSettings(options?: {
|
|
120
|
+
tenantId?: string;
|
|
121
|
+
}): Promise<OutboundSettings>;
|
|
122
|
+
updateSettings(patch: OutboundSettingsPatch, options?: {
|
|
123
|
+
tenantId?: string;
|
|
124
|
+
}): Promise<OutboundSettings>;
|
|
125
|
+
/** Stop all publishing for the tenant, effective on the very next publish. */
|
|
126
|
+
pause(options?: {
|
|
127
|
+
tenantId?: string;
|
|
128
|
+
}): Promise<OutboundSettings>;
|
|
129
|
+
resume(options?: {
|
|
130
|
+
tenantId?: string;
|
|
131
|
+
}): Promise<OutboundSettings>;
|
|
132
|
+
/** Spend ledger and cap for a month (`YYYY-MM`, default: this month, UTC). */
|
|
133
|
+
getSpend(options?: {
|
|
134
|
+
tenantId?: string;
|
|
135
|
+
month?: string;
|
|
136
|
+
}): Promise<SpendSummary>;
|
|
137
|
+
/** Record the failure on the post, then let the host tell the poster. */
|
|
138
|
+
private recordFailure;
|
|
139
|
+
/**
|
|
140
|
+
* Publish one post. The post is first claimed with a single conditional
|
|
141
|
+
* update (`status` → `publishing`, only from `options.claimFrom`), so
|
|
142
|
+
* overlapping runs or a double-clicked retry never send it twice. A post
|
|
143
|
+
* of an organization that may not publish is left as it is; after the
|
|
144
|
+
* claim, every failure is recorded on the post (never left `publishing`).
|
|
145
|
+
*/
|
|
146
|
+
publishPost(post: GarrulaSocialPost, options?: PublishPostOptions): Promise<PublishPostResult>;
|
|
147
|
+
/** The trusted publish path: enters the post's own tenant (see {@link publishPost}). */
|
|
148
|
+
private publishClaimed;
|
|
149
|
+
/** Record spend and the outbound state once a publish attempt is over. */
|
|
150
|
+
private settleOutbound;
|
|
151
|
+
/** Put a refused post back where it was, so a later run can try again. */
|
|
152
|
+
private releaseClaim;
|
|
153
|
+
/**
|
|
154
|
+
* Atomically move the post to `publishing` if it is still in one of
|
|
155
|
+
* `from`. Returns false when another publisher got there first.
|
|
156
|
+
*
|
|
157
|
+
* The claim writes the row directly, so the caller's copy of the post keeps
|
|
158
|
+
* the old revision; {@link loadClaimedPost} re-reads it before anything is
|
|
159
|
+
* saved.
|
|
160
|
+
*
|
|
161
|
+
* TODO(happyvertical/smrt#3300): a `SocialPostCollection.claimForPublish`
|
|
162
|
+
* that returns the claimed post belongs next to
|
|
163
|
+
* `recordPublishSuccess`/`recordPublishFailure`; adopt it and drop the
|
|
164
|
+
* re-read.
|
|
165
|
+
*/
|
|
166
|
+
private claimForPublish;
|
|
167
|
+
/**
|
|
168
|
+
* Re-read a post the claim just moved to `publishing`, so the revision the
|
|
169
|
+
* outcome is saved against is the one the claim wrote. Saving the caller's
|
|
170
|
+
* copy instead always fails with `RUNTIME_REVISION_CONFLICT`.
|
|
171
|
+
*/
|
|
172
|
+
private loadClaimedPost;
|
|
173
|
+
/**
|
|
174
|
+
* Runs after the claim: every outcome is recorded on the post, and a
|
|
175
|
+
* failure to record never hides why the publish itself failed.
|
|
176
|
+
*/
|
|
177
|
+
private publishPostInTenant;
|
|
178
|
+
/**
|
|
179
|
+
* Record why a claimed post was not published and return that reason. The
|
|
180
|
+
* reason is logged first, so it survives even when recording it fails.
|
|
181
|
+
*/
|
|
182
|
+
private failClaimedPost;
|
|
183
|
+
/**
|
|
184
|
+
* Refresh the stats of published posts. With `options.tenantId`, only that
|
|
185
|
+
* organization's posts are read; organizations that may not publish right
|
|
186
|
+
* now (see {@link GarrulaHost.canPublish}) are skipped.
|
|
187
|
+
*/
|
|
188
|
+
syncAnalytics(postIds?: string[], options?: {
|
|
189
|
+
tenantId?: string | null;
|
|
190
|
+
}): Promise<AnalyticsSyncResult>;
|
|
191
|
+
private resolveTargetAccounts;
|
|
192
|
+
/**
|
|
193
|
+
* Narrow ready accounts to the requested ids. Every requested id must be one
|
|
194
|
+
* of the ready accounts, so a stale or foreign id is refused, not skipped.
|
|
195
|
+
*/
|
|
196
|
+
private selectAccounts;
|
|
197
|
+
private mergeHashtags;
|
|
198
|
+
private requireAccountId;
|
|
199
|
+
/**
|
|
200
|
+
* The tenant a request acts for: its own `tenantId`, else the tenant of the
|
|
201
|
+
* current context. A request naming a different tenant than the active
|
|
202
|
+
* context is refused rather than silently crossing tenants.
|
|
203
|
+
*/
|
|
204
|
+
private resolveRequestTenantId;
|
|
205
|
+
private normalizeTenantId;
|
|
206
|
+
private getPostAccount;
|
|
207
|
+
/**
|
|
208
|
+
* Build the account's adapter with a usable access token. A token that is
|
|
209
|
+
* expired or close to it (or of unknown age) is refreshed first and the new
|
|
210
|
+
* token set is saved on the account, so the next publish starts from it.
|
|
211
|
+
* With `force`, the token is refreshed even if it looks valid (the platform
|
|
212
|
+
* just rejected it); a token that cannot be refreshed then needs a
|
|
213
|
+
* reconnect.
|
|
214
|
+
*/
|
|
215
|
+
private openAdapter;
|
|
216
|
+
/**
|
|
217
|
+
* Return credentials whose access token can be used now: the stored ones,
|
|
218
|
+
* or a refreshed set that has been saved on the account.
|
|
219
|
+
*/
|
|
220
|
+
private ensureFreshCredentials;
|
|
221
|
+
private refreshCredentials;
|
|
222
|
+
/**
|
|
223
|
+
* Save a refreshed token set on the account (encrypted, tenant-scoped).
|
|
224
|
+
* `tokenExpiresAt` on the account means "the connection lapses then", so it
|
|
225
|
+
* is cleared for refreshable OAuth 2 tokens and moved forward for Threads.
|
|
226
|
+
* A failed save is logged and the fresh token is still used for this
|
|
227
|
+
* publish.
|
|
228
|
+
*/
|
|
229
|
+
private saveRefreshedCredentials;
|
|
230
|
+
/**
|
|
231
|
+
* Mark the account as needing a reconnect and return the error to throw.
|
|
232
|
+
* Provider error text is never shown: it can be cryptic, and the message
|
|
233
|
+
* says what to do.
|
|
234
|
+
*/
|
|
235
|
+
private requireReconnect;
|
|
236
|
+
private createAdapter;
|
|
237
|
+
private buildSocialConfig;
|
|
238
|
+
private publishWithAdapter;
|
|
239
|
+
private syncAnalyticsForPost;
|
|
240
|
+
}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { PlatformRates } from './outbound/rates.js';
|
|
2
|
+
import { GarrulaTargetPlatform } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Host-supplied configuration for Garrula. Everything here is a policy or
|
|
5
|
+
* wording choice that belongs to the application using the agent, not to the
|
|
6
|
+
* agent itself; the defaults are neutral and work for any tenant.
|
|
7
|
+
*/
|
|
8
|
+
export interface GarrulaConfig {
|
|
9
|
+
/**
|
|
10
|
+
* Platforms that receive a draft when a link post is created without
|
|
11
|
+
* explicit `targets`.
|
|
12
|
+
*/
|
|
13
|
+
storyLinkTargets: GarrulaTargetPlatform[];
|
|
14
|
+
/**
|
|
15
|
+
* Platforms that receive a draft when a video post is created without
|
|
16
|
+
* explicit `targets`.
|
|
17
|
+
*/
|
|
18
|
+
articleVideoTargets: GarrulaTargetPlatform[];
|
|
19
|
+
/**
|
|
20
|
+
* Publish mode used when an account records none. `dry_run` never reaches
|
|
21
|
+
* the platform, so an unconfigured account cannot post by accident.
|
|
22
|
+
*/
|
|
23
|
+
defaultPublishMode: string;
|
|
24
|
+
/** What a tenant is called in messages shown to the person posting. */
|
|
25
|
+
tenantLabel: string;
|
|
26
|
+
/** Where a person reconnects a social account, as shown in messages. */
|
|
27
|
+
socialSettingsLocation: string;
|
|
28
|
+
/** Refresh an OAuth 2 access token this long before it expires. */
|
|
29
|
+
accessTokenRefreshMarginMs: number;
|
|
30
|
+
/** Refresh a Threads long-lived token once it is this close to lapsing. */
|
|
31
|
+
threadsRefreshWindowMs: number;
|
|
32
|
+
/**
|
|
33
|
+
* Fallback publish prices per platform, in USD, used only when the adapter's
|
|
34
|
+
* publish result reports no cost. Defaults to X's published prices; TEMPORARY
|
|
35
|
+
* until the social SDK reports real costs (happyvertical.com#518).
|
|
36
|
+
*/
|
|
37
|
+
outboundRates: Record<string, PlatformRates>;
|
|
38
|
+
/** Monthly spend cap (USD) of a tenant that has not set its own. A cap always applies. */
|
|
39
|
+
outboundDefaultMonthlyCapUsd: number;
|
|
40
|
+
/** Longest post text a draft or edit may have. */
|
|
41
|
+
outboundMaxLength: number;
|
|
42
|
+
}
|
|
43
|
+
export declare const DEFAULT_GARRULA_CONFIG: Readonly<GarrulaConfig>;
|
|
44
|
+
/** Overlay the host's partial configuration on the neutral defaults. */
|
|
45
|
+
export declare function resolveGarrulaConfig(overrides?: Partial<GarrulaConfig>): GarrulaConfig;
|
|
46
|
+
/** Why a post was not sent because its tenant may not publish. */
|
|
47
|
+
export declare function tenantBlockedMessage(config: GarrulaConfig): string;
|
|
48
|
+
/** What the caller is told when no tenant can be resolved. */
|
|
49
|
+
export declare function tenantRequiredMessage(config: GarrulaConfig): string;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { AuthResult } from '@happyvertical/social';
|
|
2
|
+
import { SocialPlatformType } from '@happyvertical/smrt-social';
|
|
3
|
+
/**
|
|
4
|
+
* Social OAuth token upkeep for Garrula's publisher.
|
|
5
|
+
*
|
|
6
|
+
* TODO(happyvertical/sdk#1376): `@happyvertical/social` adapters should
|
|
7
|
+
* refresh expired tokens themselves (expiry margin, retry after a 401) and
|
|
8
|
+
* hand the refreshed token set to a callback for the caller to persist,
|
|
9
|
+
* Threads included. Until then Garrula decides when to refresh, refreshes
|
|
10
|
+
* Threads tokens itself, and persists every refreshed token set. Drop this
|
|
11
|
+
* module's refresh logic when that ships.
|
|
12
|
+
*/
|
|
13
|
+
/** Default for how long before expiry an access token is refreshed. */
|
|
14
|
+
export declare const ACCESS_TOKEN_REFRESH_MARGIN_MS: number;
|
|
15
|
+
/**
|
|
16
|
+
* Default window in which a Threads long-lived token (60 days) is refreshed:
|
|
17
|
+
* once it is this close to lapsing. Meta refuses to refresh a token younger than 24 hours, so a
|
|
18
|
+
* publish soon after connecting keeps the token it has.
|
|
19
|
+
*/
|
|
20
|
+
export declare const THREADS_REFRESH_WINDOW_MS: number;
|
|
21
|
+
export declare const THREADS_REFRESH_URL = "https://graph.threads.net/refresh_access_token";
|
|
22
|
+
export declare function platformLabel(platform: SocialPlatformType): string;
|
|
23
|
+
/** Where a person reconnects an account, unless the host says otherwise. */
|
|
24
|
+
export declare const DEFAULT_SOCIAL_SETTINGS_LOCATION = "Settings \u2192 Social";
|
|
25
|
+
/** What the poster sees when an account has to be connected again. */
|
|
26
|
+
export declare function reconnectMessage(platform: SocialPlatformType, settingsLocation?: string): string;
|
|
27
|
+
/**
|
|
28
|
+
* The account's sign-in can no longer be used or refreshed; a person has to
|
|
29
|
+
* reconnect it. The message is safe to show and never carries provider text.
|
|
30
|
+
*/
|
|
31
|
+
export declare class GarrulaReconnectError extends Error {
|
|
32
|
+
readonly platform: SocialPlatformType;
|
|
33
|
+
readonly code = "RECONNECT_REQUIRED";
|
|
34
|
+
constructor(platform: SocialPlatformType, settingsLocation?: string);
|
|
35
|
+
}
|
|
36
|
+
/** The provider rejected the sign-in (HTTP 401 or an auth error code). */
|
|
37
|
+
export declare function isAuthError(error: unknown): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* The provider refused the refresh grant itself (revoked consent,
|
|
40
|
+
* `invalid_grant`, a rotated-away X refresh token), as opposed to a network
|
|
41
|
+
* failure or an outage worth retrying later.
|
|
42
|
+
*/
|
|
43
|
+
export declare function isRefreshRejected(error: unknown): boolean;
|
|
44
|
+
export declare function parseExpiry(value: unknown): Date | null;
|
|
45
|
+
/** Which refresh flow an account's stored credentials support. */
|
|
46
|
+
export type RefreshKind = "oauth2" | "threads" | null;
|
|
47
|
+
export declare function refreshKind(platform: SocialPlatformType, credentials: Record<string, unknown>): RefreshKind;
|
|
48
|
+
/**
|
|
49
|
+
* The stored credentials with a refreshed token set merged in. Every other
|
|
50
|
+
* stored field (client id/secret, user id, auth type, scopes) is kept.
|
|
51
|
+
*/
|
|
52
|
+
export declare function mergeRefreshedCredentials(credentials: Record<string, unknown>, auth: AuthResult): Record<string, unknown>;
|
|
53
|
+
/**
|
|
54
|
+
* Refresh a Threads long-lived token. The upstream Threads adapter does not
|
|
55
|
+
* implement `refreshToken()` (TODO(happyvertical/sdk#1376)).
|
|
56
|
+
*/
|
|
57
|
+
export declare function refreshThreadsToken(accessToken: string, fetchImpl?: typeof fetch): Promise<AuthResult>;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { Garrula, GARRULA_ACCOUNT_INACTIVE_MESSAGE, GARRULA_POST_BUSY_MESSAGE, GARRULA_TENANT_BLOCKED_MESSAGE, setGarrulaConfig, setGarrulaHost, type GarrulaOptions, } from './Garrula.js';
|
|
2
|
+
export type { AnalyticsSyncResult, ArticleVideoPostRequest, GarrulaCollections, GarrulaHost, GarrulaListOptions, GarrulaPostAnalytics, GarrulaPostType, GarrulaSocialAccount, GarrulaSocialAdapter, GarrulaSocialConfig, GarrulaSocialPost, GarrulaTargetPlatform, GarrulaClaimDatabase, PublishPostOptions, PublishPostResult, SocialFactory, StoryLinkPostRequest, } from './types.js';
|
|
3
|
+
export { DEFAULT_GARRULA_CONFIG, resolveGarrulaConfig, type GarrulaConfig, } from './config.js';
|
|
4
|
+
export { GarrulaReconnectError, reconnectMessage } from './credentials.js';
|
|
5
|
+
export { GarrulaOutboundPost, GarrulaOutboundPostCollection, GarrulaOutboundSettings, GarrulaOutboundSettingsCollection, GarrulaSourceEvent, GarrulaSourceEventCollection, GarrulaSpendEntry, GarrulaSpendEntryCollection, } from './outbound/models.js';
|
|
6
|
+
export { DEFAULT_OUTBOUND_RATES, FALLBACK_X_RATES, type PlatformRates, } from './outbound/rates.js';
|
|
7
|
+
export { createOutboundStores, OutboundService, type OutboundStores } from './outbound/service.js';
|
|
8
|
+
export { DEFAULT_AUTONOMY, DEFAULT_SOURCE_RULES } from './outbound/sources.js';
|
|
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';
|