Skip to content

Make videos on the computer you already own. FreeVideo runs MiniMax H3 in as little as 8 GB of VRAM and 16 GB of RAM, and adapts its acceleration path to your hardware.

Character library: cards without a front picture keep all pictures, same-name characters get...

Bowen Xuee4169cd4063e
diff --git a/web/cast_library.css b/web/cast_library.css
index b89c300..02bc242 100644
--- a/web/cast_library.css
+++ b/web/cast_library.css
@@ -1,3 +1,4 @@
+.fv-cast-library[hidden]{display:none}
.fv-cast-library{display:flex;align-items:center;justify-content:flex-end;gap:10px;min-width:0;padding:2px 0 10px;color:var(--fv-ink);font:13px/1.5 var(--fv-font)}
.fv-cast-heading{flex-shrink:0;margin-right:auto;font-weight:var(--fv-medium,500)}
.fv-cast-avatars{display:flex;align-items:center;gap:7px;min-width:0;overflow:auto;padding:3px}
diff --git a/web/cast_library.js b/web/cast_library.js
index d18537a..5ce3fb1 100644
--- a/web/cast_library.js
+++ b/web/cast_library.js
@@ -1,4 +1,5 @@
import {agentGet, agentPost, agentSend} from './agent_session.js';
+import {agentInterfaceEnabled} from './agent_connect.js';
// The optional generator owns its UI and job lifecycle; keep its contract here.
async function mountGenerator(options) {
@@ -28,6 +29,9 @@ export function createCastLibrary({api, text, viewURL, uploadOne, onChange = ()
let rows = [], mentioned = new Set(), disposed = false, request = 0, current = null, broadcasting = false;
const subscribers = new Set();
const element = el('div', 'fv-cast-library');
+ // The row shows, and the page session is asked for, only once /freevideo/agent says the interface is on.
+ let available = false; element.hidden = true;
+ const probe = agentInterfaceEnabled(api).then(on => { available = on && !disposed; element.hidden = !available; return available; });
const heading = el('span', 'fv-cast-heading', text('Characters', '角色'));
const avatars = el('div', 'fv-cast-avatars');
const add = button('fv-cast-add', '+', () => openDialog());
@@ -61,7 +65,7 @@ export function createCastLibrary({api, text, viewURL, uploadOne, onChange = ()
}
}
async function refresh() {
- if (disposed) return rows;
+ if (disposed || !await probe) return rows;
const serial = ++request;
const data = await value(await agentGet(api, '/freevideo/agent/cast?kind=character&limit=200'));
if (!disposed && serial === request) {
@@ -81,10 +85,10 @@ export function createCastLibrary({api, text, viewURL, uploadOne, onChange = ()
}
function openDialog(id = null) {
- if (disposed) return;
+ if (disposed || !available) return;
current?.close();
let closed = false, loading = !!id, saving = false, revision, active = 'upload';
- let generator = null, aiState = {busy: false, done: false}, deleteTimer = null, confirming = false;
+ let generator = null, aiState = {busy: false, done: false}, deleteTimer = null, confirming = false, frontless = false;
const slots = Array(6).fill(null), slotUI = [];
const dialog = el('dialog', 'fv-cast-dialog');
const top = el('div', 'fv-cast-top');
@@ -123,7 +127,7 @@ export function createCastLibrary({api, text, viewURL, uploadOne, onChange = ()
name.disabled = busy; remove.disabled = busy;
uploadTab.disabled = aiTab.disabled = busy;
save.disabled = busy || aiBusy() || !name.value.trim()
- || (active === 'upload' ? !slots[0] : !aiState.done);
+ || (active === 'upload' ? !(frontless ? slots.some(Boolean) : slots[0]) : !aiState.done);
for (let index = 0; index < slotUI.length; index++) {
slotUI[index].choose.disabled = busy; slotUI[index].remove.disabled = busy;
}
@@ -196,9 +200,10 @@ export function createCastLibrary({api, text, viewURL, uploadOne, onChange = ()
if (closed) return;
revision = card.revision; name.value = card.name;
for (let i = 0; i < 6; i++) release(i);
- const front = card.images[0]?.label === 'front';
- (front ? card.images : [null, ...card.images]).slice(0, 6)
- .forEach((picture, index) => { if (picture) slots[index] = {...picture}; });
+ // A card may have no front picture (the API does not require one): all its pictures stay, in order, and
+ // saving does not ask for a front; the front slot is only reserved while there is a front to keep first.
+ frontless = card.images.length > 0 && card.images[0].label !== 'front';
+ card.images.slice(0, 6).forEach((picture, index) => { slots[index] = {...picture}; });
slotUI.forEach((_, index) => paint(index));
generator?.cancel(); aiState = {busy: false, done: false}; select('upload'); resetDelete();
} finally { loading = false; if (!closed) sync(); }
@@ -220,7 +225,8 @@ export function createCastLibrary({api, text, viewURL, uploadOne, onChange = ()
...(typeof picture.file === 'string' ? {media: picture.file} : {file: picture.file}),
label: picture.label || (index ? 'other' : 'front'),
}));
- if (!pictures.length || pictures.length > 6 || !pictures.some(picture => picture.label === 'front'))
+ const frontNeeded = active === 'ai' || !frontless;
+ if (!pictures.length || pictures.length > 6 || (frontNeeded && !pictures.some(picture => picture.label === 'front')))
throw new Error(text('Choose a front image and up to five views', '请选择正面图片,最多五张视角图片'));
const images = [];
for (const picture of pictures) {
diff --git a/web/cast_mention.js b/web/cast_mention.js
index a402bb4..c3ba43d 100644
--- a/web/cast_mention.js
+++ b/web/cast_mention.js
@@ -48,7 +48,9 @@ export function castInRows(rows) {
const people = new Map();
(Array.isArray(rows) ? rows : []).forEach((row, index) => {
if (!enabled(row) || !row.cast?.id || row.role !== 'reference' || !IMAGE.test(row.file || '')) return;
- const person = people.get(row.cast.id) || {id: row.cast.id, name: row.cast.name || '', description: row.cast.description || '', rows: []};
+ const name = row.cast.name || '';
+ // `handle` is what the text says after the @: the name, or "name (2)" when another present character has the same name.
+ const person = people.get(row.cast.id) || {id: row.cast.id, name, handle: row.cast.handle || name, description: row.cast.description || '', rows: []};
person.rows.push(index); people.set(row.cast.id, person);
});
return [...people.values()];
@@ -56,15 +58,15 @@ export function castInRows(rows) {
// Ids of the characters present in the media list and written in the text, in list order.
export function mentionedIds(text, rows) {
- const people = castInRows(rows), names = new Set(mentionRanges(String(text ?? ''), people).map(range => range.name));
- return people.filter(person => names.has(person.name)).map(person => person.id);
+ const people = castInRows(rows), handles = new Set(mentionRanges(String(text ?? ''), people).map(range => range.name));
+ return people.filter(person => handles.has(person.handle)).map(person => person.id);
}
const escape = text => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// Where `@name` of a present character stands in the text; longer names win, an @ right after a word or an address is not
// a mention (the same rule as the @ list's).
export function mentionRanges(text, people) {
- const names = [...new Set(people.map(person => person.name).filter(Boolean))].sort((a, b) => b.length - a.length);
+ const names = [...new Set(people.map(person => person.handle ?? person.name).filter(Boolean))].sort((a, b) => b.length - a.length);
if (!names.length || !text.includes('@')) return [];
// A name written in Latin letters does not end in the middle of a longer word ("@Su Wanda" is not "@Su Wan").
const whole = name => escape(name) + (/[A-Za-z0-9_]$/.test(name) ? '(?![A-Za-z0-9_])' : '');
@@ -92,11 +94,11 @@ export function expandCast(text, rows, {active = true} = {}) {
let result = '', at = 0;
for (const range of ranges) {
result += text.slice(at, range.start); at = range.end;
- const person = people.find(p => p.name === range.name);
- if (!active || !person) { result += range.name; continue; }
+ const person = people.find(p => (p.handle ?? p.name) === range.name);
+ if (!active || !person) { result += person ? person.name : range.name; continue; }
let number = order.indexOf(person) + 1;
if (!number) {
- if (order.length >= MAX_PEOPLE) { result += range.name; continue; }
+ if (order.length >= MAX_PEOPLE) { result += person.name; continue; }
order.push(person); number = order.length;
}
result += `<Subject ${number}>`;
@@ -136,10 +138,24 @@ export function mentionState(rows, card, count = cardPictures(card).length) {
return {ok: true, present: false};
}
+// What the text says after the @ for this card: its name, unless another character in the list already goes by that
+// name (the library allows two cards with one name); then "name (2)", "name (3)" … so each @ points at one card.
+// `pending` maps ids of characters chosen a moment ago, whose rows have not landed yet, to the handle already typed.
+export function mentionHandle(rows, card, pending = new Map()) {
+ const people = castInRows(rows), present = people.find(person => person.id === card.id);
+ if (present) return present.handle;
+ if (pending.has(card.id)) return pending.get(card.id);
+ const taken = new Set([...people.map(person => person.handle), ...pending.values()]);
+ let handle = card.name, number = 1;
+ while (taken.has(handle)) handle = `${card.name} (${++number})`;
+ return handle;
+}
+
// The media list with the character's pictures appended together, so one range of picture numbers covers them.
-export function withCharacter(rows, card) {
+export function withCharacter(rows, card, typed = '') {
const list = Array.isArray(rows) ? rows.map(row => ({...row})) : [];
if (castInRows(list).some(person => person.id === card.id)) return list;
- const cast = {id: card.id, name: card.name, description: card.description || ''};
+ const handle = typed || mentionHandle(list, card);
+ const cast = {id: card.id, name: card.name, description: card.description || '', ...(handle === card.name ? {} : {handle})};
return [...list, ...cardPictures(card).map(file => ({file, role: 'reference', enabled: true, cast}))];
}
diff --git a/web/prompt_references.js b/web/prompt_references.js
index 7cbd307..0f27b5b 100644
--- a/web/prompt_references.js
+++ b/web/prompt_references.js
@@ -170,7 +170,7 @@ export function attachReferencePicker(input, {items, t = en => en, view, host, a
const item = matches[index], selection = range;
if (item.character) {
if (item.character.blocked || onCharacter(item.character) === false) return;
- close(); insert('@' + item.character.name + (/^\s/.test(input.value.slice(selection.end)) ? '' : ' '), selection.start, selection.end); close();
+ close(); insert('@' + (item.character.handle || item.character.name) + (/^\s/.test(input.value.slice(selection.end)) ? '' : ' '), selection.start, selection.end); close();
return;
}
close(); insert(item.token + (/^\s/.test(input.value.slice(selection.end)) ? '' : ' '), selection.start, selection.end); close();
diff --git a/web/studio.js b/web/studio.js
index 2c1a52d..b7d91ad 100644
--- a/web/studio.js
+++ b/web/studio.js
@@ -19,7 +19,7 @@ import { createAgentActivity } from './agent_activity.js';
import { createStudioQueue, randomSeed } from './studio_queue.js';
import { attachReferencePicker, referenceItems, syncReferencePrompt, referenceInputs, mentionAt, mediaRows } from './prompt_references.js';
import { attachTermSuggest } from './prompt_terms.js';
-import { castInRows, expandCast, mentionState, mentionedIds, withCharacter, MAX_PICTURES_EACH } from './cast_mention.js';
+import { castInRows, expandCast, mentionHandle, mentionState, mentionedIds, withCharacter, MAX_PICTURES_EACH } from './cast_mention.js';
import { attachMentionOverlay } from './cast_overlay.js';
import { openCastLibrary } from './cast_source.js';
import { reconcileLoraTriggers } from './lora_triggers.js';
@@ -514,13 +514,14 @@ export function openStudio(node, {extendFrom = null} = {}) {
const castBlocked = reason => ({frames: t('Not available with first and last frames', '首尾帧不能和角色同时使用'),
people: t('Up to 3 characters', '最多 3 个角色'), full: t('Up to 32 references', '参考素材最多 32 个'),
extend: t('Not available when continuing a video', '接着生成时不能使用角色')}[reason] || '');
+ const castPending = new Map();
function castChoices() {
if (!castLibrary || linked(node, 'text') || !castAssets()) return [];
const rows = mediaRows(node);
return castLibrary.list().filter(card => card.name).map(card => {
const state = extension.source ? {ok: false, reason: 'extend'}
: mentionState(rows, card, Math.min(MAX_PICTURES_EACH, Number(card.images) || 0));
- return {id: card.id, name: card.name, thumb: card.thumbnail?.file ? view(card.thumbnail.file) : card.thumbnail?.url || '',
+ return {id: card.id, name: card.name, handle: mentionHandle(rows, card, castPending), thumb: card.thumbnail?.file ? view(card.thumbnail.file) : card.thumbnail?.url || '',
blocked: state.ok ? '' : castBlocked(state.reason)};
});
}
@@ -529,14 +530,17 @@ export function openStudio(node, {extendFrom = null} = {}) {
if (!castLibrary || extension.source) return false;
if (castInRows(mediaRows(node)).some(entry => entry.id === person.id)) return true;
if (!mentionState(mediaRows(node), {id: person.id}, 1).ok) return false;
+ const typed = person.handle || person.name;
+ castPending.set(person.id, typed);
castLibrary.get(person.id).then(card => {
if (disposed) return;
const rows = mediaRows(node), target = castAssets();
if (!target || !mentionState(rows, card).ok) return;
- target.field.value = JSON.stringify(withCharacter(rows, card));
+ target.field.value = JSON.stringify(withCharacter(rows, card, typed));
target.owner.graph?.change?.();
window.dispatchEvent(new CustomEvent('freevideo-media', {detail: target.owner.id}));
- }).catch(() => { if (!disposed) { status.dataset.error = 'true'; status.textContent = t('Could not add the character\'s pictures', '无法加入角色的图片'); } });
+ }).catch(() => { if (!disposed) { status.dataset.error = 'true'; status.textContent = t('Could not add the character\'s pictures', '无法加入角色的图片'); } })
+ .finally(() => castPending.delete(person.id));
return true;
}
const references = attachReferencePicker(prompt, {items: () => referenceItems(node), t, view, host: dialog,