vidspotai-shared 1.0.136 → 1.0.138
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/lib/globals/aiModels/enums.d.ts +2 -0
- package/lib/globals/aiModels/enums.d.ts.map +1 -1
- package/lib/globals/aiModels/enums.js +7 -0
- package/lib/globals/aiModels/index.d.ts.map +1 -1
- package/lib/globals/aiModels/index.js +2 -0
- package/lib/globals/film/filmCatalog.d.ts +88 -0
- package/lib/globals/film/filmCatalog.d.ts.map +1 -0
- package/lib/globals/film/filmCatalog.js +218 -0
- package/lib/globals/index.d.ts +2 -0
- package/lib/globals/index.d.ts.map +1 -1
- package/lib/globals/index.js +2 -0
- package/lib/globals/reelRemake.d.ts +116 -0
- package/lib/globals/reelRemake.d.ts.map +1 -0
- package/lib/globals/reelRemake.js +142 -0
- package/lib/globals/types.d.ts +15 -1
- package/lib/globals/types.d.ts.map +1 -1
- package/lib/globals/types.js +16 -0
- package/lib/libs/storage/cdnUrl.d.ts +1 -3
- package/lib/libs/storage/cdnUrl.d.ts.map +1 -1
- package/lib/libs/storage/cdnUrl.js +8 -6
- package/lib/models/filmProject.model.d.ts +424 -0
- package/lib/models/filmProject.model.d.ts.map +1 -0
- package/lib/models/filmProject.model.js +14 -0
- package/lib/models/image.model.d.ts +3 -0
- package/lib/models/image.model.d.ts.map +1 -1
- package/lib/models/index.d.ts +2 -0
- package/lib/models/index.d.ts.map +1 -1
- package/lib/models/index.js +2 -0
- package/lib/models/reelRemake.model.d.ts +222 -0
- package/lib/models/reelRemake.model.d.ts.map +1 -0
- package/lib/models/reelRemake.model.js +2 -0
- package/lib/models/video.model.d.ts +5 -0
- package/lib/models/video.model.d.ts.map +1 -1
- package/lib/schemas/film.schema.d.ts +500 -0
- package/lib/schemas/film.schema.d.ts.map +1 -0
- package/lib/schemas/film.schema.js +201 -0
- package/lib/schemas/index.d.ts +1 -0
- package/lib/schemas/index.d.ts.map +1 -1
- package/lib/schemas/index.js +1 -0
- package/lib/services/agent/providerFallback/chains.d.ts.map +1 -1
- package/lib/services/agent/providerFallback/chains.js +13 -0
- package/lib/services/agent/tools/generateVideo.tool.d.ts.map +1 -1
- package/lib/services/agent/tools/generateVideo.tool.js +2 -1
- package/lib/services/aiGen/aiGenFactory.service.d.ts.map +1 -1
- package/lib/services/aiGen/aiGenFactory.service.js +4 -0
- package/lib/services/aiGen/providers/bytedance/bytedance.service.d.ts.map +1 -1
- package/lib/services/aiGen/providers/bytedance/bytedance.service.js +17 -0
- package/lib/services/aiGen/providers/fal/index.d.ts +1 -0
- package/lib/services/aiGen/providers/fal/index.d.ts.map +1 -1
- package/lib/services/aiGen/providers/fal/index.js +1 -0
- package/lib/services/aiGen/providers/kling/klingRequests.d.ts.map +1 -1
- package/lib/services/aiGen/providers/kling/klingRequests.js +3 -0
- package/lib/services/aiGen/providers/types.d.ts +6 -0
- package/lib/services/aiGen/providers/types.d.ts.map +1 -1
- package/lib/services/film/compileShotPrompt.d.ts +16 -0
- package/lib/services/film/compileShotPrompt.d.ts.map +1 -0
- package/lib/services/film/compileShotPrompt.js +152 -0
- package/lib/services/film/filmAssets.d.ts +73 -0
- package/lib/services/film/filmAssets.d.ts.map +1 -0
- package/lib/services/film/filmAssets.js +341 -0
- package/lib/services/film/filmCut.d.ts +159 -0
- package/lib/services/film/filmCut.d.ts.map +1 -0
- package/lib/services/film/filmCut.js +616 -0
- package/lib/services/film/filmPublic.d.ts +9 -0
- package/lib/services/film/filmPublic.d.ts.map +1 -0
- package/lib/services/film/filmPublic.js +39 -0
- package/lib/services/film/filmTakes.d.ts +65 -0
- package/lib/services/film/filmTakes.d.ts.map +1 -0
- package/lib/services/film/filmTakes.js +168 -0
- package/lib/services/film/writersRoom.d.ts +276 -0
- package/lib/services/film/writersRoom.d.ts.map +1 -0
- package/lib/services/film/writersRoom.js +431 -0
- package/lib/services/firestore.service.d.ts +7 -0
- package/lib/services/firestore.service.d.ts.map +1 -1
- package/lib/services/firestore.service.js +15 -0
- package/lib/services/index.d.ts +12 -0
- package/lib/services/index.d.ts.map +1 -1
- package/lib/services/index.js +12 -0
- package/lib/services/providerHealth.service.d.ts +12 -0
- package/lib/services/providerHealth.service.d.ts.map +1 -0
- package/lib/services/providerHealth.service.js +75 -0
- package/lib/services/reelRemake/castDetection.d.ts +56 -0
- package/lib/services/reelRemake/castDetection.d.ts.map +1 -0
- package/lib/services/reelRemake/castDetection.js +146 -0
- package/lib/services/reelRemake/castSuggestion.d.ts +43 -0
- package/lib/services/reelRemake/castSuggestion.d.ts.map +1 -0
- package/lib/services/reelRemake/castSuggestion.js +99 -0
- package/lib/services/reelRemake/keyframeSwap.d.ts +44 -0
- package/lib/services/reelRemake/keyframeSwap.d.ts.map +1 -0
- package/lib/services/reelRemake/keyframeSwap.js +139 -0
- package/lib/services/reelRemake/shootPlan.d.ts +65 -0
- package/lib/services/reelRemake/shootPlan.d.ts.map +1 -0
- package/lib/services/reelRemake/shootPlan.js +193 -0
- package/lib/services/reelRemake/stopWork.d.ts +54 -0
- package/lib/services/reelRemake/stopWork.d.ts.map +1 -0
- package/lib/services/reelRemake/stopWork.js +91 -0
- package/lib/services/translation/translation.service.d.ts +10 -1
- package/lib/services/translation/translation.service.d.ts.map +1 -1
- package/lib/services/translation/translation.service.js +34 -1
- package/lib/utils/errors.d.ts +3 -0
- package/lib/utils/errors.d.ts.map +1 -1
- package/lib/utils/errors.js +6 -0
- package/lib/utils/helpers.d.ts +1 -1
- package/lib/utils/helpers.d.ts.map +1 -1
- package/lib/utils/helpers.js +2 -1
- package/package.json +1 -1
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PROVIDER_DEPLETION_WINDOW_MS = void 0;
|
|
4
|
+
exports.markModelAccountDepleted = markModelAccountDepleted;
|
|
5
|
+
exports.isModelAccountDepleted = isModelAccountDepleted;
|
|
6
|
+
const firestore_1 = require("firebase-admin/firestore");
|
|
7
|
+
const firebase_1 = require("../libs/firebase");
|
|
8
|
+
const logger_1 = require("../utils/logger");
|
|
9
|
+
// Provider-account depletion breaker (2026-11-02).
|
|
10
|
+
//
|
|
11
|
+
// When a provider account runs dry (Kling 1102 "Account balance not enough",
|
|
12
|
+
// PixVerse quota, ...) every job for that model fails at submit until an
|
|
13
|
+
// operator tops up. Prod took ~25 kling-v4.0 jobs over four days that way:
|
|
14
|
+
// each one deducted credits, queued, failed in the worker, refunded, and
|
|
15
|
+
// paged Slack. The worker now records the depletion here; the API checks it
|
|
16
|
+
// before charging, so users get an immediate "pick another model" instead of
|
|
17
|
+
// a doomed job, and the pager fires about once per window instead of per job.
|
|
18
|
+
//
|
|
19
|
+
// Firestore rather than Redis: the API (Cloud Run, many instances) doesn't
|
|
20
|
+
// hold Redis clients and prod Redis has a ~30-client ceiling.
|
|
21
|
+
const COLLECTION = "providerHealth";
|
|
22
|
+
/** How long a depletion blocks new jobs. Short on purpose — after a top-up
|
|
23
|
+
* the model comes back on its own within this window. */
|
|
24
|
+
exports.PROVIDER_DEPLETION_WINDOW_MS = 10 * 60000;
|
|
25
|
+
const READ_CACHE_MS = 30000;
|
|
26
|
+
const readCache = new Map();
|
|
27
|
+
/** Worker side: record that `modelKey`'s provider account is out of quota. */
|
|
28
|
+
async function markModelAccountDepleted(modelKey, code) {
|
|
29
|
+
const until = Date.now() + exports.PROVIDER_DEPLETION_WINDOW_MS;
|
|
30
|
+
try {
|
|
31
|
+
const doc = {
|
|
32
|
+
modelKey,
|
|
33
|
+
depletedUntil: firestore_1.Timestamp.fromMillis(until),
|
|
34
|
+
code,
|
|
35
|
+
updatedAt: firestore_1.Timestamp.now(),
|
|
36
|
+
};
|
|
37
|
+
await firebase_1.firestore.collection(COLLECTION).doc(modelKey).set(doc);
|
|
38
|
+
readCache.set(modelKey, { at: Date.now(), depletedUntilMs: until });
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
// Best-effort: failing to record only loses the early rejection.
|
|
42
|
+
logger_1.logger.warn("providerHealth: failed to record depletion", {
|
|
43
|
+
modelKey,
|
|
44
|
+
err: err instanceof Error ? err.message : String(err),
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* API side: true while `modelKey` is inside a recorded depletion window.
|
|
50
|
+
* Cached per instance for READ_CACHE_MS; fails open (false) on read errors so
|
|
51
|
+
* a Firestore hiccup never blocks generation.
|
|
52
|
+
*/
|
|
53
|
+
async function isModelAccountDepleted(modelKey) {
|
|
54
|
+
const now = Date.now();
|
|
55
|
+
const cached = readCache.get(modelKey);
|
|
56
|
+
if (!cached || now - cached.at > READ_CACHE_MS) {
|
|
57
|
+
try {
|
|
58
|
+
const snap = await firebase_1.firestore.collection(COLLECTION).doc(modelKey).get();
|
|
59
|
+
const data = snap.exists ? snap.data() : undefined;
|
|
60
|
+
readCache.set(modelKey, {
|
|
61
|
+
at: now,
|
|
62
|
+
depletedUntilMs: data?.depletedUntil ? data.depletedUntil.toMillis() : null,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
logger_1.logger.warn("providerHealth: read failed — treating model as available", {
|
|
67
|
+
modelKey,
|
|
68
|
+
err: err instanceof Error ? err.message : String(err),
|
|
69
|
+
});
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const until = readCache.get(modelKey)?.depletedUntilMs;
|
|
74
|
+
return !!until && until > now;
|
|
75
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reel Remake — cast detection. One Gemini vision call over every shot's
|
|
3
|
+
* keyframe at once, so the same person gets the same id across shots (the
|
|
4
|
+
* model sees them side by side; per-shot calls would need a second matching
|
|
5
|
+
* pass). Returns who appears where, with a box per appearance that the
|
|
6
|
+
* keyframe swap later targets.
|
|
7
|
+
*
|
|
8
|
+
* Cost: ~24 small JPEGs ≈ 24 × 258 tokens + a short JSON answer — well under
|
|
9
|
+
* $0.01 per remake on Gemini Flash.
|
|
10
|
+
*/
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
import type { ReelRemakePerson, ReelRemakeShot } from "../../models/reelRemake.model";
|
|
13
|
+
export declare const CastDetectionSchema: z.ZodObject<{
|
|
14
|
+
people: z.ZodArray<z.ZodObject<{
|
|
15
|
+
id: z.ZodString;
|
|
16
|
+
label: z.ZodString;
|
|
17
|
+
description: z.ZodString;
|
|
18
|
+
appearances: z.ZodArray<z.ZodObject<{
|
|
19
|
+
shotIndex: z.ZodNumber;
|
|
20
|
+
box: z.ZodObject<{
|
|
21
|
+
ymin: z.ZodNumber;
|
|
22
|
+
xmin: z.ZodNumber;
|
|
23
|
+
ymax: z.ZodNumber;
|
|
24
|
+
xmax: z.ZodNumber;
|
|
25
|
+
}, z.core.$strip>;
|
|
26
|
+
prominence: z.ZodEnum<{
|
|
27
|
+
background: "background";
|
|
28
|
+
primary: "primary";
|
|
29
|
+
secondary: "secondary";
|
|
30
|
+
}>;
|
|
31
|
+
}, z.core.$strip>>;
|
|
32
|
+
}, z.core.$strip>>;
|
|
33
|
+
}, z.core.$strip>;
|
|
34
|
+
export type CastDetection = z.infer<typeof CastDetectionSchema>;
|
|
35
|
+
export interface DetectCastArgs {
|
|
36
|
+
apiKey: string;
|
|
37
|
+
/** JPEG bytes, index-aligned with `shots`. */
|
|
38
|
+
keyframes: Buffer[];
|
|
39
|
+
shots: Pick<ReelRemakeShot, "id" | "index">[];
|
|
40
|
+
model?: string;
|
|
41
|
+
timeoutMs?: number;
|
|
42
|
+
}
|
|
43
|
+
export interface DetectCastResult {
|
|
44
|
+
cast: ReelRemakePerson[];
|
|
45
|
+
/** Per shot (by id): the people in it, for `ReelRemakeShot.people`. */
|
|
46
|
+
peopleByShot: Map<string, ReelRemakeShot["people"]>;
|
|
47
|
+
}
|
|
48
|
+
export declare function detectCast(args: DetectCastArgs): Promise<DetectCastResult>;
|
|
49
|
+
/**
|
|
50
|
+
* Model output → our shape: drop out-of-range shots and degenerate boxes,
|
|
51
|
+
* merge duplicate appearances in one shot, rank people by how much they're on
|
|
52
|
+
* screen, relabel A, B, C… in that order, and cap at MAX_PEOPLE.
|
|
53
|
+
* @internal exported for tests
|
|
54
|
+
*/
|
|
55
|
+
export declare function normalizeCast(detection: CastDetection, shots: Pick<ReelRemakeShot, "id" | "index">[]): DetectCastResult;
|
|
56
|
+
//# sourceMappingURL=castDetection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"castDetection.d.ts","sourceRoot":"","sources":["../../../src/services/reelRemake/castDetection.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAatF,eAAO,MAAM,mBAAmB;;;;;;;;;;;;;;;;;;;;iBAiB9B,CAAC;AACH,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAchE,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,8CAA8C;IAC9C,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,KAAK,EAAE,IAAI,CAAC,cAAc,EAAE,IAAI,GAAG,OAAO,CAAC,EAAE,CAAC;IAC9C,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,gBAAgB,EAAE,CAAC;IACzB,uEAAuE;IACvE,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,QAAQ,CAAC,CAAC,CAAC;CACrD;AAED,wBAAsB,UAAU,CAAC,IAAI,EAAE,cAAc,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAsChF;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAC3B,SAAS,EAAE,aAAa,EACxB,KAAK,EAAE,IAAI,CAAC,cAAc,EAAE,IAAI,GAAG,OAAO,CAAC,EAAE,GAC5C,gBAAgB,CA+ClB"}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Reel Remake — cast detection. One Gemini vision call over every shot's
|
|
4
|
+
* keyframe at once, so the same person gets the same id across shots (the
|
|
5
|
+
* model sees them side by side; per-shot calls would need a second matching
|
|
6
|
+
* pass). Returns who appears where, with a box per appearance that the
|
|
7
|
+
* keyframe swap later targets.
|
|
8
|
+
*
|
|
9
|
+
* Cost: ~24 small JPEGs ≈ 24 × 258 tokens + a short JSON answer — well under
|
|
10
|
+
* $0.01 per remake on Gemini Flash.
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.CastDetectionSchema = void 0;
|
|
14
|
+
exports.detectCast = detectCast;
|
|
15
|
+
exports.normalizeCast = normalizeCast;
|
|
16
|
+
const genai_1 = require("@google/genai");
|
|
17
|
+
const zod_1 = require("zod");
|
|
18
|
+
const reelRemake_1 = require("../../globals/reelRemake");
|
|
19
|
+
const logger_1 = require("../../utils/logger");
|
|
20
|
+
/** Most people a remake tracks; extras are usually passers-by. */
|
|
21
|
+
const MAX_PEOPLE = 8;
|
|
22
|
+
const BoxSchema = zod_1.z.object({
|
|
23
|
+
ymin: zod_1.z.number(),
|
|
24
|
+
xmin: zod_1.z.number(),
|
|
25
|
+
ymax: zod_1.z.number(),
|
|
26
|
+
xmax: zod_1.z.number(),
|
|
27
|
+
});
|
|
28
|
+
exports.CastDetectionSchema = zod_1.z.object({
|
|
29
|
+
people: zod_1.z.array(zod_1.z.object({
|
|
30
|
+
id: zod_1.z.string().describe("Your own stable id for this person, e.g. p1"),
|
|
31
|
+
label: zod_1.z.string().describe("3-6 word identifying label, e.g. 'man in black bucket hat'"),
|
|
32
|
+
description: zod_1.z
|
|
33
|
+
.string()
|
|
34
|
+
.describe("Literal appearance: gender presentation, age range, build, hair, clothing, accessories"),
|
|
35
|
+
appearances: zod_1.z.array(zod_1.z.object({
|
|
36
|
+
shotIndex: zod_1.z.number().int(),
|
|
37
|
+
box: BoxSchema.describe("Bounding box of this person's whole visible body, 0-1000 scale"),
|
|
38
|
+
prominence: zod_1.z.enum(["primary", "secondary", "background"]),
|
|
39
|
+
})),
|
|
40
|
+
})),
|
|
41
|
+
});
|
|
42
|
+
const PROMPT = [
|
|
43
|
+
"These images are keyframes of a short vertical video, one per shot, in order.",
|
|
44
|
+
"Each image is preceded by its label 'Shot N'.",
|
|
45
|
+
"List every distinct real person visible in any shot (ignore people on screens, posters or reflections).",
|
|
46
|
+
"The SAME person appearing in several shots must be ONE entry with several appearances — match by face,",
|
|
47
|
+
"hair, build and clothing, not by position.",
|
|
48
|
+
"For each appearance give the shot number, a bounding box of the person's whole visible body",
|
|
49
|
+
"(ymin, xmin, ymax, xmax on a 0-1000 scale of that image), and prominence:",
|
|
50
|
+
"'primary' = the subject of the shot, 'secondary' = clearly visible but not the focus, 'background' = small or incidental.",
|
|
51
|
+
"Describe each person literally and only from what is visible. No names, no guesses about identity.",
|
|
52
|
+
].join(" ");
|
|
53
|
+
async function detectCast(args) {
|
|
54
|
+
const ai = new genai_1.GoogleGenAI({ apiKey: args.apiKey });
|
|
55
|
+
const parts = [
|
|
56
|
+
{ text: PROMPT },
|
|
57
|
+
];
|
|
58
|
+
args.keyframes.forEach((buf, i) => {
|
|
59
|
+
parts.push({ text: `Shot ${i + 1}` });
|
|
60
|
+
parts.push({ inlineData: { mimeType: "image/jpeg", data: buf.toString("base64") } });
|
|
61
|
+
});
|
|
62
|
+
const resp = await ai.models.generateContent({
|
|
63
|
+
model: args.model ?? reelRemake_1.REEL_REMAKE.castModel,
|
|
64
|
+
contents: [{ role: "user", parts }],
|
|
65
|
+
config: {
|
|
66
|
+
responseMimeType: "application/json",
|
|
67
|
+
responseSchema: zod_1.z.toJSONSchema(exports.CastDetectionSchema, { target: "draft-7" }),
|
|
68
|
+
temperature: 0.1,
|
|
69
|
+
abortSignal: AbortSignal.timeout(args.timeoutMs ?? 120000),
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
const text = resp.text;
|
|
73
|
+
if (!text)
|
|
74
|
+
throw new Error("castDetection: empty response");
|
|
75
|
+
let raw;
|
|
76
|
+
try {
|
|
77
|
+
raw = JSON.parse(text);
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
throw new Error(`castDetection: non-JSON response: ${text.slice(0, 300)}`);
|
|
81
|
+
}
|
|
82
|
+
const parsed = exports.CastDetectionSchema.safeParse(raw);
|
|
83
|
+
if (!parsed.success) {
|
|
84
|
+
throw new Error(`castDetection: schema validation failed: ${parsed.error.message}`);
|
|
85
|
+
}
|
|
86
|
+
const result = normalizeCast(parsed.data, args.shots);
|
|
87
|
+
logger_1.logger.info("castDetection ok", {
|
|
88
|
+
shots: args.shots.length,
|
|
89
|
+
people: result.cast.length,
|
|
90
|
+
});
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Model output → our shape: drop out-of-range shots and degenerate boxes,
|
|
95
|
+
* merge duplicate appearances in one shot, rank people by how much they're on
|
|
96
|
+
* screen, relabel A, B, C… in that order, and cap at MAX_PEOPLE.
|
|
97
|
+
* @internal exported for tests
|
|
98
|
+
*/
|
|
99
|
+
function normalizeCast(detection, shots) {
|
|
100
|
+
const weight = { primary: 3, secondary: 2, background: 1 };
|
|
101
|
+
const clamp = (n) => Math.min(1000, Math.max(0, Math.round(n)));
|
|
102
|
+
const people = detection.people
|
|
103
|
+
.map((p) => {
|
|
104
|
+
const byShot = new Map();
|
|
105
|
+
for (const a of p.appearances) {
|
|
106
|
+
// The prompt numbers shots from 1.
|
|
107
|
+
const idx = a.shotIndex - 1;
|
|
108
|
+
if (idx < 0 || idx >= shots.length)
|
|
109
|
+
continue;
|
|
110
|
+
const box = {
|
|
111
|
+
ymin: clamp(Math.min(a.box.ymin, a.box.ymax)),
|
|
112
|
+
xmin: clamp(Math.min(a.box.xmin, a.box.xmax)),
|
|
113
|
+
ymax: clamp(Math.max(a.box.ymin, a.box.ymax)),
|
|
114
|
+
xmax: clamp(Math.max(a.box.xmin, a.box.xmax)),
|
|
115
|
+
};
|
|
116
|
+
if (box.ymax - box.ymin < 10 || box.xmax - box.xmin < 10)
|
|
117
|
+
continue;
|
|
118
|
+
const prev = byShot.get(idx);
|
|
119
|
+
if (!prev || weight[a.prominence] > weight[prev.prominence]) {
|
|
120
|
+
byShot.set(idx, { ...a, shotIndex: idx, box });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const score = [...byShot.values()].reduce((s, a) => s + weight[a.prominence], 0);
|
|
124
|
+
return { p, byShot, score };
|
|
125
|
+
})
|
|
126
|
+
.filter((x) => x.byShot.size > 0)
|
|
127
|
+
.sort((a, b) => b.score - a.score)
|
|
128
|
+
.slice(0, MAX_PEOPLE);
|
|
129
|
+
const peopleByShot = new Map(shots.map((s) => [s.id, []]));
|
|
130
|
+
const cast = people.map(({ p, byShot }, i) => {
|
|
131
|
+
const id = String.fromCharCode(65 + i);
|
|
132
|
+
const shotIds = [];
|
|
133
|
+
for (const [idx, a] of [...byShot.entries()].sort((x, y) => x[0] - y[0])) {
|
|
134
|
+
const shot = shots[idx];
|
|
135
|
+
shotIds.push(shot.id);
|
|
136
|
+
peopleByShot.get(shot.id).push({ personId: id, box: a.box, prominence: a.prominence });
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
id,
|
|
140
|
+
label: p.label.trim(),
|
|
141
|
+
description: p.description.trim(),
|
|
142
|
+
shotIds,
|
|
143
|
+
};
|
|
144
|
+
});
|
|
145
|
+
return { cast, peopleByShot };
|
|
146
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reel Remake — theme → proposed cast. One Gemini text call: given a theme
|
|
3
|
+
* ("wizard school", "zombies") and the detected people, propose an original
|
|
4
|
+
* character per person. The user edits the result before anything is billed.
|
|
5
|
+
*
|
|
6
|
+
* Characters are original archetypes only — no franchise characters, real
|
|
7
|
+
* people or brand names (notes/REEL_REMAKE_PLAN.md §7; brand words also leak
|
|
8
|
+
* real logos into the generation, measured in the spike).
|
|
9
|
+
*/
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
import { type ReelRemakeMode } from "../../globals/reelRemake";
|
|
12
|
+
import type { ReelRemakePerson } from "../../models/reelRemake.model";
|
|
13
|
+
export declare const CastSuggestionSchema: z.ZodObject<{
|
|
14
|
+
characters: z.ZodArray<z.ZodObject<{
|
|
15
|
+
personId: z.ZodString;
|
|
16
|
+
name: z.ZodString;
|
|
17
|
+
prompt: z.ZodString;
|
|
18
|
+
}, z.core.$strip>>;
|
|
19
|
+
}, z.core.$strip>;
|
|
20
|
+
export type CastSuggestion = z.infer<typeof CastSuggestionSchema>;
|
|
21
|
+
export interface SuggestCastArgs {
|
|
22
|
+
apiKey: string;
|
|
23
|
+
theme: string;
|
|
24
|
+
mode: ReelRemakeMode;
|
|
25
|
+
cast: Pick<ReelRemakePerson, "id" | "label" | "description">[];
|
|
26
|
+
model?: string;
|
|
27
|
+
timeoutMs?: number;
|
|
28
|
+
}
|
|
29
|
+
export interface SuggestedCharacter {
|
|
30
|
+
personId: string;
|
|
31
|
+
name: string;
|
|
32
|
+
prompt: string;
|
|
33
|
+
}
|
|
34
|
+
export declare function buildCastSuggestionPrompt(args: Pick<SuggestCastArgs, "theme" | "mode" | "cast">): string;
|
|
35
|
+
export declare function suggestCast(args: SuggestCastArgs): Promise<SuggestedCharacter[]>;
|
|
36
|
+
/**
|
|
37
|
+
* Model output → one character per known person, in cast order: drop unknown
|
|
38
|
+
* ids and empties, keep the first entry per person, clip to the PUT /cast
|
|
39
|
+
* limits so a suggestion can always be saved as-is.
|
|
40
|
+
* @internal exported for tests
|
|
41
|
+
*/
|
|
42
|
+
export declare function normalizeCastSuggestion(suggestion: CastSuggestion, cast: Pick<ReelRemakePerson, "id">[]): SuggestedCharacter[];
|
|
43
|
+
//# sourceMappingURL=castSuggestion.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"castSuggestion.d.ts","sourceRoot":"","sources":["../../../src/services/reelRemake/castSuggestion.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAe,KAAK,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC5E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AAGtE,eAAO,MAAM,oBAAoB;;;;;;iBAY/B,CAAC;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAElE,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,cAAc,CAAC;IACrB,IAAI,EAAE,IAAI,CAAC,gBAAgB,EAAE,IAAI,GAAG,OAAO,GAAG,aAAa,CAAC,EAAE,CAAC;IAC/D,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,IAAI,CAAC,eAAe,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,CAAC,GAAG,MAAM,CAmBxG;AAED,wBAAsB,WAAW,CAAC,IAAI,EAAE,eAAe,GAAG,OAAO,CAAC,kBAAkB,EAAE,CAAC,CA2BtF;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,CACrC,UAAU,EAAE,cAAc,EAC1B,IAAI,EAAE,IAAI,CAAC,gBAAgB,EAAE,IAAI,CAAC,EAAE,GACnC,kBAAkB,EAAE,CAatB"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Reel Remake — theme → proposed cast. One Gemini text call: given a theme
|
|
4
|
+
* ("wizard school", "zombies") and the detected people, propose an original
|
|
5
|
+
* character per person. The user edits the result before anything is billed.
|
|
6
|
+
*
|
|
7
|
+
* Characters are original archetypes only — no franchise characters, real
|
|
8
|
+
* people or brand names (notes/REEL_REMAKE_PLAN.md §7; brand words also leak
|
|
9
|
+
* real logos into the generation, measured in the spike).
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.CastSuggestionSchema = void 0;
|
|
13
|
+
exports.buildCastSuggestionPrompt = buildCastSuggestionPrompt;
|
|
14
|
+
exports.suggestCast = suggestCast;
|
|
15
|
+
exports.normalizeCastSuggestion = normalizeCastSuggestion;
|
|
16
|
+
const genai_1 = require("@google/genai");
|
|
17
|
+
const zod_1 = require("zod");
|
|
18
|
+
const reelRemake_1 = require("../../globals/reelRemake");
|
|
19
|
+
const logger_1 = require("../../utils/logger");
|
|
20
|
+
exports.CastSuggestionSchema = zod_1.z.object({
|
|
21
|
+
characters: zod_1.z.array(zod_1.z.object({
|
|
22
|
+
personId: zod_1.z.string().describe("The person's id exactly as given, e.g. A"),
|
|
23
|
+
name: zod_1.z.string().describe("Character name, e.g. 'Hagrid' or 'Bearded Forest Wizard'"),
|
|
24
|
+
prompt: zod_1.z
|
|
25
|
+
.string()
|
|
26
|
+
.describe("Literal visual description of the character: face, hair, build, outfit, accessories. One or two sentences."),
|
|
27
|
+
})),
|
|
28
|
+
});
|
|
29
|
+
function buildCastSuggestionPrompt(args) {
|
|
30
|
+
const people = args.cast.map((p) => `- ${p.id}: ${p.label} — ${p.description}`).join("\n");
|
|
31
|
+
return [
|
|
32
|
+
"A short vertical video is being remade with a new cast. Each person below will be replaced by a character.",
|
|
33
|
+
`Theme: "${args.theme}".`,
|
|
34
|
+
args.mode === "new-world"
|
|
35
|
+
? "The characters will redo the same moves in a new setting that fits the theme."
|
|
36
|
+
: "The characters will replace the people in place; the setting stays as it is.",
|
|
37
|
+
"Propose exactly one character per person, all fitting the theme and distinct from each other.",
|
|
38
|
+
"Keep each character's build, age range and gender presentation close to the person they replace,",
|
|
39
|
+
"so the original motion still reads naturally.",
|
|
40
|
+
"Follow the theme as asked. If it names a film, show, game, book, team or brand, cast its ACTUAL characters",
|
|
41
|
+
"by their real names (theme 'Harry Potter' → 'Severus Snape', 'Hermione Granger', not a generic wizard),",
|
|
42
|
+
"with their canonical look, costumes and insignia. Only invent characters when the theme is generic.",
|
|
43
|
+
"Describe only what is visible — no backstory.",
|
|
44
|
+
"",
|
|
45
|
+
"People:",
|
|
46
|
+
people,
|
|
47
|
+
].join("\n");
|
|
48
|
+
}
|
|
49
|
+
async function suggestCast(args) {
|
|
50
|
+
const ai = new genai_1.GoogleGenAI({ apiKey: args.apiKey });
|
|
51
|
+
const resp = await ai.models.generateContent({
|
|
52
|
+
model: args.model ?? reelRemake_1.REEL_REMAKE.castModel,
|
|
53
|
+
contents: [{ role: "user", parts: [{ text: buildCastSuggestionPrompt(args) }] }],
|
|
54
|
+
config: {
|
|
55
|
+
responseMimeType: "application/json",
|
|
56
|
+
responseSchema: zod_1.z.toJSONSchema(exports.CastSuggestionSchema, { target: "draft-7" }),
|
|
57
|
+
temperature: 0.8,
|
|
58
|
+
abortSignal: AbortSignal.timeout(args.timeoutMs ?? 45000),
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
const text = resp.text;
|
|
62
|
+
if (!text)
|
|
63
|
+
throw new Error("castSuggestion: empty response");
|
|
64
|
+
let raw;
|
|
65
|
+
try {
|
|
66
|
+
raw = JSON.parse(text);
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
throw new Error(`castSuggestion: non-JSON response: ${text.slice(0, 300)}`);
|
|
70
|
+
}
|
|
71
|
+
const parsed = exports.CastSuggestionSchema.safeParse(raw);
|
|
72
|
+
if (!parsed.success) {
|
|
73
|
+
throw new Error(`castSuggestion: schema validation failed: ${parsed.error.message}`);
|
|
74
|
+
}
|
|
75
|
+
const characters = normalizeCastSuggestion(parsed.data, args.cast);
|
|
76
|
+
logger_1.logger.info("castSuggestion ok", { people: args.cast.length, characters: characters.length });
|
|
77
|
+
return characters;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Model output → one character per known person, in cast order: drop unknown
|
|
81
|
+
* ids and empties, keep the first entry per person, clip to the PUT /cast
|
|
82
|
+
* limits so a suggestion can always be saved as-is.
|
|
83
|
+
* @internal exported for tests
|
|
84
|
+
*/
|
|
85
|
+
function normalizeCastSuggestion(suggestion, cast) {
|
|
86
|
+
const byId = new Map();
|
|
87
|
+
for (const c of suggestion.characters) {
|
|
88
|
+
const personId = c.personId.trim().toUpperCase();
|
|
89
|
+
const name = c.name.trim().slice(0, reelRemake_1.REEL_REMAKE.maxCharacterNameChars);
|
|
90
|
+
const prompt = c.prompt.trim().slice(0, reelRemake_1.REEL_REMAKE.maxCharacterPromptChars);
|
|
91
|
+
if (!name || !prompt || byId.has(personId))
|
|
92
|
+
continue;
|
|
93
|
+
byId.set(personId, { personId, name, prompt });
|
|
94
|
+
}
|
|
95
|
+
return cast.flatMap((p) => {
|
|
96
|
+
const c = byId.get(p.id);
|
|
97
|
+
return c ? [c] : [];
|
|
98
|
+
});
|
|
99
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reel Remake — preview (Stage 3). Pure planning + prompt building for the
|
|
3
|
+
* billed preview: one portrait per text-only character (the identity anchor
|
|
4
|
+
* every shot reuses) and one swapped keyframe per shot that has a mapped
|
|
5
|
+
* person. The worker runs the image calls; Functions charges from the plan.
|
|
6
|
+
*/
|
|
7
|
+
import type { IReelRemakeModel, ReelRemakeBox, ReelRemakeCharacter, ReelRemakePerson, ReelRemakeShot } from "../../models/reelRemake.model";
|
|
8
|
+
export interface ReelPreviewPlan {
|
|
9
|
+
/** Mapped people with no reference image — each gets a generated portrait. */
|
|
10
|
+
anchorPersonIds: string[];
|
|
11
|
+
/** Shots with at least one mapped person — each gets a swapped keyframe. */
|
|
12
|
+
shotIds: string[];
|
|
13
|
+
}
|
|
14
|
+
export declare function planPreview(remake: Pick<IReelRemakeModel, "cast" | "shots">): ReelPreviewPlan;
|
|
15
|
+
/** Images that define how a character looks: their own refs, else the generated portrait. */
|
|
16
|
+
export declare function characterRefs(character: ReelRemakeCharacter): string[];
|
|
17
|
+
/** Credits one preview image costs on the user's plan (same floor rule as /v1/image). */
|
|
18
|
+
export declare function reelPreviewImageCredits(numInputImages: number, paid: boolean): number;
|
|
19
|
+
/** Number of input images a shot's swap sends (keyframe + character refs). Anchors count as one. */
|
|
20
|
+
export declare function swapInputCount(shot: Pick<ReelRemakeShot, "people">, cast: ReelRemakePerson[]): number;
|
|
21
|
+
export declare function buildAnchorPrompt(person: Pick<ReelRemakePerson, "description">, character: ReelRemakeCharacter): string;
|
|
22
|
+
/** "on the left" / "in the centre" / "on the right" from a 0..1000 box. */
|
|
23
|
+
export declare function horizontalPosition(box: ReelRemakeBox): string;
|
|
24
|
+
export interface SwapRequest {
|
|
25
|
+
prompt: string;
|
|
26
|
+
/** Keyframe first, then each character's refs in prompt order. */
|
|
27
|
+
inputImageUrls: string[];
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Edit instruction for one shot: every mapped person in it becomes their
|
|
31
|
+
* character in a single edit, everyone else and the scene stay. Throws if a
|
|
32
|
+
* mapped person has no usable reference (their portrait failed) — the caller
|
|
33
|
+
* fails that shot.
|
|
34
|
+
*/
|
|
35
|
+
export declare function buildSwapRequest(args: {
|
|
36
|
+
shot: Pick<ReelRemakeShot, "keyframeUrl" | "people">;
|
|
37
|
+
cast: ReelRemakePerson[];
|
|
38
|
+
mode: IReelRemakeModel["mode"];
|
|
39
|
+
theme?: string;
|
|
40
|
+
note?: string;
|
|
41
|
+
}): SwapRequest;
|
|
42
|
+
/** Closest aspect ratio the preview model accepts for a w×h frame. */
|
|
43
|
+
export declare function previewAspectRatio(width: number, height: number): string;
|
|
44
|
+
//# sourceMappingURL=keyframeSwap.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keyframeSwap.d.ts","sourceRoot":"","sources":["../../../src/services/reelRemake/keyframeSwap.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,OAAO,KAAK,EACV,gBAAgB,EAChB,aAAa,EACb,mBAAmB,EACnB,gBAAgB,EAChB,cAAc,EACf,MAAM,+BAA+B,CAAC;AAMvC,MAAM,WAAW,eAAe;IAC9B,8EAA8E;IAC9E,eAAe,EAAE,MAAM,EAAE,CAAC;IAC1B,4EAA4E;IAC5E,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB;AAED,wBAAgB,WAAW,CAAC,MAAM,EAAE,IAAI,CAAC,gBAAgB,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,eAAe,CAY7F;AAED,6FAA6F;AAC7F,wBAAgB,aAAa,CAAC,SAAS,EAAE,mBAAmB,GAAG,MAAM,EAAE,CAGtE;AAED,yFAAyF;AACzF,wBAAgB,uBAAuB,CAAC,cAAc,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,MAAM,CAOrF;AAED,oGAAoG;AACpG,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,CAAC,cAAc,EAAE,QAAQ,CAAC,EAAE,IAAI,EAAE,gBAAgB,EAAE,GAAG,MAAM,CAQrG;AAED,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,IAAI,CAAC,gBAAgB,EAAE,aAAa,CAAC,EAC7C,SAAS,EAAE,mBAAmB,GAC7B,MAAM,CAOR;AAID,2EAA2E;AAC3E,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,aAAa,GAAG,MAAM,CAK7D;AAED,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,kEAAkE;IAClE,cAAc,EAAE,MAAM,EAAE,CAAC;CAC1B;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE;IACrC,IAAI,EAAE,IAAI,CAAC,cAAc,EAAE,aAAa,GAAG,QAAQ,CAAC,CAAC;IACrD,IAAI,EAAE,gBAAgB,EAAE,CAAC;IACzB,IAAI,EAAE,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAC/B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,GAAG,WAAW,CAsCd;AAED,sEAAsE;AACtE,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAiBxE"}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Reel Remake — preview (Stage 3). Pure planning + prompt building for the
|
|
4
|
+
* billed preview: one portrait per text-only character (the identity anchor
|
|
5
|
+
* every shot reuses) and one swapped keyframe per shot that has a mapped
|
|
6
|
+
* person. The worker runs the image calls; Functions charges from the plan.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.planPreview = planPreview;
|
|
10
|
+
exports.characterRefs = characterRefs;
|
|
11
|
+
exports.reelPreviewImageCredits = reelPreviewImageCredits;
|
|
12
|
+
exports.swapInputCount = swapInputCount;
|
|
13
|
+
exports.buildAnchorPrompt = buildAnchorPrompt;
|
|
14
|
+
exports.horizontalPosition = horizontalPosition;
|
|
15
|
+
exports.buildSwapRequest = buildSwapRequest;
|
|
16
|
+
exports.previewAspectRatio = previewAspectRatio;
|
|
17
|
+
const aiModels_1 = require("../../globals/aiModels");
|
|
18
|
+
const creditUnit_1 = require("../../globals/creditUnit");
|
|
19
|
+
const reelRemake_1 = require("../../globals/reelRemake");
|
|
20
|
+
const aiGenFactory_service_1 = require("../aiGen/aiGenFactory.service");
|
|
21
|
+
/** Seedream takes 14 images; the keyframe is one of them. */
|
|
22
|
+
const MAX_SWAP_REFS = 13;
|
|
23
|
+
function planPreview(remake) {
|
|
24
|
+
const mapped = new Map(remake.cast.filter((p) => p.character).map((p) => [p.id, p]));
|
|
25
|
+
const shotIds = remake.shots
|
|
26
|
+
.filter((s) => s.people.some((p) => mapped.has(p.personId)))
|
|
27
|
+
.map((s) => s.id);
|
|
28
|
+
const inPlannedShot = new Set(remake.shots.filter((s) => shotIds.includes(s.id)).flatMap((s) => s.people.map((p) => p.personId)));
|
|
29
|
+
const anchorPersonIds = [...mapped.values()]
|
|
30
|
+
.filter((p) => p.character.refImageUrls.length === 0 && inPlannedShot.has(p.id))
|
|
31
|
+
.map((p) => p.id);
|
|
32
|
+
return { anchorPersonIds, shotIds };
|
|
33
|
+
}
|
|
34
|
+
/** Images that define how a character looks: their own refs, else the generated portrait. */
|
|
35
|
+
function characterRefs(character) {
|
|
36
|
+
if (character.refImageUrls.length)
|
|
37
|
+
return character.refImageUrls;
|
|
38
|
+
return character.anchor?.url ? [character.anchor.url] : [];
|
|
39
|
+
}
|
|
40
|
+
/** Credits one preview image costs on the user's plan (same floor rule as /v1/image). */
|
|
41
|
+
function reelPreviewImageCredits(numInputImages, paid) {
|
|
42
|
+
const modelKey = reelRemake_1.REEL_REMAKE.previewImageModel;
|
|
43
|
+
return (0, aiGenFactory_service_1.getAiGenProviderService)(modelKey).getCreditUsed({
|
|
44
|
+
modelKey,
|
|
45
|
+
numInputImages,
|
|
46
|
+
...(paid ? { creditFloor: creditUnit_1.MIN_CREDITS_PER_IMAGE_OP } : {}),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/** Number of input images a shot's swap sends (keyframe + character refs). Anchors count as one. */
|
|
50
|
+
function swapInputCount(shot, cast) {
|
|
51
|
+
const byId = new Map(cast.map((p) => [p.id, p]));
|
|
52
|
+
const refs = shot.people.reduce((n, sp) => {
|
|
53
|
+
const c = byId.get(sp.personId)?.character;
|
|
54
|
+
if (!c)
|
|
55
|
+
return n;
|
|
56
|
+
return n + (c.refImageUrls.length || 1);
|
|
57
|
+
}, 0);
|
|
58
|
+
return 1 + Math.min(refs, MAX_SWAP_REFS);
|
|
59
|
+
}
|
|
60
|
+
function buildAnchorPrompt(person, character) {
|
|
61
|
+
return [
|
|
62
|
+
`Full-body photograph of ${character.name}${character.prompt ? `: ${character.prompt}` : ""}.`,
|
|
63
|
+
`Same build, age range and gender presentation as this person: ${person.description}.`,
|
|
64
|
+
"Standing, facing the camera, face clearly visible and well lit, plain light-grey studio background.",
|
|
65
|
+
"Photorealistic, natural skin texture, sharp focus.",
|
|
66
|
+
].join(" ");
|
|
67
|
+
}
|
|
68
|
+
const PROMINENCE_ORDER = { primary: 0, secondary: 1, background: 2 };
|
|
69
|
+
/** "on the left" / "in the centre" / "on the right" from a 0..1000 box. */
|
|
70
|
+
function horizontalPosition(box) {
|
|
71
|
+
const cx = (box.xmin + box.xmax) / 2;
|
|
72
|
+
if (cx < 380)
|
|
73
|
+
return "on the left";
|
|
74
|
+
if (cx > 620)
|
|
75
|
+
return "on the right";
|
|
76
|
+
return "in the centre";
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Edit instruction for one shot: every mapped person in it becomes their
|
|
80
|
+
* character in a single edit, everyone else and the scene stay. Throws if a
|
|
81
|
+
* mapped person has no usable reference (their portrait failed) — the caller
|
|
82
|
+
* fails that shot.
|
|
83
|
+
*/
|
|
84
|
+
function buildSwapRequest(args) {
|
|
85
|
+
const byId = new Map(args.cast.map((p) => [p.id, p]));
|
|
86
|
+
const inputImageUrls = [args.shot.keyframeUrl];
|
|
87
|
+
const lines = [];
|
|
88
|
+
const people = [...args.shot.people].sort((a, b) => PROMINENCE_ORDER[a.prominence] - PROMINENCE_ORDER[b.prominence]);
|
|
89
|
+
for (const sp of people) {
|
|
90
|
+
const person = byId.get(sp.personId);
|
|
91
|
+
const character = person?.character;
|
|
92
|
+
if (!person || !character)
|
|
93
|
+
continue;
|
|
94
|
+
const refs = characterRefs(character);
|
|
95
|
+
if (!refs.length)
|
|
96
|
+
throw new Error(`No reference image for ${character.name}`);
|
|
97
|
+
const room = MAX_SWAP_REFS + 1 - inputImageUrls.length;
|
|
98
|
+
const used = refs.slice(0, Math.max(1, room));
|
|
99
|
+
const first = inputImageUrls.length + 1;
|
|
100
|
+
inputImageUrls.push(...used);
|
|
101
|
+
const imageRef = used.length === 1 ? `Image ${first}` : `Images ${first}-${first + used.length - 1}`;
|
|
102
|
+
lines.push(`- The ${person.description} ${horizontalPosition(sp.box)} becomes ${character.name}` +
|
|
103
|
+
`${character.prompt ? ` (${character.prompt})` : ""}, looking exactly like the person in ${imageRef}.`);
|
|
104
|
+
}
|
|
105
|
+
if (!lines.length)
|
|
106
|
+
throw new Error("No mapped person in this shot");
|
|
107
|
+
const prompt = [
|
|
108
|
+
"Image 1 is a frame from a video. Edit Image 1:",
|
|
109
|
+
...lines,
|
|
110
|
+
"Keep exactly the same pose, body position, head angle, expression, hand positions, framing and camera angle.",
|
|
111
|
+
args.mode === "new-world"
|
|
112
|
+
? `Change the surroundings into a setting that fits "${args.theme ?? "the characters"}", with matching lighting; keep the composition.`
|
|
113
|
+
: "Everyone else, the background, the lighting and the colour grade stay unchanged.",
|
|
114
|
+
"Keep each character's face clearly visible — no helmet, mask or visor over it unless their description asks for one.",
|
|
115
|
+
"Photorealistic, same look as a video still.",
|
|
116
|
+
...(args.note ? [`Also: ${args.note}`] : []),
|
|
117
|
+
].join("\n");
|
|
118
|
+
return { prompt, inputImageUrls };
|
|
119
|
+
}
|
|
120
|
+
/** Closest aspect ratio the preview model accepts for a w×h frame. */
|
|
121
|
+
function previewAspectRatio(width, height) {
|
|
122
|
+
const allowed = (aiModels_1.aiModelConfigs[reelRemake_1.REEL_REMAKE.previewImageModel].fields.aspectRatio?.allowedValues ?? [
|
|
123
|
+
"9:16",
|
|
124
|
+
]);
|
|
125
|
+
const target = Math.log(width / height);
|
|
126
|
+
let best = allowed[0];
|
|
127
|
+
let bestDiff = Infinity;
|
|
128
|
+
for (const a of allowed) {
|
|
129
|
+
const [w, h] = a.split(":").map(Number);
|
|
130
|
+
if (!w || !h)
|
|
131
|
+
continue;
|
|
132
|
+
const diff = Math.abs(Math.log(w / h) - target);
|
|
133
|
+
if (diff < bestDiff) {
|
|
134
|
+
best = a;
|
|
135
|
+
bestDiff = diff;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return best;
|
|
139
|
+
}
|