realtime-avatar 0.4.1 → 0.5.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 CHANGED
@@ -70,20 +70,34 @@ all), and the `video` policy types are deliberately not one-to-one with the wire
70
70
  ```ts
71
71
  // calls
72
72
  rta.startCall({ avatarId, mode?, instructions?, context?, maxSeconds?, video?, transcript?, metadata? })
73
- rta.endCall(sessionId, { reason? }) // free an abandoned call's slot now; idempotent, never throws
73
+ rta.endCall(sessionId, { reason? }) // free an abandoned call's slot; idempotent, never throws
74
74
 
75
75
  // avatars
76
- rta.createAvatarFromVideo({ displayName, videoUrl, voice? })
76
+ rta.createAvatarFromImage({ displayName, imageUrl, motionPrompt?, voice? }) // the only lane
77
+ rta.createAvatarFromVideo({ displayName, videoUrl, voice? }) // DEPRECATED — closed, 422
77
78
  rta.listAvatars()
78
79
  rta.getAvatar(avatarId)
79
- rta.syncClips(avatarId, clipUrls) // after ANY clip change
80
+ rta.updateAvatar(avatarId, patch) // re-point displayName / defaultVoiceId
81
+ rta.swapSource(avatarId, { sourceAssetId, anchorTimeMs? }) // re-shoot her: new loop, library re-renders
82
+ rta.retimeAnchor(avatarId, anchorTimeMs) // same loop, different rest frame
83
+ rta.deleteAvatar(avatarId)
84
+
85
+ // clip library — declared as JSON, never as URLs
86
+ rta.setClipLibrary(avatarId, { clips, expectedRevision? }) // declare the FULL set; CAS via expectedRevision
87
+ rta.setLoop(avatarId, { motionPrompt }) // re-direct the RESTING LOOP; clips untouched
88
+ rta.waitForLoop(avatarId) // block until it settles; THROWS if it failed
89
+ rta.waitForClips(avatarId) // block until nothing is still rendering
90
+ rta.listClips(avatarId) // rows + revision, anchor, eligibility
91
+ rta.syncClips(avatarId, clipUrls) // DEPRECATED — external-URL tier; use setClipLibrary
80
92
 
81
93
  // assets
82
94
  rta.createRemoteAsset({ kind, remoteUrl })
83
95
  rta.uploadAsset(file, { kind })
84
96
 
85
97
  // billing
86
- rta.creditBalance()
98
+ rta.creditBalance() // balance + reserved
99
+ rta.listSessions({ from, to, endUserId }) // per-session: when, how long, what it cost
100
+ rta.iterateSessions({ from, to }) // the same, paging handled
87
101
 
88
102
  // webhooks
89
103
  verifyTranscript(rawBytes, headers, secret)
package/dist/express.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { P as ProxyConfig } from './types-9leGOgVv.js';
2
- import './types-C5zypiVY.js';
1
+ import { P as ProxyConfig } from './types-CHrhuhYb.js';
2
+ import './types-BeGuvyra.js';
3
3
 
4
4
  type Expressish = {
5
5
  method: string;
package/dist/express.js CHANGED
@@ -43,7 +43,7 @@ var RealtimeAvatarHttpError = class extends RealtimeAvatarError {
43
43
 
44
44
  // ../http-client/src/client.ts
45
45
  var DEFAULT_BASE_URL = "https://realtimeavatar.ai/api/v1";
46
- var SDK_VERSION = "0.4.1";
46
+ var SDK_VERSION = "0.5.1";
47
47
  var RealtimeAvatar = class {
48
48
  #apiKey;
49
49
  #baseUrl;
@@ -157,10 +157,15 @@ var RealtimeAvatar = class {
157
157
  /**
158
158
  * Register a character from a looping clip you host.
159
159
  *
160
- * Use a VIDEO source for anything that will be called live. An avatar built from a still
161
- * image reaches `ready`, mints calls, and publishes a BLACK track the status and the
162
- * track dimensions both look fine, only the pixels are wrong. Image sources are for
163
- * offline lipsync renders.
160
+ * @deprecated CLOSED to new callers this answers `422` unless your tenant was already
161
+ * creating from video, in which case it keeps working and your existing avatars are
162
+ * untouched. Use {@link createAvatarFromImage}: one still, and the platform renders the
163
+ * resting loop and the motion library from it.
164
+ *
165
+ * The reason it closed is not arbitrary. Every clip has to start and end on ONE rest pose
166
+ * or a state switch reads as a jump, and the platform can only guarantee that when it
167
+ * rendered the loop and the clips from the same portrait. A supplied video cannot honour
168
+ * it, so the lane could never be made to look right.
164
169
  */
165
170
  async createAvatarFromVideo(input) {
166
171
  const asset = await this.createRemoteAsset({ kind: "video", remoteUrl: input.videoUrl });
@@ -173,12 +178,41 @@ var RealtimeAvatar = class {
173
178
  metadata: input.metadata
174
179
  });
175
180
  }
181
+ /**
182
+ * Register a character from ONE still image. The platform generates everything moving:
183
+ * the resting loop she idles in, then a starter motion library rendered against her rest
184
+ * pose. No footage, no clip URLs, nothing to shoot.
185
+ *
186
+ * `motionPrompt` directs the RESTING LOOP — the video she plays when nothing else is
187
+ * happening — and it is the only chance to direct it, because there is no API today that
188
+ * re-generates a loop after creation (see {@link updateAvatar} for the one thing that can
189
+ * be re-pointed). Describe a small closed arc that returns to where it started: "settles
190
+ * into frame, breathes gently, a slow blink". Omit it and the house default is used.
191
+ *
192
+ * Creation returns while the avatar is still `preprocessing`; poll {@link getAvatar} until
193
+ * it leaves that state. The loop is load-bearing, so a failure there settles `failed` with
194
+ * a readable error — the motion library is not, and a library failure degrades to
195
+ * loop-only rather than demoting the character.
196
+ */
197
+ async createAvatarFromImage(input) {
198
+ const asset = await this.createRemoteAsset({ kind: "image", remoteUrl: input.imageUrl });
199
+ return this.createAvatar({
200
+ displayName: input.displayName,
201
+ sourceKind: "image",
202
+ sourceAssetId: asset.id,
203
+ motionPrompt: input.motionPrompt,
204
+ voice: input.voice,
205
+ settings: input.settings,
206
+ metadata: input.metadata
207
+ });
208
+ }
176
209
  async createAvatar(input) {
177
210
  const body = {
178
211
  displayName: input.displayName,
179
212
  sourceKind: input.sourceKind,
180
213
  sourceAssetId: input.sourceAssetId
181
214
  };
215
+ if (input.motionPrompt !== void 0) body.motionPrompt = input.motionPrompt;
182
216
  if (input.voice !== void 0) body.voice = input.voice;
183
217
  if (input.settings !== void 0) body.settings = input.settings;
184
218
  if (input.metadata !== void 0) body.metadata = input.metadata;
@@ -191,15 +225,192 @@ var RealtimeAvatar = class {
191
225
  async getAvatar(avatarId) {
192
226
  return toAvatar(await this.#json(await this.#request("GET", `/avatars/${avatarId}`)));
193
227
  }
194
- /** Re-point what an avatar already is. `defaultVoiceId: null` clears the default voice. */
228
+ /**
229
+ * Re-point what an avatar already is. `defaultVoiceId: null` clears the default voice.
230
+ *
231
+ * Cheap metadata only. The three things that cost a render have their own methods, because
232
+ * putting them in a patch alongside a rename would hide minutes of GPU work behind a field:
233
+ * {@link retimeAnchor} moves the rest frame, {@link swapSource} replaces the footage, and
234
+ * the resting loop is re-directed by description at `PUT /v1/avatars/{id}/loop`.
235
+ */
195
236
  async updateAvatar(avatarId, patch) {
196
237
  return toAvatar(
197
238
  await this.#json(await this.#request("PATCH", `/avatars/${avatarId}`, { json: patch }))
198
239
  );
199
240
  }
241
+ /**
242
+ * Re-shoot the character — swap in new footage as her resting loop.
243
+ *
244
+ * ASYNCHRONOUS, and that is the whole design. This returns as soon as the swap is
245
+ * accepted; the avatar keeps serving its CURRENT loop, cache and clips the entire time,
246
+ * and cuts over to the new generation in one step once the replacement is prepared.
247
+ * A call minted a second after this returns is a normal call on the old footage.
248
+ *
249
+ * Two consequences worth designing for:
250
+ *
251
+ * - **The clip library empties and refills.** Old takes are footage of the old source and
252
+ * cannot splice against the new loop, so they are dropped and re-rendered. Between the
253
+ * cutover and the last re-render she rests on the new loop with less variety — never
254
+ * broken, just plainer. Do not gate your UI on the library being full.
255
+ * - **A failed swap does not fail the avatar.** She keeps serving, `status` stays `ready`,
256
+ * and the reason lands on `error`. So poll `getAvatar` and read `error` — a non-null
257
+ * `error` on a `ready` avatar is the swap that did not take, not an unhealthy character.
258
+ *
259
+ * The frame this rests on comes from the new footage: pass `anchorTimeMs` when frame 0 of
260
+ * the take is mid-blink. Video-sourced avatars only — a portrait-anchored one is a 422.
261
+ */
262
+ async swapSource(avatarId, input) {
263
+ const json2 = { sourceAssetId: input.sourceAssetId };
264
+ if (input.anchorTimeMs !== void 0) json2.anchorTimeMs = input.anchorTimeMs;
265
+ return toAvatar(await this.#json(await this.#request("PATCH", `/avatars/${avatarId}`, { json: json2 })));
266
+ }
267
+ /**
268
+ * Re-point the anchor at a different frame of the loop she ALREADY has — same footage,
269
+ * different rest pose. `swapSource` replaces the footage; this only moves the frame.
270
+ *
271
+ * Re-renders the clip library the same way, with the same degradation window, and is
272
+ * clamped server-side to the loop's last extractable frame (read the avatar back to see
273
+ * what was actually cut). Video-sourced avatars only.
274
+ */
275
+ async retimeAnchor(avatarId, anchorTimeMs) {
276
+ return toAvatar(
277
+ await this.#json(await this.#request("PATCH", `/avatars/${avatarId}`, { json: { anchorTimeMs } }))
278
+ );
279
+ }
200
280
  async deleteAvatar(avatarId) {
201
281
  await this.#json(await this.#request("DELETE", `/avatars/${avatarId}`));
202
282
  }
283
+ // A clip envelope missing `revision` would silently drop `expectedRevision` from the
284
+ // next declare — CAS degrades to unconditional with zero signal — so it throws instead.
285
+ #clipEnvelope(out) {
286
+ if (typeof out.revision !== "number" || !Array.isArray(out.data)) {
287
+ throw new RealtimeAvatarError("clip library response did not match the contract");
288
+ }
289
+ return out;
290
+ }
291
+ /**
292
+ * Declare the avatar's full desired clip library — a declaration, not a delta. The
293
+ * platform reconciles it against what exists: unchanged clips are `kept` (still
294
+ * serving), new or changed ones are `queued` to render, and clips you dropped are
295
+ * `retired`. The 202 is acceptance, not readiness — poll `listClips` until no row is
296
+ * `queued` or `generating`. A rejected upload settles `failed`, which is terminal, so
297
+ * waiting for all-`ready` waits forever. While a re-render is in flight the previous
298
+ * take keeps serving, so a declaration never blanks a live avatar.
299
+ *
300
+ * `expectedRevision` is compare-and-set: pass the `revision` you last read and a
301
+ * concurrent writer surfaces as a 409 instead of a lost update. Omit it to declare
302
+ * unconditionally.
303
+ *
304
+ * At most 12 clips: one `idle`, up to two `listen`, the rest `gesture`. An uploaded
305
+ * clip (`source: { assetId }`) must start AND end on the avatar's rest pose — pose
306
+ * validation rejects it otherwise (`status: "failed"`, the verdict in `poseCheck`),
307
+ * and the rest of the library is untouched.
308
+ */
309
+ async setClipLibrary(avatarId, library) {
310
+ const body = { clips: library.clips };
311
+ if (library.expectedRevision !== void 0) body.expectedRevision = library.expectedRevision;
312
+ return this.#clipEnvelope(
313
+ await this.#json(
314
+ await this.#request("PUT", `/avatars/${avatarId}/clips`, { json: body })
315
+ )
316
+ );
317
+ }
318
+ /**
319
+ * Re-direct the RESTING LOOP — the video she plays when nothing else is happening — from
320
+ * a new one-sentence description.
321
+ *
322
+ * Not a clip, and this is the distinction integrations get wrong: a clip with
323
+ * `role: "idle"` is a variant spliced OVER the loop, and declaring one never changes what
324
+ * she rests in. This is the only thing that does.
325
+ *
326
+ * `202`, because the render takes minutes. Three properties, all measured against a real
327
+ * render rather than asserted:
328
+ *
329
+ * - **She never goes dark.** She stays `ready` and keeps serving her previous loop for the
330
+ * entire render — returned as `servingUrl` — then the swap publishes in one step.
331
+ * - **Your clip library is untouched.** Clips render against the portrait, not against the
332
+ * loop, so a re-direct re-queues nothing and does not move `revision`.
333
+ * - **It bills once**, at the rendering model's rate, per re-direct.
334
+ *
335
+ * Refusals worth telling apart: `409 loop_pending` (one is already in flight — wait) and
336
+ * `422 loop_not_generatable` (a grandfathered video-sourced avatar has no portrait to
337
+ * re-animate — terminal, do not retry).
338
+ */
339
+ async setLoop(avatarId, loop) {
340
+ return await this.#json(
341
+ await this.#request("PUT", `/avatars/${avatarId}/loop`, { json: { motionPrompt: loop.motionPrompt } })
342
+ );
343
+ }
344
+ /**
345
+ * Block until a loop re-direct settles, and throw if it did not take.
346
+ *
347
+ * `setLoop` returns on ACCEPTANCE; the render runs for minutes afterwards. Every caller
348
+ * therefore writes the same polling loop, and the obvious version of it never terminates
349
+ * on failure — a failed re-direct leaves her `ready` (she is still serving the old loop)
350
+ * and writes nothing to `error`. `idleVideoStatus` is the only field that moves, which is
351
+ * why this exists rather than a doc line telling you to poll.
352
+ *
353
+ * Race-free without a baseline: the platform commits `queued` before `setLoop` returns, so
354
+ * by the time you can call this the status has already left `ready`.
355
+ *
356
+ * Resolves with the settled avatar — `sourceAssetId` is now the new loop. Throws on a
357
+ * failed render and on timeout; a timeout is not a failure, so re-poll or call again.
358
+ */
359
+ async waitForLoop(avatarId, options = {}) {
360
+ const timeoutMs = options.timeoutMs ?? 20 * 6e4;
361
+ const pollMs = Math.max(1e3, options.pollMs ?? 1e4);
362
+ const deadline = Date.now() + timeoutMs;
363
+ for (; ; ) {
364
+ const avatar = await this.getAvatar(avatarId);
365
+ if (avatar.idleVideoStatus === "failed") {
366
+ throw new RealtimeAvatarError(
367
+ `Loop re-direct failed for ${avatarId}. She is still serving her previous loop \u2014 nothing was lost, and you can send another description.`
368
+ );
369
+ }
370
+ if (avatar.idleVideoStatus === "ready" || avatar.idleVideoStatus === "none") return avatar;
371
+ if (Date.now() >= deadline) {
372
+ throw new RealtimeAvatarError(
373
+ `Loop re-direct for ${avatarId} was still ${avatar.idleVideoStatus} after ${Math.round(timeoutMs / 1e3)}s. This is a timeout, not a failure \u2014 the render may still land.`
374
+ );
375
+ }
376
+ await sleep(pollMs);
377
+ }
378
+ }
379
+ /**
380
+ * Block until every clip in the library has stopped moving.
381
+ *
382
+ * Settled means no row is `queued` or `generating` — NOT that every row is `ready`.
383
+ * Waiting for all-`ready` is the intuitive version and it hangs forever: a clip rejected
384
+ * by pose validation settles `failed`, which is terminal. So this returns the library
385
+ * with the failures in it and lets you decide; a partial library is a legitimate outcome
386
+ * and the rest of it is already serving.
387
+ *
388
+ * Throws only on timeout.
389
+ */
390
+ async waitForClips(avatarId, options = {}) {
391
+ const timeoutMs = options.timeoutMs ?? 20 * 6e4;
392
+ const pollMs = Math.max(1e3, options.pollMs ?? 1e4);
393
+ const deadline = Date.now() + timeoutMs;
394
+ for (; ; ) {
395
+ const library = await this.listClips(avatarId);
396
+ const moving = library.data.filter((c) => c.status === "queued" || c.status === "generating");
397
+ if (moving.length === 0) return library;
398
+ if (Date.now() >= deadline) {
399
+ throw new RealtimeAvatarError(
400
+ `${moving.length} clip(s) on ${avatarId} were still rendering after ${Math.round(timeoutMs / 1e3)}s: ${moving.map((c) => c.clipId).join(", ")}. This is a timeout, not a failure.`
401
+ );
402
+ }
403
+ await sleep(pollMs);
404
+ }
405
+ }
406
+ /** The avatar's clip library: every non-retired clip, plus revision, anchor and eligibility. */
407
+ async listClips(avatarId) {
408
+ return this.#clipEnvelope(
409
+ await this.#json(
410
+ await this.#request("GET", `/avatars/${avatarId}/clips`)
411
+ )
412
+ );
413
+ }
203
414
  /**
204
415
  * Reconcile an avatar's clip set after it changes.
205
416
  *
@@ -210,6 +421,10 @@ var RealtimeAvatar = class {
210
421
  * **At most 32 URLs per call.** This is the whole set for the avatar, not a delta, and the
211
422
  * endpoint rejects an oversize list rather than truncating it — so a library that outgrows
212
423
  * 32 needs the set trimmed, not split across two calls.
424
+ *
425
+ * @deprecated The externally-hosted clip tier this serves is sunsetting. Declare the
426
+ * library with {@link setClipLibrary} instead — the platform renders and hosts the
427
+ * clips, and pose-validates uploads against the avatar's rest pose.
213
428
  */
214
429
  async syncClips(avatarId, clipUrls) {
215
430
  const out = await this.#json(
@@ -385,7 +600,17 @@ function toAvatar(raw) {
385
600
  displayName: String(a.displayName ?? ""),
386
601
  sourceKind: a.sourceKind === "video" ? "video" : "image",
387
602
  status: a.status ?? "draft",
388
- defaultVoiceId: a.defaultVoiceId ? String(a.defaultVoiceId) : null
603
+ defaultVoiceId: a.defaultVoiceId ? String(a.defaultVoiceId) : null,
604
+ sourceAssetId: a.sourceAssetId ? String(a.sourceAssetId) : null,
605
+ // Carried because it is the ONLY channel a failed source swap has: she stays `ready`
606
+ // and serving, and this says why the re-shoot did not take.
607
+ error: a.error ? String(a.error) : null,
608
+ // The loop lane's terminal signal, and the reason it is here: a re-direct that fails
609
+ // leaves `status` on `ready` (she is still serving the old loop, which is the whole
610
+ // design) and writes nothing to `error`. Without this field a caller polling after
611
+ // `setLoop` has NO way to distinguish "still rendering" from "gave up", and waits
612
+ // forever. queued → generating → ready | failed.
613
+ idleVideoStatus: a.idleVideoStatus ?? "none"
389
614
  };
390
615
  }
391
616
  function toAsset(raw) {
package/dist/hono.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { P as ProxyConfig } from './types-9leGOgVv.js';
2
- import './types-C5zypiVY.js';
1
+ import { P as ProxyConfig } from './types-CHrhuhYb.js';
2
+ import './types-BeGuvyra.js';
3
3
 
4
4
  /**
5
5
  * Hono (and anything else built on Fetch handlers — Workers, Bun, Deno).
package/dist/hono.js CHANGED
@@ -43,7 +43,7 @@ var RealtimeAvatarHttpError = class extends RealtimeAvatarError {
43
43
 
44
44
  // ../http-client/src/client.ts
45
45
  var DEFAULT_BASE_URL = "https://realtimeavatar.ai/api/v1";
46
- var SDK_VERSION = "0.4.1";
46
+ var SDK_VERSION = "0.5.1";
47
47
  var RealtimeAvatar = class {
48
48
  #apiKey;
49
49
  #baseUrl;
@@ -157,10 +157,15 @@ var RealtimeAvatar = class {
157
157
  /**
158
158
  * Register a character from a looping clip you host.
159
159
  *
160
- * Use a VIDEO source for anything that will be called live. An avatar built from a still
161
- * image reaches `ready`, mints calls, and publishes a BLACK track the status and the
162
- * track dimensions both look fine, only the pixels are wrong. Image sources are for
163
- * offline lipsync renders.
160
+ * @deprecated CLOSED to new callers this answers `422` unless your tenant was already
161
+ * creating from video, in which case it keeps working and your existing avatars are
162
+ * untouched. Use {@link createAvatarFromImage}: one still, and the platform renders the
163
+ * resting loop and the motion library from it.
164
+ *
165
+ * The reason it closed is not arbitrary. Every clip has to start and end on ONE rest pose
166
+ * or a state switch reads as a jump, and the platform can only guarantee that when it
167
+ * rendered the loop and the clips from the same portrait. A supplied video cannot honour
168
+ * it, so the lane could never be made to look right.
164
169
  */
165
170
  async createAvatarFromVideo(input) {
166
171
  const asset = await this.createRemoteAsset({ kind: "video", remoteUrl: input.videoUrl });
@@ -173,12 +178,41 @@ var RealtimeAvatar = class {
173
178
  metadata: input.metadata
174
179
  });
175
180
  }
181
+ /**
182
+ * Register a character from ONE still image. The platform generates everything moving:
183
+ * the resting loop she idles in, then a starter motion library rendered against her rest
184
+ * pose. No footage, no clip URLs, nothing to shoot.
185
+ *
186
+ * `motionPrompt` directs the RESTING LOOP — the video she plays when nothing else is
187
+ * happening — and it is the only chance to direct it, because there is no API today that
188
+ * re-generates a loop after creation (see {@link updateAvatar} for the one thing that can
189
+ * be re-pointed). Describe a small closed arc that returns to where it started: "settles
190
+ * into frame, breathes gently, a slow blink". Omit it and the house default is used.
191
+ *
192
+ * Creation returns while the avatar is still `preprocessing`; poll {@link getAvatar} until
193
+ * it leaves that state. The loop is load-bearing, so a failure there settles `failed` with
194
+ * a readable error — the motion library is not, and a library failure degrades to
195
+ * loop-only rather than demoting the character.
196
+ */
197
+ async createAvatarFromImage(input) {
198
+ const asset = await this.createRemoteAsset({ kind: "image", remoteUrl: input.imageUrl });
199
+ return this.createAvatar({
200
+ displayName: input.displayName,
201
+ sourceKind: "image",
202
+ sourceAssetId: asset.id,
203
+ motionPrompt: input.motionPrompt,
204
+ voice: input.voice,
205
+ settings: input.settings,
206
+ metadata: input.metadata
207
+ });
208
+ }
176
209
  async createAvatar(input) {
177
210
  const body = {
178
211
  displayName: input.displayName,
179
212
  sourceKind: input.sourceKind,
180
213
  sourceAssetId: input.sourceAssetId
181
214
  };
215
+ if (input.motionPrompt !== void 0) body.motionPrompt = input.motionPrompt;
182
216
  if (input.voice !== void 0) body.voice = input.voice;
183
217
  if (input.settings !== void 0) body.settings = input.settings;
184
218
  if (input.metadata !== void 0) body.metadata = input.metadata;
@@ -191,15 +225,192 @@ var RealtimeAvatar = class {
191
225
  async getAvatar(avatarId) {
192
226
  return toAvatar(await this.#json(await this.#request("GET", `/avatars/${avatarId}`)));
193
227
  }
194
- /** Re-point what an avatar already is. `defaultVoiceId: null` clears the default voice. */
228
+ /**
229
+ * Re-point what an avatar already is. `defaultVoiceId: null` clears the default voice.
230
+ *
231
+ * Cheap metadata only. The three things that cost a render have their own methods, because
232
+ * putting them in a patch alongside a rename would hide minutes of GPU work behind a field:
233
+ * {@link retimeAnchor} moves the rest frame, {@link swapSource} replaces the footage, and
234
+ * the resting loop is re-directed by description at `PUT /v1/avatars/{id}/loop`.
235
+ */
195
236
  async updateAvatar(avatarId, patch) {
196
237
  return toAvatar(
197
238
  await this.#json(await this.#request("PATCH", `/avatars/${avatarId}`, { json: patch }))
198
239
  );
199
240
  }
241
+ /**
242
+ * Re-shoot the character — swap in new footage as her resting loop.
243
+ *
244
+ * ASYNCHRONOUS, and that is the whole design. This returns as soon as the swap is
245
+ * accepted; the avatar keeps serving its CURRENT loop, cache and clips the entire time,
246
+ * and cuts over to the new generation in one step once the replacement is prepared.
247
+ * A call minted a second after this returns is a normal call on the old footage.
248
+ *
249
+ * Two consequences worth designing for:
250
+ *
251
+ * - **The clip library empties and refills.** Old takes are footage of the old source and
252
+ * cannot splice against the new loop, so they are dropped and re-rendered. Between the
253
+ * cutover and the last re-render she rests on the new loop with less variety — never
254
+ * broken, just plainer. Do not gate your UI on the library being full.
255
+ * - **A failed swap does not fail the avatar.** She keeps serving, `status` stays `ready`,
256
+ * and the reason lands on `error`. So poll `getAvatar` and read `error` — a non-null
257
+ * `error` on a `ready` avatar is the swap that did not take, not an unhealthy character.
258
+ *
259
+ * The frame this rests on comes from the new footage: pass `anchorTimeMs` when frame 0 of
260
+ * the take is mid-blink. Video-sourced avatars only — a portrait-anchored one is a 422.
261
+ */
262
+ async swapSource(avatarId, input) {
263
+ const json2 = { sourceAssetId: input.sourceAssetId };
264
+ if (input.anchorTimeMs !== void 0) json2.anchorTimeMs = input.anchorTimeMs;
265
+ return toAvatar(await this.#json(await this.#request("PATCH", `/avatars/${avatarId}`, { json: json2 })));
266
+ }
267
+ /**
268
+ * Re-point the anchor at a different frame of the loop she ALREADY has — same footage,
269
+ * different rest pose. `swapSource` replaces the footage; this only moves the frame.
270
+ *
271
+ * Re-renders the clip library the same way, with the same degradation window, and is
272
+ * clamped server-side to the loop's last extractable frame (read the avatar back to see
273
+ * what was actually cut). Video-sourced avatars only.
274
+ */
275
+ async retimeAnchor(avatarId, anchorTimeMs) {
276
+ return toAvatar(
277
+ await this.#json(await this.#request("PATCH", `/avatars/${avatarId}`, { json: { anchorTimeMs } }))
278
+ );
279
+ }
200
280
  async deleteAvatar(avatarId) {
201
281
  await this.#json(await this.#request("DELETE", `/avatars/${avatarId}`));
202
282
  }
283
+ // A clip envelope missing `revision` would silently drop `expectedRevision` from the
284
+ // next declare — CAS degrades to unconditional with zero signal — so it throws instead.
285
+ #clipEnvelope(out) {
286
+ if (typeof out.revision !== "number" || !Array.isArray(out.data)) {
287
+ throw new RealtimeAvatarError("clip library response did not match the contract");
288
+ }
289
+ return out;
290
+ }
291
+ /**
292
+ * Declare the avatar's full desired clip library — a declaration, not a delta. The
293
+ * platform reconciles it against what exists: unchanged clips are `kept` (still
294
+ * serving), new or changed ones are `queued` to render, and clips you dropped are
295
+ * `retired`. The 202 is acceptance, not readiness — poll `listClips` until no row is
296
+ * `queued` or `generating`. A rejected upload settles `failed`, which is terminal, so
297
+ * waiting for all-`ready` waits forever. While a re-render is in flight the previous
298
+ * take keeps serving, so a declaration never blanks a live avatar.
299
+ *
300
+ * `expectedRevision` is compare-and-set: pass the `revision` you last read and a
301
+ * concurrent writer surfaces as a 409 instead of a lost update. Omit it to declare
302
+ * unconditionally.
303
+ *
304
+ * At most 12 clips: one `idle`, up to two `listen`, the rest `gesture`. An uploaded
305
+ * clip (`source: { assetId }`) must start AND end on the avatar's rest pose — pose
306
+ * validation rejects it otherwise (`status: "failed"`, the verdict in `poseCheck`),
307
+ * and the rest of the library is untouched.
308
+ */
309
+ async setClipLibrary(avatarId, library) {
310
+ const body = { clips: library.clips };
311
+ if (library.expectedRevision !== void 0) body.expectedRevision = library.expectedRevision;
312
+ return this.#clipEnvelope(
313
+ await this.#json(
314
+ await this.#request("PUT", `/avatars/${avatarId}/clips`, { json: body })
315
+ )
316
+ );
317
+ }
318
+ /**
319
+ * Re-direct the RESTING LOOP — the video she plays when nothing else is happening — from
320
+ * a new one-sentence description.
321
+ *
322
+ * Not a clip, and this is the distinction integrations get wrong: a clip with
323
+ * `role: "idle"` is a variant spliced OVER the loop, and declaring one never changes what
324
+ * she rests in. This is the only thing that does.
325
+ *
326
+ * `202`, because the render takes minutes. Three properties, all measured against a real
327
+ * render rather than asserted:
328
+ *
329
+ * - **She never goes dark.** She stays `ready` and keeps serving her previous loop for the
330
+ * entire render — returned as `servingUrl` — then the swap publishes in one step.
331
+ * - **Your clip library is untouched.** Clips render against the portrait, not against the
332
+ * loop, so a re-direct re-queues nothing and does not move `revision`.
333
+ * - **It bills once**, at the rendering model's rate, per re-direct.
334
+ *
335
+ * Refusals worth telling apart: `409 loop_pending` (one is already in flight — wait) and
336
+ * `422 loop_not_generatable` (a grandfathered video-sourced avatar has no portrait to
337
+ * re-animate — terminal, do not retry).
338
+ */
339
+ async setLoop(avatarId, loop) {
340
+ return await this.#json(
341
+ await this.#request("PUT", `/avatars/${avatarId}/loop`, { json: { motionPrompt: loop.motionPrompt } })
342
+ );
343
+ }
344
+ /**
345
+ * Block until a loop re-direct settles, and throw if it did not take.
346
+ *
347
+ * `setLoop` returns on ACCEPTANCE; the render runs for minutes afterwards. Every caller
348
+ * therefore writes the same polling loop, and the obvious version of it never terminates
349
+ * on failure — a failed re-direct leaves her `ready` (she is still serving the old loop)
350
+ * and writes nothing to `error`. `idleVideoStatus` is the only field that moves, which is
351
+ * why this exists rather than a doc line telling you to poll.
352
+ *
353
+ * Race-free without a baseline: the platform commits `queued` before `setLoop` returns, so
354
+ * by the time you can call this the status has already left `ready`.
355
+ *
356
+ * Resolves with the settled avatar — `sourceAssetId` is now the new loop. Throws on a
357
+ * failed render and on timeout; a timeout is not a failure, so re-poll or call again.
358
+ */
359
+ async waitForLoop(avatarId, options = {}) {
360
+ const timeoutMs = options.timeoutMs ?? 20 * 6e4;
361
+ const pollMs = Math.max(1e3, options.pollMs ?? 1e4);
362
+ const deadline = Date.now() + timeoutMs;
363
+ for (; ; ) {
364
+ const avatar = await this.getAvatar(avatarId);
365
+ if (avatar.idleVideoStatus === "failed") {
366
+ throw new RealtimeAvatarError(
367
+ `Loop re-direct failed for ${avatarId}. She is still serving her previous loop \u2014 nothing was lost, and you can send another description.`
368
+ );
369
+ }
370
+ if (avatar.idleVideoStatus === "ready" || avatar.idleVideoStatus === "none") return avatar;
371
+ if (Date.now() >= deadline) {
372
+ throw new RealtimeAvatarError(
373
+ `Loop re-direct for ${avatarId} was still ${avatar.idleVideoStatus} after ${Math.round(timeoutMs / 1e3)}s. This is a timeout, not a failure \u2014 the render may still land.`
374
+ );
375
+ }
376
+ await sleep(pollMs);
377
+ }
378
+ }
379
+ /**
380
+ * Block until every clip in the library has stopped moving.
381
+ *
382
+ * Settled means no row is `queued` or `generating` — NOT that every row is `ready`.
383
+ * Waiting for all-`ready` is the intuitive version and it hangs forever: a clip rejected
384
+ * by pose validation settles `failed`, which is terminal. So this returns the library
385
+ * with the failures in it and lets you decide; a partial library is a legitimate outcome
386
+ * and the rest of it is already serving.
387
+ *
388
+ * Throws only on timeout.
389
+ */
390
+ async waitForClips(avatarId, options = {}) {
391
+ const timeoutMs = options.timeoutMs ?? 20 * 6e4;
392
+ const pollMs = Math.max(1e3, options.pollMs ?? 1e4);
393
+ const deadline = Date.now() + timeoutMs;
394
+ for (; ; ) {
395
+ const library = await this.listClips(avatarId);
396
+ const moving = library.data.filter((c) => c.status === "queued" || c.status === "generating");
397
+ if (moving.length === 0) return library;
398
+ if (Date.now() >= deadline) {
399
+ throw new RealtimeAvatarError(
400
+ `${moving.length} clip(s) on ${avatarId} were still rendering after ${Math.round(timeoutMs / 1e3)}s: ${moving.map((c) => c.clipId).join(", ")}. This is a timeout, not a failure.`
401
+ );
402
+ }
403
+ await sleep(pollMs);
404
+ }
405
+ }
406
+ /** The avatar's clip library: every non-retired clip, plus revision, anchor and eligibility. */
407
+ async listClips(avatarId) {
408
+ return this.#clipEnvelope(
409
+ await this.#json(
410
+ await this.#request("GET", `/avatars/${avatarId}/clips`)
411
+ )
412
+ );
413
+ }
203
414
  /**
204
415
  * Reconcile an avatar's clip set after it changes.
205
416
  *
@@ -210,6 +421,10 @@ var RealtimeAvatar = class {
210
421
  * **At most 32 URLs per call.** This is the whole set for the avatar, not a delta, and the
211
422
  * endpoint rejects an oversize list rather than truncating it — so a library that outgrows
212
423
  * 32 needs the set trimmed, not split across two calls.
424
+ *
425
+ * @deprecated The externally-hosted clip tier this serves is sunsetting. Declare the
426
+ * library with {@link setClipLibrary} instead — the platform renders and hosts the
427
+ * clips, and pose-validates uploads against the avatar's rest pose.
213
428
  */
214
429
  async syncClips(avatarId, clipUrls) {
215
430
  const out = await this.#json(
@@ -385,7 +600,17 @@ function toAvatar(raw) {
385
600
  displayName: String(a.displayName ?? ""),
386
601
  sourceKind: a.sourceKind === "video" ? "video" : "image",
387
602
  status: a.status ?? "draft",
388
- defaultVoiceId: a.defaultVoiceId ? String(a.defaultVoiceId) : null
603
+ defaultVoiceId: a.defaultVoiceId ? String(a.defaultVoiceId) : null,
604
+ sourceAssetId: a.sourceAssetId ? String(a.sourceAssetId) : null,
605
+ // Carried because it is the ONLY channel a failed source swap has: she stays `ready`
606
+ // and serving, and this says why the re-shoot did not take.
607
+ error: a.error ? String(a.error) : null,
608
+ // The loop lane's terminal signal, and the reason it is here: a re-direct that fails
609
+ // leaves `status` on `ready` (she is still serving the old loop, which is the whole
610
+ // design) and writes nothing to `error`. Without this field a caller polling after
611
+ // `setLoop` has NO way to distinguish "still rendering" from "gave up", and waits
612
+ // forever. queued → generating → ready | failed.
613
+ idleVideoStatus: a.idleVideoStatus ?? "none"
389
614
  };
390
615
  }
391
616
  function toAsset(raw) {