@chaouidz/postpilot 1.1.0 → 1.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/README.md +103 -87
- package/bin/postpilot.mjs +292 -285
- package/package.json +27 -27
- package/src/client.mjs +168 -146
package/README.md
CHANGED
|
@@ -1,87 +1,103 @@
|
|
|
1
|
-
# PostPilot CLI
|
|
2
|
-
|
|
3
|
-
Thin Node.js 20+ client for the same approval-based API used by MCP. No bundled server or provider credentials. Public docs: https://postpilot.yktis.com/docs.
|
|
4
|
-
|
|
5
|
-
Install
|
|
6
|
-
Without a global install: `npx
|
|
7
|
-
|
|
8
|
-
The original versioned download remains available: `npm install -g https://postpilot.yktis.com/downloads/postpilot-1.0.0.tgz`.
|
|
9
|
-
For source review: run `npm pack` in `packages/cli`, then `npm install -g ./chaouidz-postpilot-1.1.
|
|
10
|
-
|
|
11
|
-
Python, PHP and other languages can call the HTTP API directly or invoke this CLI as a subprocess. npm distributes the Node.js CLI; it does not require your application to use JavaScript. There is no separate pip or Composer package.
|
|
12
|
-
|
|
13
|
-
##
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
postpilot
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
1
|
+
# PostPilot CLI
|
|
2
|
+
|
|
3
|
+
Thin Node.js 20+ client for the same approval-based API used by MCP. No bundled server or provider credentials. Public docs: https://postpilot.yktis.com/docs.
|
|
4
|
+
|
|
5
|
+
Install this patch: `npm install -g https://postpilot.yktis.com/downloads/chaouidz-postpilot-1.1.1.tgz`, then run `postpilot help`.
|
|
6
|
+
Without a global install: `npx --package=https://postpilot.yktis.com/downloads/chaouidz-postpilot-1.1.1.tgz postpilot help`.
|
|
7
|
+
Registry package: https://www.npmjs.com/package/@chaouidz/postpilot. npm currently has 1.1.0; publishing 1.1.1 still requires a completed npm browser-authentication session. The versioned archive above contains the tested 1.1.1 patch.
|
|
8
|
+
The original versioned download remains available: `npm install -g https://postpilot.yktis.com/downloads/postpilot-1.0.0.tgz`.
|
|
9
|
+
For source review: run `npm pack` in `packages/cli`, then `npm install -g ./chaouidz-postpilot-1.1.1.tgz`. Packing does not publish to npm.
|
|
10
|
+
|
|
11
|
+
Python, PHP and other languages can call the HTTP API directly or invoke this CLI as a subprocess. npm distributes the Node.js CLI; it does not require your application to use JavaScript. There is no separate pip or Composer package.
|
|
12
|
+
|
|
13
|
+
## Runtime requirement
|
|
14
|
+
|
|
15
|
+
Node.js 20 or newer is required on Windows, macOS and Linux. Install the current Node.js LTS from https://nodejs.org/en/download, reopen your terminal, then check `node --version`.
|
|
16
|
+
|
|
17
|
+
## Capacity and retry errors
|
|
18
|
+
|
|
19
|
+
`CAPACITY_EXCEEDED` exits with code 8, is not retryable unchanged and has no retryAfter. It names the account, calendar day, cap, excess items and timezone. Move the named items to another account day. Only genuine throttling uses RATE_LIMITED (exit 6) and Retry-After.
|
|
20
|
+
|
|
21
|
+
The server counts days in the account metadata timezone, falling back to the owner profile timezone. It does not group by UTC or by the machine timezone. An ISO timestamp's numeric offset determines the instant, not the account's quota boundary. Plan timezone overrides do not change account/day capacity.
|
|
22
|
+
|
|
23
|
+
During rolling upgrades, the CLI recognizes an old RATE_LIMITED response only when it explicitly identifies ACCOUNT_CALENDAR_DAY capacity. Existing single-post commands and MCP tool names remain supported.
|
|
24
|
+
|
|
25
|
+
## Login and secrets
|
|
26
|
+
|
|
27
|
+
Create a token in PostPilot Settings with `social:read`, `social:write`, an expiry and only the channel you intend to use. Keep trusted automation disabled for human approval.
|
|
28
|
+
|
|
29
|
+
Run `postpilot login` and paste the token at the hidden prompt. Windows uses Credential Manager; macOS uses Keychain via `security`; Linux uses Secret Service via `secret-tool` (install your distribution's libsecret tools and unlock a desktop keychain). A missing/locked keychain fails explicitly. There is no plaintext-file fallback. Piped login requires `--token-stdin` and still stores in the OS keychain.
|
|
30
|
+
|
|
31
|
+
CI/cron: configure `POSTPILOT_TOKEN` through the runtime's secret store and use `--non-interactive --json`. Do not put it in a shell history, command argument, checked-in config or prompt. `POSTPILOT_URL` or `--url` selects an API origin; remote connections require HTTPS. Redirects are not followed with credentials.
|
|
32
|
+
|
|
33
|
+
`logout` removes only the local keychain entry. Revoke the token in PostPilot Settings to invalidate all copies; already scheduled posts must be cancelled separately.
|
|
34
|
+
|
|
35
|
+
## Schedule a Short to ONE named channel
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
postpilot login
|
|
39
|
+
postpilot whoami --json
|
|
40
|
+
postpilot accounts list --json
|
|
41
|
+
postpilot upload short.mp4 --idempotency-key my-short-import-001 --json
|
|
42
|
+
# Copy the returned uploadSessionId as UPLOAD_ID below.
|
|
43
|
+
postpilot draft create --account "My Tutorials" --asset UPLOAD_ID --title "My Short" --privacy private --idempotency-key my-short-draft-001 --json
|
|
44
|
+
# Copy the returned id as DRAFT_ID. Pick a FUTURE ISO timestamp with an offset.
|
|
45
|
+
postpilot schedule --post DRAFT_ID --account "My Tutorials" --at "2027-01-10T09:00:00+01:00" --timezone Europe/Zurich --dry-run --json
|
|
46
|
+
postpilot schedule --post DRAFT_ID --account "My Tutorials" --at "2027-01-10T09:00:00+01:00" --timezone Europe/Zurich --idempotency-key my-short-schedule-001 --json
|
|
47
|
+
# Exit code 5 means PENDING_APPROVAL. Open the returned approvalUrl while signed in.
|
|
48
|
+
# Review the exact single account, private visibility and time, then approve.
|
|
49
|
+
postpilot status --post DRAFT_ID --json
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Exact IDs are preferred. Names must uniquely match one visible channel; an ambiguous name fails. No platform-wide broadcast fallback. A scheduled/queued result does not mean published. Dry-run validates local inputs; dynamic provider capacity and plan allowances are not reserved. Publication is queued only after browser approval, unless this token explicitly enables trusted automation.
|
|
53
|
+
|
|
54
|
+
Other commands: `assets list [--limit 1-100] [--cursor CURSOR]`, `publish --post ID --account ID [--dry-run]`, `cancel --post ID`, `prepare-video input.mov --output compatible.mp4` (offline, requires ffmpeg, never overwrites an existing file). Set privacy on the draft before scheduling; the approval payload and stored draft determine publication privacy. Image/raw uploads accept PNG up to 20 MiB and MP4 up to 90 MiB. Preparation may produce larger files, which the raw upload API will reject.
|
|
55
|
+
|
|
56
|
+
All commands accept `--json` and `--non-interactive`. `--account`, `--at`, `--privacy` and `--dry-run` apply where relevant; help lists the full syntax. Keep an idempotency key identical on retries of one operation. Inspect failed/ambiguous delivery before attempting a new publication.
|
|
57
|
+
|
|
58
|
+
## Schedule a plan with one approval
|
|
59
|
+
|
|
60
|
+
```json
|
|
61
|
+
[
|
|
62
|
+
{
|
|
63
|
+
"video": "videos/first.mp4",
|
|
64
|
+
"title": "First video",
|
|
65
|
+
"description": "My description",
|
|
66
|
+
"tags": ["tutorial", "workflow"],
|
|
67
|
+
"scheduledAt": "2027-02-01T09:00:00+01:00",
|
|
68
|
+
"account": "My Tutorials"
|
|
69
|
+
}
|
|
70
|
+
]
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Paths are relative to the JSON file. Use an array or `{ "posts": [...] }`, with 1-100 items. Each account must be an exact ID or uniquely named account visible to your token. `--account` supplies a fallback for items without an account.
|
|
74
|
+
|
|
75
|
+
```sh
|
|
76
|
+
postpilot schedule --file plan.json
|
|
77
|
+
# Default is read-only. Review the preview, then explicitly prepare the plan:
|
|
78
|
+
postpilot schedule --file plan.json --yes
|
|
79
|
+
# Open the single returned approvalUrl and review every video/account/time.
|
|
80
|
+
postpilot status --approval APPROVAL_ID --json
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
`--yes` prepares uploads and drafts; the batch still needs human approval. It never proves approval or bypasses it. `--yes --dry-run` is rejected. Existing single-post scheduling commands retain their behavior for compatibility.
|
|
84
|
+
|
|
85
|
+
The CLI validates every file and the server calendar-day cap before any upload. Default capacity is 10 posts per account per local calendar day, configurable by plan. A rejected plan identifies the account/day and item numbers to move. Existing billing entitlements and provider quotas still apply. Twenty posts over ten days fit the default daily cap on an eligible plan.
|
|
86
|
+
|
|
87
|
+
MP4 files are limited to 90 MiB each; use `prepare-video` first for other formats. Only HTTP 429 uploads retry, at most five retries, honoring seconds or HTTP-date Retry-After. A wait over five minutes stops with an actionable error. Ambiguous network failures never retry automatically.
|
|
88
|
+
|
|
89
|
+
Stable per-item keys and a `.postpilot-*.json` checkpoint beside the plan allow interrupted runs to resume without uploading or creating drafts again. Checkpoints contain hashes, post IDs and an approval link, never credentials or file contents. Do not edit them. Reusing an explicit `--idempotency-key` for changed content is rejected. Inspect expired/failed/partly failed batch results and prepare only intended retries with a new plan key.
|
|
90
|
+
|
|
91
|
+
YouTube defaults to private visibility; optional `privacy` selects `private`, `unlisted` or `public`. Tags become YouTube keywords and are appended as hashtags to other platform captions. TikTok items must include explicit `tiktokSettings` with `privacy`, `allowComments`, `allowDuet` and `allowStitch`; creator policy is rechecked during approval. Pinterest requires the target board in `pinterestSettings.boardIds`. Existing platform settings can be supplied through `facebookSettings`, `instagramPostType` and `instagramCaption`.
|
|
92
|
+
|
|
93
|
+
## Exit codes
|
|
94
|
+
|
|
95
|
+
0 success, 2 invalid usage/input, 3 authentication or scope failure, 4 not found, 5 pending browser approval, 6 rate limited, 7 provider/network/server failure, 8 capacity exceeded. JSON errors include stable `code`, `message`, and available request/retry details. Honor Retry-After; provider quotas are not unlimited or guaranteed.
|
|
96
|
+
|
|
97
|
+
## Tests
|
|
98
|
+
|
|
99
|
+
`npm test` uses a local mock server, fake account IDs and fixture media. Native keychain integration is opt-in with `POSTPILOT_TEST_NATIVE_KEYCHAIN=true`; it creates and removes only a unique fixture credential. No live publishing or real social-account calls occur.
|
|
100
|
+
|
|
101
|
+
## Resume an interrupted batch
|
|
102
|
+
|
|
103
|
+
Run the same `postpilot schedule --file plan.json --yes` command with unchanged files and arguments. The credential-free checkpoint retains completed draft IDs; stable upload, draft and batch idempotency keys recover a server commit whose response was lost. Inspect expired or partly failed approvals before intentionally preparing new work. Never remove a checkpoint or change keys merely to retry an ambiguous operation.
|
package/bin/postpilot.mjs
CHANGED
|
@@ -1,285 +1,292 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { execFile } from "node:child_process";
|
|
3
|
-
import { promisify } from "node:util";
|
|
4
|
-
import { Client, CliError, EXIT_CODES } from "../src/client.mjs";
|
|
5
|
-
import { schedulePlan } from "../src/plan.mjs";
|
|
6
|
-
import { keychain } from "../src/keychain.mjs";
|
|
7
|
-
const args = process.argv.slice(2),
|
|
8
|
-
options = {},
|
|
9
|
-
positionals = [];
|
|
10
|
-
const boolean = new Set(["json", "dry-run", "non-interactive", "token-stdin", "help", "yes"]);
|
|
11
|
-
const valued = new Set([
|
|
12
|
-
"url",
|
|
13
|
-
"file",
|
|
14
|
-
"account",
|
|
15
|
-
"at",
|
|
16
|
-
"privacy",
|
|
17
|
-
"post",
|
|
18
|
-
"approval",
|
|
19
|
-
"asset",
|
|
20
|
-
"title",
|
|
21
|
-
"content",
|
|
22
|
-
"output",
|
|
23
|
-
"idempotency-key",
|
|
24
|
-
"timezone",
|
|
25
|
-
"limit",
|
|
26
|
-
"cursor",
|
|
27
|
-
]);
|
|
28
|
-
function required(name) {
|
|
29
|
-
if (!options[name]) throw new CliError(`--${name} is required.`);
|
|
30
|
-
return options[name];
|
|
31
|
-
}
|
|
32
|
-
function output(value) {
|
|
33
|
-
console.log(JSON.stringify(value, null, options.json ? 0 : 2));
|
|
34
|
-
if (value?.status === "PENDING_APPROVAL") process.exitCode = 5;
|
|
35
|
-
}
|
|
36
|
-
async function tokenInput() {
|
|
37
|
-
if (!process.stdin.isTTY) {
|
|
38
|
-
if (!options["token-stdin"])
|
|
39
|
-
throw new CliError("Use --token-stdin for piped login, or paste interactively.");
|
|
40
|
-
let value = "";
|
|
41
|
-
for await (const chunk of process.stdin) {
|
|
42
|
-
value += chunk;
|
|
43
|
-
if (value.length > 1024) throw new CliError("Invalid token length.");
|
|
44
|
-
}
|
|
45
|
-
return value.trim();
|
|
46
|
-
}
|
|
47
|
-
if (options["non-interactive"])
|
|
48
|
-
throw new CliError("Non-interactive login requires --token-stdin.");
|
|
49
|
-
process.stderr.write("Paste your PostPilot token (hidden): ");
|
|
50
|
-
return new Promise((resolve, reject) => {
|
|
51
|
-
let value = "";
|
|
52
|
-
process.stdin.setRawMode(true);
|
|
53
|
-
process.stdin.resume();
|
|
54
|
-
const done = (error) => {
|
|
55
|
-
process.stdin.setRawMode(false);
|
|
56
|
-
process.stdin.pause();
|
|
57
|
-
process.stdin.off("data", onData);
|
|
58
|
-
process.stderr.write("\n");
|
|
59
|
-
error ? reject(error) : resolve(value.trim());
|
|
60
|
-
};
|
|
61
|
-
const onData = (chunk) => {
|
|
62
|
-
for (const char of chunk.toString()) {
|
|
63
|
-
if (char === "\r" || char === "\n") return done();
|
|
64
|
-
if (char === "\u0003") return done(new CliError("Login cancelled."));
|
|
65
|
-
if (char === "\u007f" || char === "\b") value = value.slice(0, -1);
|
|
66
|
-
else value += char;
|
|
67
|
-
if (value.length > 512) return done(new CliError("Invalid token length."));
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
process.stdin.on("data", onData);
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
try {
|
|
74
|
-
for (let index = 0; index < args.length; index++) {
|
|
75
|
-
const value = args[index];
|
|
76
|
-
if (!value.startsWith("--")) {
|
|
77
|
-
positionals.push(value);
|
|
78
|
-
continue;
|
|
79
|
-
}
|
|
80
|
-
const name = value.slice(2);
|
|
81
|
-
if (boolean.has(name)) options[name] = true;
|
|
82
|
-
else if (valued.has(name) && args[index + 1] && !args[index + 1].startsWith("--"))
|
|
83
|
-
options[name] = args[++index];
|
|
84
|
-
else throw new CliError(`Unknown or incomplete option --${name}.`);
|
|
85
|
-
}
|
|
86
|
-
const [command, subcommand] = positionals;
|
|
87
|
-
const origin = new URL(options.url || process.env.POSTPILOT_URL || "https://postpilot.yktis.com")
|
|
88
|
-
.origin;
|
|
89
|
-
if (!command || command === "help" || options.help) {
|
|
90
|
-
output({
|
|
91
|
-
usage: "postpilot <command> [--json] [--non-interactive]",
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
"
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
"
|
|
100
|
-
"
|
|
101
|
-
"
|
|
102
|
-
"
|
|
103
|
-
"
|
|
104
|
-
"
|
|
105
|
-
"
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
"--
|
|
109
|
-
"--
|
|
110
|
-
"--
|
|
111
|
-
"--
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
"--
|
|
115
|
-
"--
|
|
116
|
-
"--
|
|
117
|
-
"--
|
|
118
|
-
"--
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
"
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
"
|
|
144
|
-
"-
|
|
145
|
-
|
|
146
|
-
"-
|
|
147
|
-
"
|
|
148
|
-
"
|
|
149
|
-
"
|
|
150
|
-
"
|
|
151
|
-
"
|
|
152
|
-
|
|
153
|
-
"
|
|
154
|
-
"
|
|
155
|
-
"
|
|
156
|
-
"
|
|
157
|
-
"
|
|
158
|
-
"
|
|
159
|
-
"
|
|
160
|
-
"
|
|
161
|
-
"
|
|
162
|
-
"
|
|
163
|
-
"
|
|
164
|
-
"
|
|
165
|
-
"
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
}
|
|
191
|
-
} else if (command === "
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
);
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
);
|
|
218
|
-
else if (command === "
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
)
|
|
251
|
-
|
|
252
|
-
if (
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
)
|
|
268
|
-
);
|
|
269
|
-
else if (command === "
|
|
270
|
-
output(
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { execFile } from "node:child_process";
|
|
3
|
+
import { promisify } from "node:util";
|
|
4
|
+
import { Client, CliError, EXIT_CODES } from "../src/client.mjs";
|
|
5
|
+
import { schedulePlan } from "../src/plan.mjs";
|
|
6
|
+
import { keychain } from "../src/keychain.mjs";
|
|
7
|
+
const args = process.argv.slice(2),
|
|
8
|
+
options = {},
|
|
9
|
+
positionals = [];
|
|
10
|
+
const boolean = new Set(["json", "dry-run", "non-interactive", "token-stdin", "help", "yes"]);
|
|
11
|
+
const valued = new Set([
|
|
12
|
+
"url",
|
|
13
|
+
"file",
|
|
14
|
+
"account",
|
|
15
|
+
"at",
|
|
16
|
+
"privacy",
|
|
17
|
+
"post",
|
|
18
|
+
"approval",
|
|
19
|
+
"asset",
|
|
20
|
+
"title",
|
|
21
|
+
"content",
|
|
22
|
+
"output",
|
|
23
|
+
"idempotency-key",
|
|
24
|
+
"timezone",
|
|
25
|
+
"limit",
|
|
26
|
+
"cursor",
|
|
27
|
+
]);
|
|
28
|
+
function required(name) {
|
|
29
|
+
if (!options[name]) throw new CliError(`--${name} is required.`);
|
|
30
|
+
return options[name];
|
|
31
|
+
}
|
|
32
|
+
function output(value) {
|
|
33
|
+
console.log(JSON.stringify(value, null, options.json ? 0 : 2));
|
|
34
|
+
if (value?.status === "PENDING_APPROVAL") process.exitCode = 5;
|
|
35
|
+
}
|
|
36
|
+
async function tokenInput() {
|
|
37
|
+
if (!process.stdin.isTTY) {
|
|
38
|
+
if (!options["token-stdin"])
|
|
39
|
+
throw new CliError("Use --token-stdin for piped login, or paste interactively.");
|
|
40
|
+
let value = "";
|
|
41
|
+
for await (const chunk of process.stdin) {
|
|
42
|
+
value += chunk;
|
|
43
|
+
if (value.length > 1024) throw new CliError("Invalid token length.");
|
|
44
|
+
}
|
|
45
|
+
return value.trim();
|
|
46
|
+
}
|
|
47
|
+
if (options["non-interactive"])
|
|
48
|
+
throw new CliError("Non-interactive login requires --token-stdin.");
|
|
49
|
+
process.stderr.write("Paste your PostPilot token (hidden): ");
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
let value = "";
|
|
52
|
+
process.stdin.setRawMode(true);
|
|
53
|
+
process.stdin.resume();
|
|
54
|
+
const done = (error) => {
|
|
55
|
+
process.stdin.setRawMode(false);
|
|
56
|
+
process.stdin.pause();
|
|
57
|
+
process.stdin.off("data", onData);
|
|
58
|
+
process.stderr.write("\n");
|
|
59
|
+
error ? reject(error) : resolve(value.trim());
|
|
60
|
+
};
|
|
61
|
+
const onData = (chunk) => {
|
|
62
|
+
for (const char of chunk.toString()) {
|
|
63
|
+
if (char === "\r" || char === "\n") return done();
|
|
64
|
+
if (char === "\u0003") return done(new CliError("Login cancelled."));
|
|
65
|
+
if (char === "\u007f" || char === "\b") value = value.slice(0, -1);
|
|
66
|
+
else value += char;
|
|
67
|
+
if (value.length > 512) return done(new CliError("Invalid token length."));
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
process.stdin.on("data", onData);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
for (let index = 0; index < args.length; index++) {
|
|
75
|
+
const value = args[index];
|
|
76
|
+
if (!value.startsWith("--")) {
|
|
77
|
+
positionals.push(value);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const name = value.slice(2);
|
|
81
|
+
if (boolean.has(name)) options[name] = true;
|
|
82
|
+
else if (valued.has(name) && args[index + 1] && !args[index + 1].startsWith("--"))
|
|
83
|
+
options[name] = args[++index];
|
|
84
|
+
else throw new CliError(`Unknown or incomplete option --${name}.`);
|
|
85
|
+
}
|
|
86
|
+
const [command, subcommand] = positionals;
|
|
87
|
+
const origin = new URL(options.url || process.env.POSTPILOT_URL || "https://postpilot.yktis.com")
|
|
88
|
+
.origin;
|
|
89
|
+
if (!command || command === "help" || options.help) {
|
|
90
|
+
output({
|
|
91
|
+
usage: "postpilot <command> [--json] [--non-interactive]",
|
|
92
|
+
requires: {
|
|
93
|
+
node: ">=20",
|
|
94
|
+
operatingSystems: ["Windows", "macOS", "Linux"],
|
|
95
|
+
installRuntime:
|
|
96
|
+
"Install current Node.js LTS from https://nodejs.org/en/download, then reopen your terminal.",
|
|
97
|
+
},
|
|
98
|
+
commands: [
|
|
99
|
+
"login [--token-stdin]",
|
|
100
|
+
"whoami",
|
|
101
|
+
"logout",
|
|
102
|
+
"accounts list",
|
|
103
|
+
"assets list",
|
|
104
|
+
"upload <file>",
|
|
105
|
+
"prepare-video <file> --output <file>",
|
|
106
|
+
"draft create --account <id-or-name> --asset <id>",
|
|
107
|
+
"schedule --file <plan.json> [--account <id-or-name>] [--dry-run | --yes]",
|
|
108
|
+
"schedule --post <id> --account <id-or-name> --at <ISO-offset>",
|
|
109
|
+
"publish --post <id> --account <id-or-name>",
|
|
110
|
+
"status --post <id>",
|
|
111
|
+
"cancel --post <id>",
|
|
112
|
+
],
|
|
113
|
+
flags: [
|
|
114
|
+
"--file",
|
|
115
|
+
"--yes",
|
|
116
|
+
"--account",
|
|
117
|
+
"--at",
|
|
118
|
+
"--privacy",
|
|
119
|
+
"--dry-run",
|
|
120
|
+
"--json",
|
|
121
|
+
"--non-interactive",
|
|
122
|
+
"--idempotency-key",
|
|
123
|
+
"--timezone",
|
|
124
|
+
"--url",
|
|
125
|
+
],
|
|
126
|
+
exitCodes: {
|
|
127
|
+
success: 0,
|
|
128
|
+
usage: 2,
|
|
129
|
+
auth: 3,
|
|
130
|
+
notFound: 4,
|
|
131
|
+
approvalPending: 5,
|
|
132
|
+
rateLimited: 6,
|
|
133
|
+
providerOrNetwork: 7,
|
|
134
|
+
capacityExceeded: 8,
|
|
135
|
+
},
|
|
136
|
+
docs: "https://postpilot.yktis.com/cli",
|
|
137
|
+
});
|
|
138
|
+
} else if (command === "prepare-video") {
|
|
139
|
+
const input = subcommand;
|
|
140
|
+
if (!input) throw new CliError("Choose an input video.");
|
|
141
|
+
const target = required("output");
|
|
142
|
+
const ffargs = [
|
|
143
|
+
"-hide_banner",
|
|
144
|
+
"-loglevel",
|
|
145
|
+
"error",
|
|
146
|
+
"-n",
|
|
147
|
+
"-threads",
|
|
148
|
+
"2",
|
|
149
|
+
"-filter_threads",
|
|
150
|
+
"1",
|
|
151
|
+
"-i",
|
|
152
|
+
input,
|
|
153
|
+
"-map",
|
|
154
|
+
"0:v:0",
|
|
155
|
+
"-map",
|
|
156
|
+
"0:a?",
|
|
157
|
+
"-vf",
|
|
158
|
+
"scale=w=min(1920\\,iw):h=min(1920\\,ih):force_original_aspect_ratio=decrease:force_divisible_by=2,format=yuv420p,fps=30",
|
|
159
|
+
"-c:v",
|
|
160
|
+
"libx264",
|
|
161
|
+
"-crf",
|
|
162
|
+
"20",
|
|
163
|
+
"-pix_fmt",
|
|
164
|
+
"yuv420p",
|
|
165
|
+
"-c:a",
|
|
166
|
+
"aac",
|
|
167
|
+
"-ar",
|
|
168
|
+
"48000",
|
|
169
|
+
"-ac",
|
|
170
|
+
"2",
|
|
171
|
+
"-movflags",
|
|
172
|
+
"+faststart",
|
|
173
|
+
target,
|
|
174
|
+
];
|
|
175
|
+
if (options["dry-run"]) output({ dryRun: true, command: "ffmpeg", arguments: ffargs });
|
|
176
|
+
else {
|
|
177
|
+
try {
|
|
178
|
+
await promisify(execFile)("ffmpeg", ffargs, {
|
|
179
|
+
timeout: 180000,
|
|
180
|
+
maxBuffer: 1024 * 1024,
|
|
181
|
+
windowsHide: true,
|
|
182
|
+
});
|
|
183
|
+
} catch {
|
|
184
|
+
throw new CliError(
|
|
185
|
+
"Video preparation failed. Install ffmpeg, choose a fresh output path and check the input.",
|
|
186
|
+
"INTERNAL_FAILURE"
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
output({ output: target, uploaded: false });
|
|
190
|
+
}
|
|
191
|
+
} else if (command === "logout") {
|
|
192
|
+
await keychain("delete", origin);
|
|
193
|
+
output({
|
|
194
|
+
loggedOut: true,
|
|
195
|
+
message:
|
|
196
|
+
"Local credential removed. Revoke the token in Settings to invalidate it everywhere.",
|
|
197
|
+
});
|
|
198
|
+
} else if (command === "login") {
|
|
199
|
+
const token = await tokenInput();
|
|
200
|
+
const client = new Client(origin, token);
|
|
201
|
+
await client.tool("social_get_profile");
|
|
202
|
+
await keychain("set", origin, token);
|
|
203
|
+
output({ loggedIn: true, storage: "OS keychain", origin });
|
|
204
|
+
} else {
|
|
205
|
+
const client = new Client(
|
|
206
|
+
origin,
|
|
207
|
+
process.env.POSTPILOT_TOKEN || (await keychain("get", origin).catch(() => ""))
|
|
208
|
+
);
|
|
209
|
+
const key = options["idempotency-key"] ? { idempotencyKey: options["idempotency-key"] } : {};
|
|
210
|
+
const common = {
|
|
211
|
+
...key,
|
|
212
|
+
...(options.timezone ? { timezone: options.timezone } : {}),
|
|
213
|
+
...(options["dry-run"] ? { dryRun: true } : {}),
|
|
214
|
+
};
|
|
215
|
+
if (command === "whoami") output(await client.tool("social_get_profile"));
|
|
216
|
+
else if (command === "accounts" && subcommand === "list")
|
|
217
|
+
output(await client.tool("accounts_list"));
|
|
218
|
+
else if (command === "assets" && subcommand === "list")
|
|
219
|
+
output(
|
|
220
|
+
await client.tool("assets_list", {
|
|
221
|
+
...(options.limit ? { limit: Number(options.limit) } : {}),
|
|
222
|
+
...(options.cursor ? { cursor: options.cursor } : {}),
|
|
223
|
+
})
|
|
224
|
+
);
|
|
225
|
+
else if (command === "upload") {
|
|
226
|
+
if (!subcommand) throw new CliError("Choose a PNG or MP4 file.");
|
|
227
|
+
if (options["dry-run"]) output({ dryRun: true, file: subcommand, uploaded: false });
|
|
228
|
+
else output(await client.upload(subcommand, options["idempotency-key"]));
|
|
229
|
+
} else if (command === "draft" && subcommand === "create") {
|
|
230
|
+
const account = await client.account(required("account"));
|
|
231
|
+
const post = {
|
|
232
|
+
content: options.content || "",
|
|
233
|
+
videoTitle: options.title || "",
|
|
234
|
+
youtubePrivacyStatus: options.privacy || "private",
|
|
235
|
+
platformTargets: [account.platform],
|
|
236
|
+
selectedAccountIds: [account.id],
|
|
237
|
+
...(options.asset
|
|
238
|
+
? {
|
|
239
|
+
uploadSessionId: options.asset,
|
|
240
|
+
mediaType: account.platform === "YOUTUBE" ? "VIDEO_SHORT" : "VIDEO",
|
|
241
|
+
}
|
|
242
|
+
: {}),
|
|
243
|
+
};
|
|
244
|
+
if (options["dry-run"]) output({ dryRun: true, post, created: false });
|
|
245
|
+
else output(await client.tool("post_create_draft", { post, ...key }));
|
|
246
|
+
} else if (command === "schedule" && options.file) {
|
|
247
|
+
if (options.post || options.at) throw new CliError("Use --file or --post/--at, not both.");
|
|
248
|
+
output(await schedulePlan(client, options.file, options));
|
|
249
|
+
} else if (["schedule", "publish"].includes(command)) {
|
|
250
|
+
const account = await client.account(required("account"));
|
|
251
|
+
const postId = required("post");
|
|
252
|
+
if (
|
|
253
|
+
command === "schedule" &&
|
|
254
|
+
(!options.at ||
|
|
255
|
+
!/T.*(?:Z|[+-]\d{2}:\d{2})$/.test(options.at) ||
|
|
256
|
+
!Number.isFinite(Date.parse(options.at)))
|
|
257
|
+
)
|
|
258
|
+
throw new CliError("--at requires an ISO timestamp with Z or a numeric offset.");
|
|
259
|
+
if (options.privacy)
|
|
260
|
+
throw new CliError("Set privacy when creating the draft; review it in the approval page.");
|
|
261
|
+
output(
|
|
262
|
+
await client.tool(command === "schedule" ? "post_schedule" : "post_publish", {
|
|
263
|
+
postId,
|
|
264
|
+
accountIds: [account.id],
|
|
265
|
+
...(command === "schedule" ? { scheduledAt: options.at } : {}),
|
|
266
|
+
...common,
|
|
267
|
+
})
|
|
268
|
+
);
|
|
269
|
+
} else if (command === "status")
|
|
270
|
+
output(
|
|
271
|
+
await client.tool(
|
|
272
|
+
options.approval ? "approval_status" : "post_status",
|
|
273
|
+
options.approval ? { approvalId: options.approval } : { postId: required("post") }
|
|
274
|
+
)
|
|
275
|
+
);
|
|
276
|
+
else if (command === "cancel")
|
|
277
|
+
output(await client.tool("post_cancel", { postId: required("post"), ...key }));
|
|
278
|
+
else throw new CliError("Unknown command. Run postpilot help.");
|
|
279
|
+
}
|
|
280
|
+
} catch (error) {
|
|
281
|
+
const code = error instanceof CliError ? error.code : "INTERNAL_FAILURE";
|
|
282
|
+
const message =
|
|
283
|
+
error instanceof CliError
|
|
284
|
+
? error.message
|
|
285
|
+
: "The operation failed. Check your input and OS keychain, then try again.";
|
|
286
|
+
console.error(
|
|
287
|
+
JSON.stringify({
|
|
288
|
+
error: { code, message, ...(error instanceof CliError ? error.details : {}) },
|
|
289
|
+
})
|
|
290
|
+
);
|
|
291
|
+
process.exitCode = EXIT_CODES[code] || 7;
|
|
292
|
+
}
|
package/package.json
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@chaouidz/postpilot",
|
|
3
|
-
"version": "1.1.
|
|
4
|
-
"description": "PostPilot CLI for scoped, approval-based social publishing and
|
|
5
|
-
"type": "module",
|
|
6
|
-
"bin": {
|
|
7
|
-
"postpilot": "bin/postpilot.mjs"
|
|
8
|
-
},
|
|
9
|
-
"files": [
|
|
10
|
-
"bin",
|
|
11
|
-
"src",
|
|
12
|
-
"README.md"
|
|
13
|
-
],
|
|
14
|
-
"engines": {
|
|
15
|
-
"node": ">=20"
|
|
16
|
-
},
|
|
17
|
-
"license": "UNLICENSED",
|
|
18
|
-
"repository": {
|
|
19
|
-
"type": "git",
|
|
20
|
-
"url": "https://github.com/yktis/postpilot.git",
|
|
21
|
-
"directory": "packages/cli"
|
|
22
|
-
},
|
|
23
|
-
"homepage": "https://postpilot.yktis.com/cli",
|
|
24
|
-
"scripts": {
|
|
25
|
-
"test": "node --test test/*.test.mjs"
|
|
26
|
-
}
|
|
27
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@chaouidz/postpilot",
|
|
3
|
+
"version": "1.1.1",
|
|
4
|
+
"description": "PostPilot CLI for scoped, approval-based social publishing and batch scheduling",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"postpilot": "bin/postpilot.mjs"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin",
|
|
11
|
+
"src",
|
|
12
|
+
"README.md"
|
|
13
|
+
],
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=20"
|
|
16
|
+
},
|
|
17
|
+
"license": "UNLICENSED",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://github.com/yktis/postpilot.git",
|
|
21
|
+
"directory": "packages/cli"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://postpilot.yktis.com/cli",
|
|
24
|
+
"scripts": {
|
|
25
|
+
"test": "node --test test/*.test.mjs"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/client.mjs
CHANGED
|
@@ -1,146 +1,168 @@
|
|
|
1
|
-
import { randomUUID, createHash } from "node:crypto";
|
|
2
|
-
import { createReadStream } from "node:fs";
|
|
3
|
-
import { stat } from "node:fs/promises";
|
|
4
|
-
import { basename, extname } from "node:path";
|
|
5
|
-
export class CliError extends Error {
|
|
6
|
-
constructor(message, code = "INVALID_PARAMETERS", details = {}) {
|
|
7
|
-
super(message);
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
this.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
)
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
1
|
+
import { randomUUID, createHash } from "node:crypto";
|
|
2
|
+
import { createReadStream } from "node:fs";
|
|
3
|
+
import { stat } from "node:fs/promises";
|
|
4
|
+
import { basename, extname } from "node:path";
|
|
5
|
+
export class CliError extends Error {
|
|
6
|
+
constructor(message, code = "INVALID_PARAMETERS", details = {}) {
|
|
7
|
+
super(message);
|
|
8
|
+
// Older servers labeled plan capacity as throttling. Normalize that precise
|
|
9
|
+
// legacy classification during rollout; never infer it from a message.
|
|
10
|
+
const capacity =
|
|
11
|
+
code === "CAPACITY_EXCEEDED" ||
|
|
12
|
+
(code === "RATE_LIMITED" &&
|
|
13
|
+
(details.limitType === "ACCOUNT_CALENDAR_DAY" ||
|
|
14
|
+
details.details?.limitType === "ACCOUNT_CALENDAR_DAY"));
|
|
15
|
+
this.code = capacity ? "CAPACITY_EXCEEDED" : code;
|
|
16
|
+
this.details = { ...details };
|
|
17
|
+
delete this.details.code;
|
|
18
|
+
delete this.details.message;
|
|
19
|
+
if (this.code !== "RATE_LIMITED") {
|
|
20
|
+
delete this.details.retryAfter;
|
|
21
|
+
if (this.details.details) {
|
|
22
|
+
this.details.details = { ...this.details.details };
|
|
23
|
+
delete this.details.details.retryAfter;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (capacity) this.details.retryable = false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export const EXIT_CODES = {
|
|
30
|
+
INVALID_PARAMETERS: 2,
|
|
31
|
+
AUTHENTICATION_EXPIRED: 3,
|
|
32
|
+
INSUFFICIENT_SCOPE: 3,
|
|
33
|
+
NOT_FOUND: 4,
|
|
34
|
+
PENDING_APPROVAL: 5,
|
|
35
|
+
RATE_LIMITED: 6,
|
|
36
|
+
CAPACITY_EXCEEDED: 8,
|
|
37
|
+
PROVIDER_REJECTED: 7,
|
|
38
|
+
INTERNAL_FAILURE: 7,
|
|
39
|
+
};
|
|
40
|
+
export class Client {
|
|
41
|
+
constructor(origin, token, fetcher = fetch) {
|
|
42
|
+
this.origin = new URL(origin).origin;
|
|
43
|
+
this.token = token;
|
|
44
|
+
this.fetch = fetcher;
|
|
45
|
+
if (!token)
|
|
46
|
+
throw new CliError(
|
|
47
|
+
"Run postpilot login or configure POSTPILOT_TOKEN.",
|
|
48
|
+
"AUTHENTICATION_EXPIRED"
|
|
49
|
+
);
|
|
50
|
+
if (
|
|
51
|
+
!/^https:/.test(this.origin) &&
|
|
52
|
+
!["127.0.0.1", "localhost", "[::1]"].includes(new URL(origin).hostname)
|
|
53
|
+
)
|
|
54
|
+
throw new CliError("Remote API connections require HTTPS.");
|
|
55
|
+
}
|
|
56
|
+
async request(path, init = {}) {
|
|
57
|
+
let response;
|
|
58
|
+
try {
|
|
59
|
+
response = await this.fetch(this.origin + path, {
|
|
60
|
+
...init,
|
|
61
|
+
redirect: "error",
|
|
62
|
+
signal: AbortSignal.timeout(150000),
|
|
63
|
+
headers: { ...init.headers, Authorization: `Bearer ${this.token}` },
|
|
64
|
+
});
|
|
65
|
+
} catch {
|
|
66
|
+
throw new CliError(
|
|
67
|
+
"Could not reach PostPilot. Check the URL and network; do not blindly retry publication.",
|
|
68
|
+
"INTERNAL_FAILURE"
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
const body = await response.json().catch(() => ({}));
|
|
72
|
+
const error =
|
|
73
|
+
(body.result?.isError ? body.result.structuredContent?.error : null) ||
|
|
74
|
+
body.error?.data ||
|
|
75
|
+
body.error;
|
|
76
|
+
if (!response.ok)
|
|
77
|
+
throw new CliError(
|
|
78
|
+
typeof error === "string" ? error : error?.message || `HTTP ${response.status}`,
|
|
79
|
+
error?.code ||
|
|
80
|
+
(response.status === 401
|
|
81
|
+
? "AUTHENTICATION_EXPIRED"
|
|
82
|
+
: response.status === 403
|
|
83
|
+
? "INSUFFICIENT_SCOPE"
|
|
84
|
+
: response.status === 429
|
|
85
|
+
? "RATE_LIMITED"
|
|
86
|
+
: "INTERNAL_FAILURE"),
|
|
87
|
+
{
|
|
88
|
+
...error?.details,
|
|
89
|
+
requestId: response.headers.get("X-Request-Id") || error?.requestId,
|
|
90
|
+
retryAfter:
|
|
91
|
+
response.headers.get("Retry-After") || error?.retryAfter || error?.details?.retryAfter,
|
|
92
|
+
}
|
|
93
|
+
);
|
|
94
|
+
if (body.error)
|
|
95
|
+
throw new CliError(
|
|
96
|
+
error?.message || "MCP request failed",
|
|
97
|
+
error?.code || "INVALID_PARAMETERS",
|
|
98
|
+
error
|
|
99
|
+
);
|
|
100
|
+
if (body.result?.isError) {
|
|
101
|
+
const detail = body.result.structuredContent?.error || {};
|
|
102
|
+
throw new CliError(
|
|
103
|
+
detail.message || body.result.content?.[0]?.text || "Tool failed",
|
|
104
|
+
detail.code || "INTERNAL_FAILURE",
|
|
105
|
+
detail
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
return body;
|
|
109
|
+
}
|
|
110
|
+
async tool(name, args = {}) {
|
|
111
|
+
const body = await this.request("/api/mcp", {
|
|
112
|
+
method: "POST",
|
|
113
|
+
headers: { "Content-Type": "application/json", "MCP-Protocol-Version": "2025-06-18" },
|
|
114
|
+
body: JSON.stringify({
|
|
115
|
+
jsonrpc: "2.0",
|
|
116
|
+
id: randomUUID(),
|
|
117
|
+
method: "tools/call",
|
|
118
|
+
params: { name, arguments: args },
|
|
119
|
+
}),
|
|
120
|
+
});
|
|
121
|
+
return body.result?.structuredContent ?? JSON.parse(body.result?.content?.[0]?.text || "null");
|
|
122
|
+
}
|
|
123
|
+
async account(selector) {
|
|
124
|
+
const data = await this.tool("accounts_list");
|
|
125
|
+
const items = Array.isArray(data) ? data : (data.items ?? data.accounts ?? []);
|
|
126
|
+
const exact = items.filter((a) => a.id === selector);
|
|
127
|
+
const matches = exact.length
|
|
128
|
+
? exact
|
|
129
|
+
: items.filter(
|
|
130
|
+
(a) => String(a.platformUsername || a.name || "").toLowerCase() === selector.toLowerCase()
|
|
131
|
+
);
|
|
132
|
+
if (matches.length !== 1)
|
|
133
|
+
throw new CliError(
|
|
134
|
+
"Choose exactly one account ID or a unique channel name from accounts list."
|
|
135
|
+
);
|
|
136
|
+
return matches[0];
|
|
137
|
+
}
|
|
138
|
+
async upload(file, key) {
|
|
139
|
+
const info = await stat(file);
|
|
140
|
+
if (!info.isFile()) throw new CliError("Select one regular file.");
|
|
141
|
+
const mime =
|
|
142
|
+
extname(file).toLowerCase() === ".png"
|
|
143
|
+
? "image/png"
|
|
144
|
+
: extname(file).toLowerCase() === ".mp4"
|
|
145
|
+
? "video/mp4"
|
|
146
|
+
: null;
|
|
147
|
+
if (!mime)
|
|
148
|
+
throw new CliError(
|
|
149
|
+
"Raw uploads accept PNG or MP4. Use prepare-video for other video formats."
|
|
150
|
+
);
|
|
151
|
+
if (info.size > (mime === "image/png" ? 20 : 90) * 1024 * 1024)
|
|
152
|
+
throw new CliError("File exceeds the raw media upload size limit.");
|
|
153
|
+
const hash = createHash("sha256");
|
|
154
|
+
for await (const chunk of createReadStream(file)) hash.update(chunk);
|
|
155
|
+
return this.request("/api/mcp/media", {
|
|
156
|
+
method: "POST",
|
|
157
|
+
headers: {
|
|
158
|
+
"Content-Type": mime,
|
|
159
|
+
"Content-Length": String(info.size),
|
|
160
|
+
"X-File-Name": encodeURIComponent(basename(file)),
|
|
161
|
+
"Idempotency-Key": key || randomUUID(),
|
|
162
|
+
"X-Content-SHA256": hash.digest("hex"),
|
|
163
|
+
},
|
|
164
|
+
body: createReadStream(file),
|
|
165
|
+
duplex: "half",
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
}
|