@volter/twin-tiktok 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +310 -0
  3. package/client/tiktok-consent.tsx +154 -0
  4. package/client/tiktok-mirror.css +137 -0
  5. package/client/tiktok-mirror.tsx +492 -0
  6. package/dist/client/tiktok-consent.bundle.js +18 -0
  7. package/dist/client/tiktok-consent.d.ts +47 -0
  8. package/dist/client/tiktok-consent.js +20 -0
  9. package/dist/client/tiktok-consent.tsx +154 -0
  10. package/dist/client/tiktok-mirror.bundle.js +487 -0
  11. package/dist/client/tiktok-mirror.css +137 -0
  12. package/dist/client/tiktok-mirror.d.ts +42 -0
  13. package/dist/client/tiktok-mirror.js +315 -0
  14. package/dist/client/tiktok-mirror.tsx +492 -0
  15. package/dist/src/cli.d.ts +2 -0
  16. package/dist/src/cli.js +44 -0
  17. package/dist/src/index.d.ts +22 -0
  18. package/dist/src/index.js +167 -0
  19. package/dist/src/tiktok-blobs.d.ts +66 -0
  20. package/dist/src/tiktok-blobs.js +161 -0
  21. package/dist/src/tiktok-budget.d.ts +56 -0
  22. package/dist/src/tiktok-budget.js +136 -0
  23. package/dist/src/tiktok-capabilities.d.ts +7 -0
  24. package/dist/src/tiktok-capabilities.js +1855 -0
  25. package/dist/src/tiktok-conformance.d.ts +11 -0
  26. package/dist/src/tiktok-conformance.js +498 -0
  27. package/dist/src/tiktok-connector.d.ts +158 -0
  28. package/dist/src/tiktok-connector.js +600 -0
  29. package/dist/src/tiktok-consent-ui.d.ts +19 -0
  30. package/dist/src/tiktok-consent-ui.js +127 -0
  31. package/dist/src/tiktok-errors.d.ts +78 -0
  32. package/dist/src/tiktok-errors.js +175 -0
  33. package/dist/src/tiktok-ids.d.ts +16 -0
  34. package/dist/src/tiktok-ids.js +48 -0
  35. package/dist/src/tiktok-media.d.ts +7 -0
  36. package/dist/src/tiktok-media.js +86 -0
  37. package/dist/src/tiktok-mirror-ui.d.ts +49 -0
  38. package/dist/src/tiktok-mirror-ui.js +159 -0
  39. package/dist/src/tiktok-pkce.d.ts +25 -0
  40. package/dist/src/tiktok-pkce.js +56 -0
  41. package/dist/src/tiktok-posting.d.ts +100 -0
  42. package/dist/src/tiktok-posting.js +599 -0
  43. package/dist/src/tiktok-sample-mp4.d.ts +10 -0
  44. package/dist/src/tiktok-sample-mp4.js +55 -0
  45. package/dist/src/tiktok-scopes.d.ts +29 -0
  46. package/dist/src/tiktok-scopes.js +106 -0
  47. package/dist/src/tiktok-server.d.ts +28 -0
  48. package/dist/src/tiktok-server.js +89 -0
  49. package/dist/src/tiktok-store.d.ts +164 -0
  50. package/dist/src/tiktok-store.js +451 -0
  51. package/dist/src/tiktok-twin.d.ts +70 -0
  52. package/dist/src/tiktok-twin.js +1197 -0
  53. package/dist/src/tiktok-user.d.ts +28 -0
  54. package/dist/src/tiktok-user.js +174 -0
  55. package/package.json +74 -0
  56. package/src/cli.ts +43 -0
  57. package/src/index.ts +270 -0
  58. package/src/tiktok-blobs.ts +217 -0
  59. package/src/tiktok-budget.ts +163 -0
  60. package/src/tiktok-capabilities.ts +2022 -0
  61. package/src/tiktok-conformance.ts +526 -0
  62. package/src/tiktok-connector.ts +637 -0
  63. package/src/tiktok-consent-ui.ts +146 -0
  64. package/src/tiktok-errors.ts +197 -0
  65. package/src/tiktok-ids.ts +51 -0
  66. package/src/tiktok-journey.uitest.ts +305 -0
  67. package/src/tiktok-media.ts +89 -0
  68. package/src/tiktok-mirror-ui.ts +167 -0
  69. package/src/tiktok-pkce.ts +61 -0
  70. package/src/tiktok-posting.ts +617 -0
  71. package/src/tiktok-sample-mp4.ts +54 -0
  72. package/src/tiktok-scopes.ts +122 -0
  73. package/src/tiktok-server.ts +100 -0
  74. package/src/tiktok-store.ts +543 -0
  75. package/src/tiktok-twin.ts +1361 -0
  76. package/src/tiktok-user.ts +137 -0
@@ -0,0 +1,637 @@
1
+ // TikTok CONNECTOR — the live-vendor pull path.
2
+ //
3
+ // PULL (real -> twin): the twin's persona registry is only as useful as the identities in it, and
4
+ // Login Kit exposes exactly two authenticated reads a user token can perform: `GET /v2/user/info/`
5
+ // and the Display API's video reads. A pull observes the real account behind the operator's own
6
+ // user access token and, on request, that account's public videos, and folds them into the
7
+ // observed log.
8
+ //
9
+ // SCOPES DECIDE WHAT A PULL CAN SEE, and that is a fact about this vendor rather than a bug to
10
+ // paper over:
11
+ // • `/v2/user/info/` fields are gated per scope (basic / profile / stats), so a token that holds
12
+ // only `user.info.basic` cannot observe `username` or `follower_count`. The default pull asks
13
+ // for EVERY modelled field — a default-field pull would silently observe a bald persona — and
14
+ // a token with fewer scopes must NARROW `userFields` explicitly. A `scope_not_authorized`
15
+ // refusal THROWS; it is never read as "the account has no username".
16
+ // • the video reads need `video.list`, which the common Login Kit integration (Dub's included)
17
+ // does not request. So `videos` is OPT-IN: a refused read must never fold an empty video list
18
+ // over observed state, and silently skipping it would make "no videos" ambiguous.
19
+ //
20
+ // WHAT A PULL CANNOT OBSERVE, recorded as reasoned gaps rather than faked:
21
+ // • the DEVELOPER APP registry — TikTok apps are created in the developer portal; no API reads
22
+ // them (`tiktok.connector.pull_clients`, todo);
23
+ // • the GRANT (which scopes this token holds) — TikTok publishes no token-introspection
24
+ // endpoint, so the granted set cannot be enumerated; a successful user/info read evidences the
25
+ // scopes of the fields it returned and nothing more (`tiktok.connector.pull_grants`, todo).
26
+ //
27
+ // PUSH of the identity surface is an honest gap, not an omission: TikTok exposes NO write API for apps,
28
+ // users or grants — apps live in the developer portal, and a user's app permissions are managed in
29
+ // TikTok's own settings. `pushPendingTikTokActions` reports that; filed as `tiktok.connector.push`
30
+ // (todo). A World's POSTS are the exception, and they cross through the kernel's perform: the
31
+ // Content Posting API is TikTok's own write door for a new video (PERFORM, below).
32
+ //
33
+ // The vendor I/O is an INJECTED executor (the auth boundary): the kernel and this pack hold NO
34
+ // TikTok credential and import NO network client. Offline/tests pass a fake executor; live runs
35
+ // pass `liveTikTokExecute(accessToken)`. Same code path either way.
36
+ import { assertBudgetGuardIntact, deployableEntries, observeResources } from '@volter/world-core';
37
+ import type { PerformContext, PushOutcome, RemoteExecute, SyncResource, TwinAction } from '@volter/world-core';
38
+ import { TikTokBudget, TikTokBudgetError, tiktokBudgetPath, tiktokCallWeight, type TikTokBudgetOptions } from './tiktok-budget.ts';
39
+ import { clearPerformPublish, readPerformPublish, readTikTokBlobRange, tikTokBlobSize, writePerformPublish } from './tiktok-blobs.ts';
40
+ import { USER_FIELDS, VIDEO_FIELDS } from './tiktok-user.ts';
41
+
42
+ const SERVICE = 'tiktok';
43
+
44
+ /** TikTok's real API host — the live executor's routing table. ONLY mapped paths may be called
45
+ * live. The token/revoke paths are mapped so a live rehearsal can refresh or revoke the
46
+ * operator's OWN token through the guarded path; nothing else on open.tiktokapis.com is
47
+ * reachable, which is what keeps an unmodelled surface from being poked by accident (D8). */
48
+ const HOSTS: Record<string, string> = {
49
+ '/v2/user/info': 'https://open.tiktokapis.com',
50
+ '/v2/video/list': 'https://open.tiktokapis.com',
51
+ '/v2/video/query': 'https://open.tiktokapis.com',
52
+ '/v2/oauth/token': 'https://open.tiktokapis.com',
53
+ '/v2/oauth/revoke': 'https://open.tiktokapis.com',
54
+ };
55
+
56
+ /** Every modelled user field a pull requests by default — narrow it for a narrower token. */
57
+ export const PULL_USER_FIELDS: readonly string[] = USER_FIELDS;
58
+ /** Every modelled video field a pull requests when `videos` is enabled. */
59
+ export const PULL_VIDEO_FIELDS: readonly string[] = VIDEO_FIELDS;
60
+
61
+ /**
62
+ * The injected real-TikTok boundary. `execute` issues ONE request and returns the parsed JSON
63
+ * body. A real client (a raw fetch wrapper) is structurally assignable; tests pass a fake.
64
+ */
65
+ export type TikTokExecute = (
66
+ method: 'GET' | 'POST',
67
+ path: string,
68
+ init?: { headers?: Record<string, string>; body?: string },
69
+ ) => Promise<Record<string, any>>;
70
+
71
+ export type LiveTikTokOptions = {
72
+ /** Injected `fetch`, so a test can COUNT the requests the guard did or did not let through. */
73
+ fetchImpl?: typeof fetch;
74
+ /** An existing budget to share across executors. Omit and one is constructed. Cannot be null. */
75
+ budget?: TikTokBudget;
76
+ /** Construction options for the default budget (ledger path, clock). Cannot loosen it. */
77
+ budgetOptions?: TikTokBudgetOptions;
78
+ };
79
+
80
+ /**
81
+ * A live executor against the real TikTok open API, holding the operator's OWN user access token.
82
+ *
83
+ * THIS IS THE ONE PLACE this pack issues a live TikTok request, and therefore the one place the
84
+ * rate budget has to be enforced. EVERY call is guarded: the budget is charged BEFORE the request
85
+ * goes out (`checkBudget`, which THROWS instead of returning when the ceiling or a cooldown says
86
+ * stop) and the response is fed back (`recordCall`) so a 429 / `Retry-After` becomes a PERSISTED
87
+ * cooldown that makes every later call fail fast WITHOUT touching TikTok. There is deliberately no
88
+ * option to disable the guard and no value of `budget` that yields an unguarded client
89
+ * (`assertBudgetGuardIntact`).
90
+ */
91
+ export function liveTikTokExecute(accessToken: string, opts: LiveTikTokOptions = {}): TikTokExecute {
92
+ const doFetch = opts.fetchImpl ?? fetch;
93
+ const budget = opts.budget !== undefined && opts.budget !== null
94
+ ? assertBudgetGuardIntact(opts.budget, TikTokBudget, 'liveTikTokExecute')
95
+ : new TikTokBudget({ ...(opts.budgetOptions ?? {}), token: accessToken });
96
+ const explicitLedger = opts.budgetOptions?.path !== undefined || opts.budgetOptions?.root !== undefined;
97
+ if (opts.budget && !explicitLedger && budget.path !== tiktokBudgetPath({ token: accessToken })) {
98
+ throw new Error('liveTikTokExecute: injected budget is not keyed to the credential this client will send');
99
+ }
100
+ return async (method, path, init) => {
101
+ // TikTok writes every documented path WITH a trailing slash; the routing table and the price
102
+ // rules are keyed on the bare form, so both spellings resolve to one entry.
103
+ const bare = (path.split('?')[0] ?? path).replace(/\/+$/, '');
104
+ const host = HOSTS[bare];
105
+ if (!host) throw new Error(`liveTikTokExecute: refusing to call an unmapped TikTok path: ${bare}`);
106
+ const weight = tiktokCallWeight(method, path);
107
+ if (Object.keys(init?.headers ?? {}).some((name) => name.toLowerCase() === 'authorization')) {
108
+ throw new Error('liveTikTokExecute: refusing an injected Authorization header; the guarded credential is fixed at construction');
109
+ }
110
+ // THROWS instead of calling. Nothing below this line runs when the budget refuses.
111
+ const reservation = budget.checkBudget(weight);
112
+ const res = await doFetch(`${host}${path}`, {
113
+ method,
114
+ headers: { ...(init?.headers ?? {}), Authorization: `Bearer ${accessToken}` },
115
+ ...(init?.body !== undefined ? { body: init.body } : {}),
116
+ });
117
+ const resHeaders: Record<string, string> = {};
118
+ res.headers.forEach((v: string, k: string) => {
119
+ resHeaders[k.toLowerCase()] = v;
120
+ });
121
+ // Settles the reservation and, on a back-off signal, arms the cooldown. The cooldown is
122
+ // persisted before body parsing or any throw, so even an HTML/plain-text 429 survives it.
123
+ // recordCall may THROW after arming the cooldown (a back-off beyond the cap). On a refused
124
+ // call that louder refusal wins; an answer TikTok ACCEPTED is kept, so a write that landed is
125
+ // never recorded as failed and performed again on retry.
126
+ try {
127
+ budget.recordCall(weight, resHeaders, { status: res.status, reservation });
128
+ } catch (error) {
129
+ if (!(error instanceof TikTokBudgetError) || !res.ok) throw error;
130
+ }
131
+ const raw = await res.text();
132
+ let parsed: Record<string, any>;
133
+ try {
134
+ parsed = raw === '' ? {} : JSON.parse(raw) as Record<string, any>;
135
+ } catch {
136
+ throw new Error(`tiktok returned non-JSON for ${method} ${bare}: HTTP ${res.status}`);
137
+ }
138
+ // A REFUSED pull is NOT an empty account. TikTok answers a Display API failure with a NESTED
139
+ // error object whose `code` is something other than `ok` — and it carries that object on
140
+ // SUCCESS too, so a status check alone cannot tell refusal from genuine emptiness. Both
141
+ // shapes throw.
142
+ if (res.status >= 400) throw new Error(`tiktok refused ${method} ${bare}: HTTP ${res.status} ${JSON.stringify(parsed)}`);
143
+ const code = (parsed?.['error'] as Record<string, unknown> | undefined)?.['code'];
144
+ if (typeof code === 'string' && code !== 'ok') {
145
+ throw new Error(`tiktok refused ${method} ${bare}: ${JSON.stringify(parsed['error'])}`);
146
+ }
147
+ // The OAuth endpoints use the FLAT envelope instead — a string `error` field is a refusal.
148
+ if (typeof parsed?.['error'] === 'string') {
149
+ throw new Error(`tiktok refused ${method} ${bare}: ${JSON.stringify(parsed)}`);
150
+ }
151
+ return parsed;
152
+ };
153
+ }
154
+
155
+ /**
156
+ * Map a real `GET /v2/user/info/` response -> the account (persona) SyncResource.
157
+ *
158
+ * NOTHING IS INVENTED: a field the response omits records NOTHING rather than a placeholder —
159
+ * including username and display_name, because a scope-narrowed token legitimately omits them and
160
+ * a `?? null` there would fold null over a previously pulled persona's real values.
161
+ *
162
+ * The subject id is `union_id`, the vendor's own cross-app user key — NOT `open_id`, which is
163
+ * per-app and would give one human as many twin accounts as they have authorized apps.
164
+ */
165
+ export function mapUserInfoAccount(body: Record<string, any>): SyncResource {
166
+ const user = (body?.data?.user ?? {}) as Record<string, any>;
167
+ const str = (key: string, field: string) => (typeof user[key] === 'string' ? { [field]: user[key] } : {});
168
+ const num = (key: string, field: string) => (typeof user[key] === 'number' ? { [field]: user[key] } : {});
169
+ return {
170
+ type: 'account',
171
+ id: String(user['union_id'] ?? ''),
172
+ fields: {
173
+ ...str('username', 'username'),
174
+ ...str('display_name', 'displayName'),
175
+ ...str('avatar_url', 'avatarUrl'),
176
+ ...str('avatar_url_100', 'avatarUrl100'),
177
+ ...str('avatar_large_url', 'avatarLargeUrl'),
178
+ ...str('bio_description', 'bioDescription'),
179
+ ...str('profile_deep_link', 'profileDeepLink'),
180
+ ...(typeof user['is_verified'] === 'boolean' ? { isVerified: user['is_verified'] } : {}),
181
+ ...num('follower_count', 'followerCount'),
182
+ ...num('following_count', 'followingCount'),
183
+ ...num('likes_count', 'likesCount'),
184
+ ...num('video_count', 'videoCount'),
185
+ pulled: true,
186
+ },
187
+ };
188
+ }
189
+
190
+ /** Map one real Video object -> the video SyncResource, owned by the pulled account. */
191
+ export function mapVideo(video: Record<string, any>, ownerUnionId: string): SyncResource {
192
+ const num = (key: string, field: string) => (typeof video[key] === 'number' ? { [field]: video[key] } : {});
193
+ return {
194
+ type: 'video',
195
+ id: String(video['id'] ?? ''),
196
+ fields: {
197
+ ownerUnionId,
198
+ ...(typeof video['title'] === 'string' ? { title: video['title'] } : {}),
199
+ ...(typeof video['video_description'] === 'string' ? { videoDescription: video['video_description'] } : {}),
200
+ ...num('create_time', 'createTime'),
201
+ ...num('duration', 'duration'),
202
+ ...num('height', 'height'),
203
+ ...num('width', 'width'),
204
+ ...num('like_count', 'likeCount'),
205
+ ...num('comment_count', 'commentCount'),
206
+ ...num('share_count', 'shareCount'),
207
+ ...num('view_count', 'viewCount'),
208
+ ...(typeof video['is_aigc'] === 'boolean' ? { isAigc: video['is_aigc'] } : {}),
209
+ pulled: true,
210
+ },
211
+ };
212
+ }
213
+
214
+ /**
215
+ * A MOVING pull timestamp, forced strictly increasing within the process — never a pinned constant
216
+ * (ADDING_A_TWIN.md §6: under a fixed poll time a vendor value that REVERTS across polls collides
217
+ * with its own earlier observation and the delta silently vanishes).
218
+ */
219
+ let lastPollMs = 0;
220
+ function pollTimestamp(): string {
221
+ const now = Date.now();
222
+ lastPollMs = now > lastPollMs ? now : lastPollMs + 1;
223
+ return new Date(lastPollMs).toISOString();
224
+ }
225
+
226
+ export type PullOptions = {
227
+ root?: string;
228
+ occurredAt?: string;
229
+ /** Narrow the `user.fields` a pull asks for — required when the token holds fewer scopes than
230
+ * the three `user.info.*` ones (an unauthorized field is a refusal, not an empty value). */
231
+ userFields?: readonly string[];
232
+ /** Also pull the account's videos. OFF by default: the Display API video reads need the separate
233
+ * `video.list` scope, and a refused read must never fold an empty list over observed state. */
234
+ videos?: boolean;
235
+ /** How many videos to ask for in one page (TikTok's own maximum is 20). */
236
+ videoCount?: number;
237
+ };
238
+
239
+ async function collectTikTok(execute: TikTokExecute, opts: PullOptions): Promise<SyncResource[]> {
240
+ const fields = (opts.userFields ?? PULL_USER_FIELDS).join(',');
241
+ const body = await execute('GET', `/v2/user/info/?fields=${fields}`);
242
+ // The refusal check lives HERE, not only in the live executor: an injected executor (or a vendor
243
+ // 200 carrying a non-ok error object) must never fold an empty account over observed state. A
244
+ // reply with no `data.user.union_id` is a refusal or a malformed body, and either one THROWS.
245
+ const unionId = (body as Record<string, any> | null)?.['data']?.['user']?.['union_id'];
246
+ if (!body || typeof body !== 'object' || typeof unionId !== 'string' || unionId === '') {
247
+ throw new Error(`tiktok pull refused or malformed: ${JSON.stringify(body).slice(0, 200)}`);
248
+ }
249
+ const out: SyncResource[] = [mapUserInfoAccount(body)];
250
+ if (opts.videos === true) {
251
+ const count = opts.videoCount ?? 20;
252
+ const listed = await execute(
253
+ 'POST',
254
+ `/v2/video/list/?fields=${PULL_VIDEO_FIELDS.join(',')}`,
255
+ { headers: { 'content-type': 'application/json' }, body: JSON.stringify({ max_count: count }) },
256
+ );
257
+ const videos = (listed as Record<string, any> | null)?.['data']?.['videos'];
258
+ if (!Array.isArray(videos)) {
259
+ throw new Error(`tiktok video pull refused or malformed: ${JSON.stringify(listed).slice(0, 200)}`);
260
+ }
261
+ for (const video of videos as Array<Record<string, any>>) {
262
+ if (typeof video?.['id'] === 'string' && video['id'] !== '') out.push(mapVideo(video, unionId));
263
+ }
264
+ }
265
+ return out;
266
+ }
267
+
268
+ /** PULL the operator's own identity (and optionally their videos) into the observed log. */
269
+ export async function pullTikTok(execute: TikTokExecute, opts: PullOptions = {}): Promise<number> {
270
+ const resources = await collectTikTok(execute, opts);
271
+ const at = opts.occurredAt ?? pollTimestamp();
272
+ observeResources(SERVICE, resources, { ...(opts.root !== undefined ? { root: opts.root } : {}), at, batch: `obs:${SERVICE}:${at}` });
273
+ return resources.length;
274
+ }
275
+
276
+ /**
277
+ * D7 consumer-facing pull entry point: pull everything readable from the real TikTok surface and
278
+ * fold it into the twin in ONE observation, returning the standard `{ observed, deltasAppended }`.
279
+ * Idempotent — a re-pull of identical state appends nothing.
280
+ */
281
+ export async function syncTikTokFromReal(
282
+ execute: TikTokExecute,
283
+ opts: PullOptions = {},
284
+ ): Promise<{ observed: number; deltasAppended: number }> {
285
+ const at = opts.occurredAt ?? pollTimestamp();
286
+ const resources = await collectTikTok(execute, opts);
287
+ const result = observeResources(SERVICE, resources, { ...(opts.root !== undefined ? { root: opts.root } : {}), at, batch: `obs:${SERVICE}:${at}` });
288
+ return { observed: resources.length, deltasAppended: result.appended };
289
+ }
290
+
291
+ /**
292
+ * PUSH without a kernel executor — reported, never faked. TikTok has no API that creates a
293
+ * developer app, seeds a user, grants a scope, or edits a published video: those are developer-portal
294
+ * and in-app actions. A World's new posts DO have a door (the Content Posting API), and they cross
295
+ * through the kernel's deploy (`performTikTokAction`), which holds the sealed credential this
296
+ * function never does. This never confirms an action and never pretends to have pushed one; it
297
+ * returns the pending count so a caller can see exactly how much local state is waiting.
298
+ */
299
+ export function pushPendingTikTokActions(root?: string): { pushed: 0; unpushable: number } {
300
+ return { pushed: 0, unpushable: deployableEntries(SERVICE, root).length };
301
+ }
302
+
303
+ // ── PROTOCOL 2: the pack's half of the real state system ────────────────────────────────────
304
+
305
+ /** A `TikTokExecute` over the kernel's executor. At a REAL boundary the kernel sets the sealed
306
+ * credential over these headers (executor.ts); at the twin's own wire any credential is one. */
307
+ export function tiktokExecuteOver(execute: RemoteExecute): TikTokExecute {
308
+ return async (method, path, init) => {
309
+ const res = await execute({
310
+ method,
311
+ path,
312
+ headers: { accept: 'application/json', authorization: 'Bearer twin', ...(init?.headers ?? {}) },
313
+ ...(init?.body === undefined ? {} : { body: init.body }),
314
+ });
315
+ try { return JSON.parse(res.body || '{}'); } catch { return {}; }
316
+ };
317
+ }
318
+
319
+ /**
320
+ * The refresh adapter (protocol 2): `GET /v2/user/info/` is a real read of the real account, so the
321
+ * operator's own identity comes back from TikTok itself. It is also nearly the only thing this
322
+ * vendor lets a client read about its own identity surface — apps, grants and scopes are portal
323
+ * state.
324
+ *
325
+ * ── WHY THIS ONE DOES NOT THROW, AND THE PULL ABOVE STILL DOES ──────────────────────────────────
326
+ * Two entry points, two callers, two correct behaviours — and the difference is deliberate rather
327
+ * than a softened refusal:
328
+ *
329
+ * • `pullTikTok` / `syncTikTokFromReal` are the OPERATOR's pull over an executor they built. The
330
+ * credential is theirs and it is meant to work, so any refusal is a fault they must see: those
331
+ * THROW on every refusal shape, and `tiktok.connector.refused_pull_throws` holds that.
332
+ * • THIS adapter is driven by the kernel against whatever credential the root holds — including
333
+ * none. A reply that discloses no account is then not "the vendor failed", it is "this wire
334
+ * has nothing to tell us", and the honest answer is ZERO OBSERVATIONS. It is the googleoauth
335
+ * remote adapter's own shape (`collectGoogleOAuthIdentity` pushes a resource only when the
336
+ * reply carries one), transcribed.
337
+ *
338
+ * WHAT IS NOT WEAKENED, because this is the line the doctrine actually draws: nothing empty is ever
339
+ * FOLDED. `observeResources` is not called at all on this path, so no prior observation is
340
+ * overwritten, no subject is tombstoned, and a previously pulled persona survives byte for byte —
341
+ * which is the whole content of "a refused pull is not an empty account". The count comes back 0,
342
+ * and a caller that reads the count sees exactly that nothing was observed.
343
+ * `tiktok.connector.remote_refresh_folds_nothing_when_unreadable` pins both halves at once.
344
+ */
345
+ export async function syncTikTokFromRemote(
346
+ execute: RemoteExecute,
347
+ opts: { root?: string; origin?: string; occurredAt?: string } = {},
348
+ ): Promise<{ observed: number; deltasAppended: number }> {
349
+ const at = opts.occurredAt ?? new Date().toISOString();
350
+ const body = await tiktokExecuteOver(execute)('GET', `/v2/user/info/?fields=${PULL_USER_FIELDS.join(',')}`);
351
+ const unionId = (body as Record<string, any> | null)?.['data']?.['user']?.['union_id'];
352
+ if (typeof unionId !== 'string' || unionId === '') return { observed: 0, deltasAppended: 0 };
353
+ const resources = [mapUserInfoAccount(body)];
354
+ const result = observeResources(SERVICE, resources, { ...(opts.root !== undefined ? { root: opts.root } : {}), at, batch: `obs:${SERVICE}:${at}` });
355
+ return { observed: resources.length, deltasAppended: result.appended };
356
+ }
357
+
358
+ // ── PERFORM: a World's post crosses to TikTok through the Content Posting API ──────────────────
359
+ //
360
+ // A `video.publish` entry (a Direct Post the World made) crosses as the vendor's own flow, the one
361
+ // its Direct Post guide walks: (1) creator_info — the creator the credential names, and whether the
362
+ // entry's privacy_level is one of their options (a mismatch is refused here, before anything is
363
+ // sent); (2) video/init with FILE_UPLOAD and the chunk plan TikTok's rules require for the stored
364
+ // size; (3) each chunk PUT to the upload_url the vendor returned, PRESIGNED — the URL's upload_token
365
+ // is its authorization, so the sealed credential never goes with it, and the host is the vendor's
366
+ // upload host, not the API's; (4) status/fetch until PUBLISH_COMPLETE. A `video.inbox_upload` entry
367
+ // crosses the same way through inbox/video/init and ends at SEND_TO_USER_INBOX. The bytes are read
368
+ // off the blob seam by the entry's digest, a chunk at a time, across the branch's ancestors.
369
+ //
370
+ // THE BUDGET wraps the executor the perform receives (`budgetedTikTokExecute`, the X pack's shape),
371
+ // with ONE LEDGER PER CREDENTIAL — keyed by `ctx.credential`, the sealed credential's keyed
372
+ // fingerprint, so every World and branch performing with one TikTok account spends one allowance (the
373
+ // vendor meters the token, not the World), and by `ctx.root` only when no credential is sealed (the
374
+ // X / YouTube / LinkedIn keying): creator_info, the init and every status read
375
+ // are charged before they go and refused without calling TikTok when the ledger says stop; an answer
376
+ // that has arrived is never dropped. The chunk PUTs are NOT charged: once a publish has started, the
377
+ // budget cannot stop it — a half-sent video would be worse than a spent minute.
378
+ //
379
+ // THE BOUND. TikTok publishes no processing time and no polling interval, only status/fetch's 30 reads
380
+ // a minute per token; the wait reads every STATUS_POLL_MS (3 s: twenty a minute, which leaves the
381
+ // ledger room for the creator_info and init beside them) and stops at MAX_PUBLISH_WAIT_MS (120 s).
382
+ // Past that the perform fails RETRYABLY, and the publish it opened is KEPT (tiktok-blobs.ts
383
+ // perform-publishes): a Direct Post publishes itself once its bytes are in, so the next deploy asks
384
+ // that publish where it stands rather than initializing a second post, and resumes its missing chunks
385
+ // on the upload_url it kept. Only a publish TikTok says is gone — `invalid_publish_id`, or FAILED —
386
+ // is cleared, so the next perform opens a fresh one. A chunk TikTok does not take fails the perform
387
+ // retryably with the publish still kept. A 429 or a 5xx
388
+ // from status/fetch is TikTok saying "later", so the wait keeps reading inside its bound and ends
389
+ // RETRYABLY, never as a permanent failure.
390
+
391
+ /** How often the perform reads status/fetch while TikTok processes (see THE BOUND above). */
392
+ export const STATUS_POLL_MS = 3_000;
393
+ /** How long, in total, a perform waits on TikTok's processing before it fails the entry retryably. */
394
+ export const MAX_PUBLISH_WAIT_MS = 120_000;
395
+ /** The chunk the perform sends: TikTok's 5 MB minimum (read as MiB), so a 4 GB video is 800 chunks. */
396
+ export const PERFORM_CHUNK_BYTES = 5 * 1024 * 1024;
397
+
398
+ /** TikTok answered "later" (429, a 5xx) — the entry stays deployable and the next deploy tries again. */
399
+ export class TikTokRetryableError extends Error {
400
+ readonly retryable = true;
401
+ constructor(message: string) {
402
+ super(message);
403
+ this.name = 'TikTokRetryableError';
404
+ }
405
+ }
406
+
407
+ /** TikTok has the video but has not finished processing it inside the bound: retry later. */
408
+ export class TikTokStillProcessingError extends TikTokRetryableError {
409
+ constructor(readonly publishId: string, waitedMs: number, reads: number) {
410
+ super(`video still processing at TikTok (publish ${publishId}) after ${Math.round(waitedMs / 1000)}s and ${reads} status reads — the entry stays pending; the next deploy asks this publish where it stands`);
411
+ this.name = 'TikTokStillProcessingError';
412
+ }
413
+ }
414
+
415
+ /**
416
+ * The kernel executor, charged to this pack's TikTokBudget: the check RESERVES before the call and
417
+ * throws (TikTokBudgetError) instead of calling when the ceiling or a cooldown says stop; the answer
418
+ * settles the reservation and arms a cooldown on 429 / Retry-After. The vendor has ANSWERED by then:
419
+ * if recording throws (a back-off past the cap, persisted first), the answer still goes back and the
420
+ * next call meets the cooldown — dropping it would record a post TikTok accepted as failed.
421
+ */
422
+ export function budgetedTikTokExecute(execute: RemoteExecute, budget: TikTokBudget = new TikTokBudget()): RemoteExecute {
423
+ const guard = assertBudgetGuardIntact(budget, TikTokBudget, 'budgetedTikTokExecute');
424
+ return async (request) => {
425
+ const weight = tiktokCallWeight(request.method, request.path);
426
+ const reservation = guard.checkBudget(weight);
427
+ const res = await execute(request);
428
+ try {
429
+ guard.recordCall(weight, Object.fromEntries(Object.entries(res.headers ?? {}).map(([k, v]) => [k.toLowerCase(), v])), { status: res.status, reservation });
430
+ } catch (error) {
431
+ if (!(error instanceof TikTokBudgetError)) throw error;
432
+ }
433
+ return res;
434
+ };
435
+ }
436
+
437
+ const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
438
+
439
+ /** One Content Posting call: its `data`, or a throw carrying the vendor's refusal. */
440
+ async function postingCall(execute: RemoteExecute, label: string, path: string, body: unknown): Promise<{ data: Record<string, any>; raw: string }> {
441
+ const res = await execute({ method: 'POST', path, headers: { accept: 'application/json', 'content-type': 'application/json; charset=UTF-8' }, body: JSON.stringify(body) });
442
+ let parsed: Record<string, any> = {};
443
+ try { parsed = res.body ? JSON.parse(res.body) : {}; } catch { throw new Error(`tiktok answered ${label} with a body that is not JSON: HTTP ${res.status} ${res.body.slice(0, 200)}`); }
444
+ const code = parsed?.error?.code;
445
+ // TikTok saying "later" is not a refusal of the post
446
+ if (res.status === 429 || res.status >= 500) throw new TikTokRetryableError(`tiktok answered ${label} with HTTP ${res.status} (${String(code ?? 'no code')}) — retry later`);
447
+ if (res.status < 200 || res.status >= 300 || (typeof code === 'string' && code !== 'ok')) {
448
+ throw new TikTokRefusal(label, res.status, typeof code === 'string' ? code : '', JSON.stringify(parsed?.error ?? parsed).slice(0, 300));
449
+ }
450
+ return { data: (parsed.data ?? {}) as Record<string, any>, raw: res.body };
451
+ }
452
+
453
+ /** TikTok refused a call, with its own `error.code`. */
454
+ class TikTokRefusal extends Error {
455
+ constructor(label: string, readonly status: number, readonly code: string, detail: string) {
456
+ super(`tiktok refused ${label}: HTTP ${status} ${detail}`);
457
+ this.name = 'TikTokRefusal';
458
+ }
459
+ }
460
+
461
+ /** A budget refusal is a wait while the bound has room; past it, the caller's verdict. */
462
+ async function withinBudget<T>(deadline: number, call: () => Promise<T>): Promise<T> {
463
+ for (;;) {
464
+ try {
465
+ return await call();
466
+ } catch (error) {
467
+ // only a window that will open again is a wait: ceiling, burst, a cooldown the vendor asked for
468
+ if (!(error instanceof TikTokBudgetError) || !['ceiling', 'burst', 'cooldown'].includes(error.kind)) throw error;
469
+ if (Date.now() + error.retryAfterMs > deadline) throw error;
470
+ await sleep(error.retryAfterMs);
471
+ }
472
+ }
473
+ }
474
+
475
+ /** The chunk plan TikTok's rules require for `size` bytes at the perform's chunk size. */
476
+ export function performChunkPlan(size: number): { chunkSize: number; totalChunkCount: number } {
477
+ if (size < PERFORM_CHUNK_BYTES) return { chunkSize: size, totalChunkCount: 1 };
478
+ return { chunkSize: PERFORM_CHUNK_BYTES, totalChunkCount: Math.floor(size / PERFORM_CHUNK_BYTES) };
479
+ }
480
+
481
+ type StatusRead = { status: string; failReason?: string; postId?: string; uploadedBytes: number };
482
+
483
+ /** status/fetch, with `publicaly_available_post_id` read off the RAW body: it is a list of int64 JSON
484
+ * numbers, and JSON.parse would round a 19-digit id to the nearest double. */
485
+ async function readStatus(execute: RemoteExecute, publishId: string): Promise<StatusRead> {
486
+ const { data, raw } = await postingCall(execute, 'status/fetch', '/v2/post/publish/status/fetch/', { publish_id: publishId });
487
+ const ids = /"publicaly_available_post_id"\s*:\s*\[\s*"?(\d+)/.exec(raw);
488
+ return {
489
+ status: String(data.status ?? ''),
490
+ ...(typeof data.fail_reason === 'string' && data.fail_reason ? { failReason: data.fail_reason } : {}),
491
+ ...(ids ? { postId: ids[1] } : {}),
492
+ uploadedBytes: Number(data.uploaded_bytes ?? 0),
493
+ };
494
+ }
495
+
496
+ async function publishOver(budgeted: RemoteExecute, raw: RemoteExecute, action: TwinAction, mode: 'direct' | 'inbox', root?: string): Promise<PushOutcome> {
497
+ const f = (action.fields ?? {}) as Record<string, any>;
498
+ const sha = f._content_sha256;
499
+ const size = await tikTokBlobSize(sha, root);
500
+ if (size === null) throw new Error(`tiktok: ${action.operation} ${action.subject.id} has no stored media to post (_content_sha256 ${String(sha ?? 'absent')})`);
501
+ const contentType = typeof f._content_type === 'string' ? f._content_type : 'video/mp4';
502
+ const started = Date.now();
503
+ const deadline = started + MAX_PUBLISH_WAIT_MS;
504
+ const done = mode === 'direct' ? 'PUBLISH_COMPLETE' : 'SEND_TO_USER_INBOX';
505
+
506
+ // (0) a publish an earlier perform of this entry opened: where does it stand? It is re-opened ONLY
507
+ // when TikTok says it is gone — `invalid_publish_id`, or status FAILED ("an error has occurred and
508
+ // the entire process has failed"). Anything else is the same publish, still alive: PROCESSING_UPLOAD
509
+ // is "the upload is in process", and `uploaded_bytes` is "the number of bytes uploaded (1-indexed)
510
+ // for FILE_UPLOAD" (Get Post Status reference) — a count of what arrived, which says nothing about
511
+ // whether TikTok will still post it. So a partial count is where this perform resumes the SAME
512
+ // publish's chunks, on the upload_url it kept; a second init could put the video up twice.
513
+ let kept = await readPerformPublish(action.id, root);
514
+ if (kept && (kept.sha256 !== sha || kept.size !== size)) kept = null;
515
+ let offset = 0;
516
+ if (kept) {
517
+ let state: StatusRead | null = null;
518
+ try {
519
+ state = await withinBudget(deadline, () => readStatus(budgeted, kept!.publishId));
520
+ } catch (error) {
521
+ if (!(error instanceof TikTokRefusal) || error.code !== 'invalid_publish_id') throw error;
522
+ }
523
+ if (state === null || state.status === 'FAILED') { await clearPerformPublish(action.id, root); kept = null; }
524
+ else offset = state.status === 'PROCESSING_UPLOAD' ? state.uploadedBytes : size;
525
+ }
526
+
527
+ // (1) + (2) the creator and the init — the only calls charged before a publish exists
528
+ if (!kept) {
529
+ if (mode === 'direct') {
530
+ const creator = await withinBudget(deadline, () => postingCall(budgeted, 'creator_info', '/v2/post/publish/creator_info/query/', {}));
531
+ const options: unknown[] = Array.isArray(creator.data.privacy_level_options) ? creator.data.privacy_level_options : [];
532
+ if (!options.includes(f.privacyLevel)) {
533
+ throw new Error(`tiktok: the creator @${String(creator.data.creator_username ?? '?')} cannot post as ${String(f.privacyLevel)} (their options: ${options.join(', ')})`);
534
+ }
535
+ }
536
+ const plan = performChunkPlan(size);
537
+ const postInfo = {
538
+ title: String(f.videoDescription ?? f.title ?? ''),
539
+ privacy_level: f.privacyLevel,
540
+ disable_duet: f.disableDuet === true,
541
+ disable_comment: f.disableComment === true,
542
+ disable_stitch: f.disableStitch === true,
543
+ ...(typeof f.videoCoverTimestampMs === 'number' ? { video_cover_timestamp_ms: f.videoCoverTimestampMs } : {}),
544
+ ...(f.brandContentToggle === true ? { brand_content_toggle: true } : {}),
545
+ ...(f.brandOrganicToggle === true ? { brand_organic_toggle: true } : {}),
546
+ ...(f.isAigc === true ? { is_aigc: true } : {}),
547
+ };
548
+ const init = await withinBudget(deadline, () => postingCall(
549
+ budgeted,
550
+ mode === 'direct' ? 'video/init' : 'inbox/video/init',
551
+ mode === 'direct' ? '/v2/post/publish/video/init/' : '/v2/post/publish/inbox/video/init/',
552
+ {
553
+ ...(mode === 'direct' ? { post_info: postInfo } : {}),
554
+ source_info: { source: 'FILE_UPLOAD', video_size: size, chunk_size: plan.chunkSize, total_chunk_count: plan.totalChunkCount },
555
+ },
556
+ ));
557
+ const publishId = init.data.publish_id;
558
+ if (typeof publishId !== 'string' || typeof init.data.upload_url !== 'string') throw new Error(`tiktok init answered no publish_id/upload_url: ${init.raw.slice(0, 200)}`);
559
+ kept = { publishId, uploadUrl: init.data.upload_url, sha256: String(sha), size, ...plan };
560
+ await writePerformPublish(action.id, kept, root);
561
+ offset = 0;
562
+ }
563
+ const publish = kept;
564
+
565
+ // (3) the chunks still missing, in order, presigned, unbudgeted. A PUT TikTok does not take leaves
566
+ // the publish kept and fails RETRYABLY: the next perform asks status/fetch where it stands.
567
+ for (let index = Math.floor(offset / publish.chunkSize); offset < size && index < publish.totalChunkCount; index += 1) {
568
+ const first = index * publish.chunkSize;
569
+ const last = index === publish.totalChunkCount - 1 ? size - 1 : first + publish.chunkSize - 1;
570
+ const chunk = (await readTikTokBlobRange(sha, first, last, root)) ?? new Uint8Array(0);
571
+ const answer = await raw({
572
+ method: 'PUT',
573
+ path: publish.uploadUrl,
574
+ presigned: true,
575
+ headers: { 'content-type': contentType, 'content-length': String(chunk.length), 'content-range': `bytes ${first}-${last}/${size}` },
576
+ body: chunk,
577
+ });
578
+ if (answer.status === 206 || answer.status === 201) continue;
579
+ throw new TikTokRetryableError(`tiktok: chunk ${index + 1}/${publish.totalChunkCount} of publish ${publish.publishId} answered HTTP ${answer.status} — the publish is kept; the next deploy asks it where it stands: ${answer.body.slice(0, 200)}`);
580
+ }
581
+
582
+ // (4) the wait, bounded; "later" answers keep it waiting
583
+ let reads = 0;
584
+ for (;;) {
585
+ let state: StatusRead | null = null;
586
+ try {
587
+ state = await withinBudget(deadline, () => readStatus(budgeted, publish.publishId));
588
+ reads += 1;
589
+ } catch (error) {
590
+ if (error instanceof TikTokBudgetError) throw new TikTokStillProcessingError(publish.publishId, Date.now() - started, reads);
591
+ if (!(error instanceof TikTokRetryableError)) throw error;
592
+ }
593
+ if (state?.status === done) {
594
+ await clearPerformPublish(action.id, root);
595
+ const postId = state.postId;
596
+ return {
597
+ externalId: postId ?? publish.publishId,
598
+ ...(postId ? { url: `https://www.tiktok.com/video/${postId}` } : {}),
599
+ data: { publish_id: publish.publishId, status: state.status, ...(postId ? { post_id: postId } : {}) },
600
+ };
601
+ }
602
+ if (state?.status === 'FAILED') {
603
+ await clearPerformPublish(action.id, root);
604
+ throw new Error(`tiktok failed publish ${publish.publishId}: ${state.failReason ?? 'no fail_reason'}`);
605
+ }
606
+ if (Date.now() + STATUS_POLL_MS > deadline) throw new TikTokStillProcessingError(publish.publishId, Date.now() - started, reads);
607
+ await sleep(STATUS_POLL_MS);
608
+ }
609
+ }
610
+
611
+ /**
612
+ * The perform adapter (protocol 2).
613
+ *
614
+ * A World's post crosses (`video.publish`, `video.inbox_upload`: see PERFORM above). Everything else a
615
+ * World writes here has no upstream home, and saying so is the point: TikTok publishes NO API that
616
+ * creates a developer app, seeds a user, grants a scope or issues a token — those are developer-portal
617
+ * and in-app settings actions a person takes in a browser — and an upload whose file failed TikTok's
618
+ * checks here (`publish.fail`) has nothing to send.
619
+ */
620
+ export async function performTikTokAction(execute: RemoteExecute, action: TwinAction, ctx: PerformContext, budget?: TikTokBudget): Promise<PushOutcome> {
621
+ const op = action.operation ?? `${action.subject.type}.update`;
622
+ if (op === 'video.publish' || op === 'video.inbox_upload') {
623
+ // ONE LEDGER PER CREDENTIAL (its keyed fingerprint), else per World root when none is sealed;
624
+ // `budget` is injectable so a verify charges a temporary ledger of its own
625
+ const budgeted = budgetedTikTokExecute(execute, budget ?? new TikTokBudget(ctx.credential !== undefined ? { token: ctx.credential } : ctx.root !== undefined ? { root: ctx.root } : {}));
626
+ return publishOver(budgeted, execute, action, op === 'video.publish' ? 'direct' : 'inbox', ctx.root);
627
+ }
628
+ return {
629
+ externalId: action.subject.id,
630
+ data: {
631
+ performed: false,
632
+ reason: op === 'publish.fail'
633
+ ? `${op}: the upload failed TikTok's checks on this twin (${String((action.fields ?? {}).failReason ?? 'unknown')}), so there is no post to send`
634
+ : `${op} has nowhere to go: TikTok creates developer apps, users, tokens and scope grants in the developer portal and in TikTok's own account settings, and publishes no API for any of them`,
635
+ },
636
+ };
637
+ }