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.
Add white-model guidance: Agent scene submit, page render and upload, ready-made scenes
Bowen Xue5e2cc3dfb2dd
diff --git a/freevideo_engine/agent/cli.py b/freevideo_engine/agent/cli.pyindex 3d91a67..0625986 100644--- a/freevideo_engine/agent/cli.py+++ b/freevideo_engine/agent/cli.py@@ -121,6 +121,15 @@ def build_parser():if name == 'operations':sub.add_argument('--operations-file', help='JSON array of operations (- = stdin)')schema.cli_arguments(sub, schema.image_schema(name))+ command = commands.add_parser('whitebox', help='Render a rough 3D white-model reference video in the user\'s open page')+ whitebox = command.add_subparsers(dest='operation', required=True)+ for name, (method, path, summary) in schema.WHITEBOX_OPERATIONS.items():+ sub = whitebox.add_parser(name, help=summary)+ if '{id}' in path:+ sub.add_argument('id')+ if name == 'submit':+ sub.add_argument('file', nargs='?', help='JSON {code, summary?} request (- = stdin); options override it')+ schema.cli_arguments(sub, schema.whitebox_schema(name))command = commands.add_parser('sessions', help='Share messages and plans with the user in a work conversation')sessions = command.add_subparsers(dest='operation', required=True)for name in ('list', 'create', 'events', 'post'):@@ -236,9 +245,10 @@ def main(argv=None):schema.validate(schema.CAST, values['id'])path = path.replace('{id}', values['id'])result = client.get(path, **value) if method == 'GET' else client.send(method, path, value)- elif args.command in ('projects', 'images'):- table, shape_of, ident = ((schema.PROJECT_OPERATIONS, schema.project_schema, schema.PROJECT)- if args.command == 'projects' else (schema.IMAGE_OPERATIONS, schema.image_schema, schema.IMAGE))+ elif args.command in ('projects', 'images', 'whitebox'):+ table, shape_of, ident = {'projects': (schema.PROJECT_OPERATIONS, schema.project_schema, schema.PROJECT),+ 'images': (schema.IMAGE_OPERATIONS, schema.image_schema, schema.IMAGE),+ 'whitebox': (schema.WHITEBOX_OPERATIONS, schema.whitebox_schema, schema.WHITEBOX)}[args.command]method, path, _ = table[args.operation]shape = shape_of(args.operation)value = read(args.file) if getattr(args, 'file', None) else {}diff --git a/freevideo_engine/agent/errors.py b/freevideo_engine/agent/errors.pyindex 79a676c..1653aa5 100644--- a/freevideo_engine/agent/errors.py+++ b/freevideo_engine/agent/errors.py@@ -33,6 +33,7 @@ ERRORS = {'not_found': (404, False, 'The requested job, film or asset does not exist.', 'List /freevideo/agent/v1/jobs, /drama or /library and select an existing ID.'),'no_video': (409, False, 'The source job has no finished video.', 'GET /freevideo/agent/v1/jobs/{id} and wait for a succeeded job containing result.video.'),'not_ready': (503, True, 'The FreeVideo installation is not ready.', 'Open Settings → Install / repair and finish setup. If the installation token is unsafe or incomplete, restore a regular non-symlink agent/token owned by your account, or remove that token and restart FreeVideo.'),+ 'page_required': (409, True, 'This needs the FreeVideo page open in a browser.', 'Ask the user to open FreeVideo in their browser, then retry.'),'busy': (409, True, 'The requested resource is currently in use.', 'Wait retry_after_seconds, read GET /freevideo/agent/v1/queue, then retry the request.'),'conflict': (409, False, 'The requested operation conflicts with the current state.', 'Read the current job with GET /freevideo/agent/v1/jobs/{id} before retrying.'),'dependency_failed': (409, False, 'A required job did not succeed.', 'Read the dependency jobs, resume failed generation, then submit the dependent job with a new ref.'),diff --git a/freevideo_engine/agent/guide.py b/freevideo_engine/agent/guide.pyindex 07b4cec..8789fac 100644--- a/freevideo_engine/agent/guide.py+++ b/freevideo_engine/agent/guide.py@@ -4,7 +4,9 @@ Summarised from MiniMax's h3-prompt-writing skill (linked below) and FreeVideo'sown prompt rules; the enhancement job applies the same contract."""from ..default_prompt import DEFAULT_PROMPT-from .schema import CAST_OPERATIONS, DRAMA_OPERATIONS, IMAGE_OPERATIONS, PROJECT_OPERATIONS, SESSION_OPERATIONS+from .schema import CAST_OPERATIONS, DRAMA_OPERATIONS, IMAGE_OPERATIONS, PROJECT_OPERATIONS, SESSION_OPERATIONS, WHITEBOX_OPERATIONS++from .whitebox import SCENE_PROMPTSOURCE = 'https://github.com/MiniMax-AI/MiniMax-H3/tree/main/skills/h3-prompt-writing'@@ -133,6 +135,17 @@ GUIDE = {'A new paint layer is empty, so heal and clone there have nothing to work from; paint and gradients go on a paint layer above a locked picture.','On 409 image_changed, recompute from error.image and its current_revision. POST /images/{id}/render makes a PNG input file from the revision you read; use the job result file as a picture source.',],+ 'whitebox': [+ 'A white model is a rough 3D stand-in for the shot (boxes, camera, motion) rendered in the user\'s FreeVideo page. Used as a reference video that guides only the first steps, the final video keeps its layout and motion.',+ 'Use it only when the user asked for it: the user message carries whitebox=true, or the user says so. Never choose it yourself, and never suggest it as a default.',+ 'Write the scene with the prompt in whitebox_scene_prompt (replace {SHOT} with the user\'s one-sentence shot). POST /whitebox with the code and a one-sentence summary; it needs the page open and answers page_required otherwise: tell the user to open FreeVideo.',+ 'Read GET /whitebox/{id} every few seconds. On failed, fix the code from error and send it again, at most 3 retries; if they all fail, tell the user and stop, with no silent fallback.',+ 'On ready, ask the user to approve it first: write a confirm event with whitebox={work, seconds, summary}. After approval, generate with media.references=[{"library": work, "early_steps": K}] where K is about 3/8 of the first-pass steps rounded (8 steps -> 3, 20 steps -> 8; early_ratio in the ready answer), so the white model keeps the same share of the schedule on every quality level. The prompt describes the real look of the final video, not white boxes. Use no other kind of reference with it unless the user gave one.',+ 'When the user wants a change, write a new scene from the edited description and repeat; the earlier white model stays in the library.',+ ],+ 'whitebox_scene_prompt': SCENE_PROMPT,+ 'whitebox_routes': [dict(operation=name, method=method, path=path, description=summary)+ for name, (method, path, summary) in WHITEBOX_OPERATIONS.items()],'image_routes': [dict(operation=name, method=method, path=path, description=summary)for name, (method, path, summary) in IMAGE_OPERATIONS.items()],'sessions': [@@ -272,6 +285,8 @@ CAPABILITIES_ZH = {('GET', '/library/{day}/{run}/file'): '下载已保存的视频',('GET', '/library/{day}/{run}/thumbnail'): '查看已保存的视频缩略图',('GET', '/queue'): '查看队列顺序及任务属于 Agent 还是用户',+ ('POST', '/whitebox'): '发送你写好的白模场景代码,由用户打开的 FreeVideo 页面渲染成短视频并存入作品库;页面没打开返回 page_required',+ ('GET', '/whitebox/{id}'): '查看白模场景进度:waiting、rendering、ready(work 是作品库编号,放进 media.references 并加 early_steps)或 failed(error)',('GET', '/editor/sources/content'): '下载剪辑项目的源素材或缩略图;使用 GET /projects/{id} 返回的素材标识',('GET', '/inbox'): '等待当前工作会话中用户的消息或答复,最多 50 秒;again=true 表示暂时没有,再调一次',('POST', '/drama/plan'): '预览多镜头影片的剧本和各镜头拍摄条数,不保存、不提交。'diff --git a/freevideo_engine/agent/jobs.py b/freevideo_engine/agent/jobs.pyindex b56e89b..98be19e 100644--- a/freevideo_engine/agent/jobs.py+++ b/freevideo_engine/agent/jobs.py@@ -493,6 +493,8 @@ class Jobs:self.projects = Projects(port.output_root)from .images import Imagesself.images = Images(self, self.directory.parent / 'images')+ from .whitebox import Whitebox+ self.whitebox = Whitebox(self.directory.parent / 'whitebox', port.output_root)self.load()from .productions import Productionsself.productions = Productions(self)@@ -1515,8 +1517,10 @@ class Jobs:assets.append(dict(file=self.media_file(row, spec['media'][role], role, target=target),role=role))for i, item in enumerate(spec['media'].get('references', []), 1):- assets.append(dict(file=self.media_file(row, item, 'reference-%02d' % i, target=target),- role='reference'))+ asset = dict(file=self.media_file(row, item, 'reference-%02d' % i, target=target), role='reference')+ if 'early_steps' in item:+ asset['early_steps'] = item['early_steps']+ assets.append(asset)return assetsassets = resolve()graph = specs.comfy_prompt(spec, node, prompt=prompt, assets=assets)diff --git a/freevideo_engine/agent/mcp.py b/freevideo_engine/agent/mcp.pyindex 321e363..62ce0d2 100644--- a/freevideo_engine/agent/mcp.py+++ b/freevideo_engine/agent/mcp.py@@ -71,6 +71,10 @@ def tool_list():rows.extend(dict(name=PROJECT_TOOLS[name], title='Editing project: ' + name, description=summary,annotations=read if method == 'GET' else write)for name, (method, _, summary) in PROJECT_OPERATIONS.items())+ from .schema import WHITEBOX_OPERATIONS, WHITEBOX_TOOLS+ rows.extend(dict(name=WHITEBOX_TOOLS[name], title='White model: ' + name, description=summary,+ annotations=read if method == 'GET' else write)+ for name, (method, _, summary) in WHITEBOX_OPERATIONS.items())from .schema import IMAGE_OPERATIONS, IMAGE_TOOLSrows.extend(dict(name=IMAGE_TOOLS[name], title='Image: ' + name, description=summary,annotations=read if method == 'GET' else write)@@ -316,7 +320,7 @@ class Server:images.append(dict(type='image', **value['previous']))value['previous'] = Truereturn value, images- from .schema import IMAGE_TOOLS, PROJECT_TOOLS, TOOL_ENDPOINTS+ from .schema import IMAGE_TOOLS, PROJECT_TOOLS, TOOL_ENDPOINTS, WHITEBOX_TOOLSfrom .schema import SESSION_OPERATIONS, SESSION_TOOLSif name in SESSION_TOOLS.values():operation = next(op for op, tool in SESSION_TOOLS.items() if tool == name)@@ -326,7 +330,7 @@ class Server:if method == 'GET' and wait is not None:args['wait'] = waitreturn (c.get(path, **args) if method == 'GET' else c.post(path, args)), []- if name in PROJECT_TOOLS.values() or name in IMAGE_TOOLS.values():+ if name in PROJECT_TOOLS.values() or name in IMAGE_TOOLS.values() or name in WHITEBOX_TOOLS.values():method, path = TOOL_ENDPOINTS[name]if '{id}' in path:path = path.replace('{id}', args.pop('id'))diff --git a/freevideo_engine/agent/media.py b/freevideo_engine/agent/media.pyindex c2038ac..a274a13 100644--- a/freevideo_engine/agent/media.py+++ b/freevideo_engine/agent/media.py@@ -676,6 +676,7 @@ def _stream_seconds(stream, container):def _video_stream(container):import av+ import av.streamattached = getattr(getattr(av.stream, 'Disposition', None), 'attached_pic', 0)for stream in container.streams.video:if not (attached and stream.disposition & attached):diff --git a/freevideo_engine/agent/routes.py b/freevideo_engine/agent/routes.pyindex 69534e6..a7b05b4 100644--- a/freevideo_engine/agent/routes.py+++ b/freevideo_engine/agent/routes.py@@ -30,6 +30,8 @@ from . import schema as contract, errors, accessfrom .jobs import ACTIVE, DONE, JobError, Jobs, chain_error, library_viewPREFIX = '/freevideo/agent/v1'+from .whitebox import MAX_VIDEO_BYTES as MAX_WHITEBOX_VIDEO+from .whitebox_presets import PRESETS, preset as whitebox_preset# What an empty wait tells the agent: the conversation is open for as long as the user keeps the page.KEEP_WAITING = ('Nothing new yet. The user is still in FreeVideo and may speak at any moment: call this wait ''again now. An empty wait never ends the conversation; do not end your turn or write a summary.')@@ -171,6 +173,13 @@ def features(continuation, vlm):**{'projects.' + name: rules for name in ('insert', 'split', 'move', 'trim', 'remove', 'export', 'generate', 'listen')}}+def whitebox_support(jobs):+ """White-model guidance needs the user's open page to render, and the Mac engine cannot use it yet."""+ if quality_tiers.on_mac():+ return dict(available=False, needs_page=True, note='Mac 暂不支持白模。')+ return dict(available=True, needs_page=True, page_open=jobs.whitebox.page_open(), note='白模预览在 FreeVideo 页面中显示,需保持页面打开。')++def capabilities(port, jobs):from ..lora_triggers import list_triggersfrom .. import __version__@@ -200,6 +209,7 @@ def capabilities(port, jobs):api_version=API_VERSION, freevideo_version=release or __version__, build=__version__, ready=ready, problem=problem,continuation=continuation,features=features(continuation, vlm),+ whitebox=whitebox_support(jobs),device=dict(backend=machine.get('device_backend', 'cuda') if machine else None,vram_limit_gib=machine.get('vram_gib'), ram_limit_gib=machine.get('ram_gib')),kinds={'generate': 'Video from a prompt and optional keyframes or references',@@ -960,7 +970,10 @@ def register():if store.stopped(sid):# Speaking again continues a conversation the user stopped earlier.store.append(sid, dict(type='control', action='resumed'), by='user')- row = store.append(sid, dict(type='message', text=value['text'], mid=value['id']), by='user')+ message = dict(type='message', text=value['text'], mid=value['id'])+ if value.get('whitebox'):+ message['whitebox'] = True+ row = store.append(sid, message, by='user')fold['sent'][value['id']] = row['seq']fold['ids'][row['seq']] = value['id']while len(fold['sent']) > 500:@@ -1900,6 +1913,97 @@ def register():for operation, (method, suffix, _) in contract.IMAGE_OPERATIONS.items():getattr(routes, method.lower())(PREFIX + suffix)(image_handler(operation))+ def whitebox_handler(operation):+ async def handler(request):+ scenes = jobs.whitebox+ if operation == 'get':+ deadline = time.monotonic() + request[query_key].get('wait', 0)+ while True:+ view = await asyncio.to_thread(scenes.read, request.match_info['id'])+ if view['status'] in ('ready', 'failed') or time.monotonic() >= deadline:+ return reply(view)+ await asyncio.sleep(.5)+ value = await body(request)+ if not scenes.page_open():+ raise JobError('page_required', 'No FreeVideo page is open to render the scene')+ return reply(await asyncio.to_thread(scenes.create, value['code'], value.get('summary'),+ request.get('agent_session')), 201)+ return guarded(handler)++ for operation, (method, suffix, _) in contract.WHITEBOX_OPERATIONS.items():+ getattr(routes, method.lower())(PREFIX + suffix)(whitebox_handler(operation))++ # The page renders scenes: it asks for work, then sends back the video or the reason it failed.+ def whitebox_confirm(notice):+ store = work_sessions()+ if not notice or store is None:+ return+ try:+ store.append(notice['session'], dict(type='confirm', id='cf' + secrets.token_hex(5), state='asked',+ question=notice['question'], whitebox=dict(work=notice['work'], seconds=notice['seconds'],+ summary=notice['summary'])), by='builtin')+ except Exception:+ logging.getLogger(__name__).warning('White-model confirmation failed', exc_info=True)++ @routes.get('/freevideo/agent/whitebox/next')+ @guarded+ async def whitebox_next(request):+ return reply(dict(scene=await asyncio.to_thread(jobs.whitebox.claim)))++ @routes.get('/freevideo/agent/whitebox/presets')+ @guarded+ async def whitebox_presets(request):+ return reply(dict(presets=[{k: row[k] for k in ('id', 'name', 'shot', 'prompt')} for row in PRESETS]))++ @routes.post('/freevideo/agent/whitebox/try')+ @guarded+ async def whitebox_try(request):+ if request.content_type != 'application/json':+ return problem('unsupported_media_type', 'Send JSON', 415)+ value = await body(request)+ row = whitebox_preset(value.get('preset') if isinstance(value, dict) else None)+ if row is None:+ raise JobError('not_found', 'No such ready-made scene')+ sid = value.get('session')+ notice = None+ if sid is not None:+ store = work_sessions()+ if store is None or not isinstance(sid, str) or not store.has(chat['client'], sid):+ raise JobError('not_found', 'No such conversation')+ # The conversation gets the confirmation card once the page has rendered the scene.+ notice = dict(question=row['prompt'], summary=row['name'])+ scene = await asyncio.to_thread(jobs.whitebox.create, row['code'], row['shot'], sid, notice)+ return reply(dict(scene, preset=row['id'], name=row['name'], prompt=row['prompt']), 201)++ @routes.post('/freevideo/agent/whitebox/{id}/video')+ @guarded+ async def whitebox_video(request):+ scenes = jobs.whitebox+ with tempfile.TemporaryDirectory(prefix='.whitebox-', dir=port.input_root) as temporary:+ target = Path(temporary) / 'scene.mp4'+ size = 0+ with target.open('xb') as output:+ async for chunk in request.content.iter_chunked(256 * 1024):+ size += len(chunk)+ if size > MAX_WHITEBOX_VIDEO:+ raise JobError('too_large', 'Upload exceeds limit', limit_bytes=MAX_WHITEBOX_VIDEO)+ output.write(chunk)+ if not size:+ raise JobError('invalid_media', 'Supply the rendered video')+ async with upload_decoders:+ scene = await asyncio.to_thread(scenes.finish, request.match_info['id'], target)+ await asyncio.to_thread(whitebox_confirm, scenes.take_notice(scene['id']))+ return reply(scene)++ @routes.post('/freevideo/agent/whitebox/{id}/failed')+ @guarded+ async def whitebox_failed(request):+ if request.content_type != 'application/json':+ return problem('unsupported_media_type', 'Send JSON', 415)+ value = await body(request)+ message = value.get('message') if isinstance(value, dict) else None+ return reply(await asyncio.to_thread(jobs.whitebox.fail, request.match_info['id'], message))+@routes.get(PREFIX + '/editor/sources/content')@guardedasync def editor_source_content(request):diff --git a/freevideo_engine/agent/schema.py b/freevideo_engine/agent/schema.pyindex e19542c..d72982c 100644--- a/freevideo_engine/agent/schema.py+++ b/freevideo_engine/agent/schema.py@@ -107,11 +107,15 @@ def edit_document():'x-editor-document': True}-def media(image=False, *, local=False, clip=False, video=False):+def media(image=False, *, local=False, clip=False, video=False, early=False):properties = dict(file=FILE, from_job=JOB, library=SAVED, frame=FRAME,edit={'oneOf': [edit_recipe(), edit_document()]}, label=string(80))if local:properties['path'] = string(4096)+ if early:+ properties['early_steps'] = number(1, 32, integer=True, description=+ 'A reference video only: it guides the first pass for this many steps and is dropped afterwards, '+ 'including from the second pass. Omitted: the video guides every step.')if clip:properties.pop('frame')properties.pop('edit')@@ -139,7 +143,7 @@ def common():def generation_media(*, local=False, enhance=False):return obj({'first': nullable(media(True, local=local)), 'last': nullable(media(True, local=local)),- 'references': nullable(array(media(enhance, local=local), high=8 if enhance else 32))},+ 'references': nullable(array(media(enhance, local=local, early=not enhance), high=8 if enhance else 32))},allOf=[{'if': {'required': ['references'], 'properties': {'references': {'type': 'array', 'minItems': 1}}},'then': {'properties': {'first': {'type': 'null'}, 'last': {'type': 'null'}}}}])@@ -391,6 +395,13 @@ def contracts(*, local=False):else:extra.update(body=shape, idempotent=True, **({'status': 201} if name in ('create', 'generate', 'export', 'listen') else {}))routes[method, suffix] = route(summary, **extra)+ for name, (method, suffix, summary) in WHITEBOX_OPERATIONS.items():+ extra = dict(path=obj({'id': WHITEBOX}, ('id',))) if '{id}' in suffix else {}+ if method == 'GET':+ extra['query'] = whitebox_schema(name)+ else:+ extra.update(body=whitebox_schema(name), status=201)+ routes[method, suffix] = route(summary, **extra)for name, (method, suffix, summary) in IMAGE_OPERATIONS.items():shape = image_schema(name)extra = dict(path=obj({'id': IMAGE}, ('id',))) if '{id}' in suffix else {}@@ -408,12 +419,22 @@ def contracts(*, local=False):('/connect/reset', 'POST', EMPTY), ('/chat', 'GET', None), ('/chat/now', 'GET', None),('/chat/video', 'GET', None), ('/chat/list', 'GET', None), ('/chat/new', 'POST', EMPTY),('/chat/delete', 'POST', obj({'session': SESSION}, ('session',))),- ('/chat', 'POST', obj({'text': string(8000), 'id': CHAT_ID, 'session': SESSION}, ('text', 'id'))),+ ('/chat', 'POST', obj({'text': string(8000), 'id': CHAT_ID, 'session': SESSION, 'whitebox': {'type': 'boolean'}}, ('text', 'id'))),('/chat/confirm', 'POST', obj({'id': string(12, pattern=r'^cf[0-9a-f]{10}$'), 'approve': {'type': 'boolean'},'session': SESSION}, ('id', 'approve')))):routes[(method, '/freevideo/agent' + suffix)] = route('Studio Agent controls', body=body, auth=False, local=True)routes['GET', '/freevideo/agent/chat']['query'] = obj({'after': number(0, integer=True), 'session': SESSION})routes['GET', '/freevideo/agent/chat/video']['query'] = obj({'id': string(43, pattern=r'^[0-9]{4}-[0-9]{2}-[0-9]{2}/[0-9a-f]{32}$')}, ('id',))+ for method, suffix, summary, extra in (+ ('GET', '/next', 'The page asks for a white-model scene to render', {}),+ ('GET', '/presets', 'The ready-made white-model scenes the page can try', {}),+ ('POST', '/try', 'The page renders one ready-made white-model scene (no model writes code)',+ dict(body=obj(dict(preset=string(32, pattern=r'^[a-z0-9-]{1,32}$'), session=SESSION), ('preset',)))),+ ('POST', '/{id}/video', 'The page sends the rendered white-model video (the raw mp4 body)',+ dict(path=obj({'id': WHITEBOX}, ('id',)))),+ ('POST', '/{id}/failed', 'The page reports that a white-model scene could not be rendered',+ dict(path=obj({'id': WHITEBOX}, ('id',)), body=obj(dict(message=string(500)), ('message',))))):+ routes[method, '/freevideo/agent/whitebox' + suffix] = route(summary, auth=False, local=True, **extra)for suffix in ('', '/connect'):routes['GET', '/freevideo/agent' + suffix]['query'] = obj({'lang': enum(('zh', 'en'))})for name, (method, suffix, summary) in SESSION_OPERATIONS.items():@@ -539,6 +560,7 @@ def cast_view():# changed with the page's commands (web/image_commands.js). Shapes follow the page's# own fields (lineHeight, shape width), not the /media/edit recipe's.IMAGE = string(34, pattern=r'^i-[0-9a-f]{32}$')+WHITEBOX = string(12, pattern=r'^wb[0-9a-f]{10}$')LAYER_ID = string(128, pattern=r'^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$')COLOR = string(32, pattern=r'^(?:#[0-9A-Fa-f]{3,4}|#[0-9A-Fa-f]{6}|#[0-9A-Fa-f]{8}|transparent)$')IMAGE_SIDE = number(1, 8192, integer=True)@@ -643,6 +665,20 @@ IMAGE_TOOLS = dict(create='freevideo_image_create', get='freevideo_image_read',operations='freevideo_image_operations', render='freevideo_image_render')+WHITEBOX_OPERATIONS = {+ 'submit': ('POST', '/whitebox', 'Send the white-model scene code you wrote (build(THREE), see GET /guide). The user\'s open FreeVideo page renders it to a short video that joins the library.'),+ 'get': ('GET', '/whitebox/{id}', 'Read a white-model scene: waiting, rendering, ready (work = library ID for media.references with early_steps) or failed (error).'),+}+WHITEBOX_TOOLS = dict(submit='freevideo_whitebox_submit', get='freevideo_whitebox_read')+++def whitebox_schema(name):+ if name == 'submit':+ return obj(dict(code=string(65536, description='JavaScript that defines build(THREE) and returns {scene, camera, update(t)}.'),+ summary=string(500, description='One sentence on what the scene shows, for the confirm card.')), ('code',))+ return obj({'wait': WAIT})++def image_operation_fields():"""The image commands (IDC): op -> (fields, required, description)."""layer_id = LAYER_ID@@ -773,6 +809,7 @@ def session_event(*, stored=False, agent_only=False):1, 20000), total_seconds=number(0)), ('type', 'shots'))]if not agent_only:variants[0]['properties']['mid'] = CHAT_ID+ variants[0]['properties']['whitebox'] = {'type': 'boolean', 'description': 'The user chose a white-model preview for this request.'}from .errors import ERRORSvariants += [obj(dict(type={'const': 'step'}, id=string(12, pattern=r'^st[0-9a-f]{10}$'),tool=string(200), summary=text, state=enum(('started', 'done', 'failed')),@@ -782,6 +819,8 @@ def session_event(*, stored=False, agent_only=False):obj(dict(type={'const': 'confirm'}, id=string(12, pattern=r'^cf[0-9a-f]{10}$'),question=text, estimate_seconds=number(0), state=enum(('asked', 'approved', 'declined')),action=enum(('start', 'reshoot')), refs=obj(dict(drama=PRODUCTION, shot=SLUG)),+ whitebox=obj(dict(work=SAVED, seconds=number(0), summary=text), ('work',),+ description='The rendered white-model preview the user is asked to approve.'),digest=string(64, pattern=r'^[0-9a-f]{64}$')),('type', 'id', 'state'), **{'if': {'properties': {'state': {'const': 'asked'}}},'then': {'required': ['question']}}),@@ -1253,6 +1292,7 @@ def schemas():cast={name: cast_schema(name) for name in CAST_OPERATIONS}, cast_view=cast_view(),projects={name: project_schema(name) for name in PROJECT_OPERATIONS},images={name: image_schema(name) for name in IMAGE_OPERATIONS},+ whitebox={name: whitebox_schema(name) for name in WHITEBOX_OPERATIONS},sessions=dict(event=session_event(stored=True), meta=session_view(),**{name: session_schema(name) for name in SESSION_OPERATIONS}),confirmations=confirm_settings(), image_result=image_result())@@ -1377,6 +1417,8 @@ TOOL_ENDPOINTS.update({'freevideo_cast_' + name: (method, suffix)for name, (method, suffix, _) in CAST_OPERATIONS.items()})TOOL_ENDPOINTS.update({PROJECT_TOOLS[name]: (method, suffix)for name, (method, suffix, _) in PROJECT_OPERATIONS.items()})+TOOL_ENDPOINTS.update({WHITEBOX_TOOLS[name]: (method, suffix)+ for name, (method, suffix, _) in WHITEBOX_OPERATIONS.items()})TOOL_ENDPOINTS.update({IMAGE_TOOLS[name]: (method, suffix)for name, (method, suffix, _) in IMAGE_OPERATIONS.items()})TOOL_ENDPOINTS.update({SESSION_TOOLS[name]: SESSION_OPERATIONS[name][:2]diff --git a/freevideo_engine/agent/spec.py b/freevideo_engine/agent/spec.pyindex 75ee140..a148bd7 100644--- a/freevideo_engine/agent/spec.py+++ b/freevideo_engine/agent/spec.py@@ -68,13 +68,13 @@ def new_id():return 'j' + secrets.token_hex(6)-def media_item(value, path, *, image=False, frame_default=None):+def media_item(value, path, *, image=False, frame_default=None, early=False):if isinstance(value, str):value = {'file': value}value = dict(value) if isinstance(value, dict) else valueif isinstance(value, dict) and frame_default is not None and any(k in value for k in ('from_job', 'library')):value.setdefault('frame', frame_default)- errors = contract.issues(contract.media(image, local=True), value, path)+ errors = contract.issues(contract.media(image, local=True, early=early), value, path)if errors:raise SpecError(errors)return value@@ -152,7 +152,7 @@ def generation_media(value):return {}result = {role: media_item(value[role], '/media/' + role, image=True) for role in FRAMES if value.get(role) is not None}if value.get('references'):- result['references'] = [media_item(v, '/media/references/%d' % i) for i, v in enumerate(value['references'])]+ result['references'] = [media_item(v, '/media/references/%d' % i, early=True) for i, v in enumerate(value['references'])]return resultdiff --git a/freevideo_engine/agent/whitebox.py b/freevideo_engine/agent/whitebox.pynew file mode 100644index 0000000..95a157b--- /dev/null+++ b/freevideo_engine/agent/whitebox.py@@ -0,0 +1,211 @@+"""White-model scenes: the Agent writes the scene, the user's page renders it, the video joins the library.++The video is a rough stand-in for the real shot (boxes, camera, motion). A confirmed one is then used as a+reference video that only guides the first steps (early_steps), so the final video keeps its layout and motion."""+import json+import os+import secrets+import shutil+import threading+import time+import uuid+from pathlib import Path++from . import media+from .jobs import JobError++ID = r'^wb[0-9a-f]{10}$'+MAX_CODE = 64 * 1024+MAX_VIDEO_BYTES = 64 * 1024 * 1024+MAX_SECONDS = 12+MIN_SECONDS = 1+KEPT = 20+PAGE_FRESH = 15. # A page that asked for work this recently is open.+RENDER_LEASE = 150. # A claimed scene nobody finished goes back to the queue once, then fails.+EARLY_STEPS = 3 # For an 8-step first pass; other lengths scale by 3/8 (20 steps -> 8).+FOLLOW_RATIO = 3 / 8+SCENE_PROMPT = """You are a 3D blocking programmer. From one shot description, write a Three.js scene that builds a white model with as few shapes as possible: only boxes, cylinders, spheres, cones and planes, with no textures, no external files and no text. It is a composition and motion reference for a video model, not a finished picture: shapes must be recognisable, with plausible positions, scale and motion paths.++Shot description: {SHOT}++Output rules (follow strictly):+1. Output only one ES module that exports `export function build(THREE)` and returns `{ scene, camera, update }`.+2. `update(t)`: t is in seconds, 0 to 5.0. Every object and the camera depend only on t (a pure function: no Date, no Math.random, no accumulated state). The whole action in the description must finish within 5 seconds, and the camera and the main subject stay visible throughout.+3. Right-handed, y up, metres, true scale (a person about 1.7 m, a cat about 0.3 m, a room about 3 m high).+4. Camera: `PerspectiveCamera(fov, 960/544, 0.1, 200)`; set its position and `lookAt` inside `update` on every frame; move it with smooth interpolation (smoothstep or a spline): no jitter, no jumps.+5. Materials: always `new THREE.MeshStandardMaterial({ color, roughness: 1 })`, one simple colour per object that differs from its neighbours (a striking colour for the subject; grey, beige or light blue for the background). Set `mesh.name`; the subject's name starts with `hero`.+6. Lights: one HemisphereLight and one DirectionalLight with shadows; set `scene.background` to a light sky or wall colour. No fog, no post-processing.+7. At most 80 meshes. Build everything in `build`; `update` only changes positions, rotations and scales. No dependency other than the module itself.+8. Write motion as interpolation between key points (position, orientation, scale where needed); no physics simulation.++Return only the code.++Fix list for a retry: THREE.Geometry no longer exists (use BufferGeometry classes); never create objects in `update`; camera position and lookAt target must differ; if too dark use hemisphere light >= 1.2 and directional >= 1.4; if the camera enters an object, move its path farther or higher."""+++def _remux(source, target):+ """A webm recording becomes a real mp4 without re-encoding: the library opens every work as video.mp4."""+ import av+ with av.open(str(source)) as incoming, av.open(str(target), 'w', format='mp4') as outgoing:+ video = incoming.streams.video[0]+ stream = outgoing.add_stream_from_template(video)+ for packet in incoming.demux(video):+ if packet.dts is None:+ continue+ packet.stream = stream+ outgoing.mux(packet)+++class Whitebox:+ def __init__(self, directory, output_root, clock=time.time):+ self.directory, self.root, self.clock = Path(directory), Path(output_root), clock+ self.lock = threading.RLock()+ self.seen = None+ self.records = {}+ try:+ for path in sorted(self.directory.glob('wb*.json')):+ try:+ record = json.loads(path.read_text(encoding='utf-8'))+ except (OSError, ValueError):+ continue+ if isinstance(record, dict) and record.get('id') == path.stem:+ self.records[record['id']] = record+ except OSError:+ pass++ # Storage -------------------------------------------------------------------------------------------+ def _save(self, record):+ self.directory.mkdir(parents=True, exist_ok=True)+ path = self.directory / (record['id'] + '.json')+ temporary = path.with_name(path.name + '.tmp')+ temporary.write_text(json.dumps(record, ensure_ascii=False), encoding='utf-8')+ os.replace(temporary, path)+ stale = sorted(self.records.values(), key=lambda r: r['created_at'])[:-KEPT]+ for old in stale:+ self.records.pop(old['id'], None)+ (self.directory / (old['id'] + '.json')).unlink(missing_ok=True)++ def _get(self, identity):+ record = self.records.get(identity)+ if record is None:+ raise JobError('not_found', 'No such white-model scene')+ return record++ def _expire(self, record):+ """A claim the page never finished: back to waiting once, then failed."""+ if record['status'] == 'rendering' and self.clock() - record['claimed_at'] > RENDER_LEASE:+ if record['attempts'] < 2:+ record['status'] = 'waiting'+ else:+ record.update(status='failed', error='The page did not finish rendering the scene.')+ self._save(record)++ def view(self, record):+ self._expire(record)+ result = {k: record[k] for k in ('id', 'status', 'summary', 'created_at')}+ result['page_open'] = self.page_open()+ if record['status'] == 'ready':+ result.update(work=record['work'], seconds=record['seconds'], early_steps=EARLY_STEPS, early_ratio=FOLLOW_RATIO)+ if record['status'] == 'failed':+ result['error'] = record['error']+ return result++ def page_open(self):+ return self.seen is not None and self.clock() - self.seen <= PAGE_FRESH++ # Agent ---------------------------------------------------------------------------------------------+ def create(self, code, summary, session=None, notice=None):+ if not isinstance(code, str) or not code.strip() or len(code.encode('utf-8')) > MAX_CODE:+ raise JobError('invalid_request', 'Scene code must be nonempty text of at most %d bytes' % MAX_CODE)+ if 'build' not in code:+ raise JobError('invalid_request', 'Scene code must define build(THREE)')+ with self.lock:+ record = dict(id='wb' + secrets.token_hex(5), status='waiting', code=code, summary=str(summary or '')[:500],+ session=session, created_at=self.clock(), claimed_at=None, attempts=0, work=None,+ seconds=None, error=None, notice=notice)+ self.records[record['id']] = record+ self._save(record)+ return self.view(record)++ def take_notice(self, identity):+ """What the server should tell the user's conversation once the scene is ready; handed out once."""+ with self.lock:+ record = self._get(identity)+ notice = record.get('notice')+ if record['status'] != 'ready' or not notice:+ return None+ record['notice'] = None+ self._save(record)+ return dict(notice, session=record['session'], work=record['work'], seconds=record['seconds'])++ def read(self, identity):+ with self.lock:+ return self.view(self._get(identity))++ # Page ----------------------------------------------------------------------------------------------+ def claim(self):+ """The oldest waiting scene with its code, or None; asking at all tells the Agent a page is open."""+ with self.lock:+ self.seen = self.clock()+ for record in sorted(self.records.values(), key=lambda r: r['created_at']):+ self._expire(record)+ if record['status'] == 'waiting':+ record.update(status='rendering', claimed_at=self.clock(), attempts=record['attempts'] + 1)+ self._save(record)+ return dict(id=record['id'], code=record['code'], summary=record['summary'])+ return None++ def fail(self, identity, message):+ with self.lock:+ record = self._get(identity)+ if record['status'] in ('ready', 'failed'):+ return self.view(record)+ record.update(status='failed', error=str(message or 'The scene failed to render')[:500])+ self._save(record)+ return self.view(record)++ def finish(self, identity, upload):+ """Store the page's rendered mp4 as a library work and mark the scene ready."""+ with self.lock:+ record = self._get(identity)+ if record['status'] == 'ready':+ return self.view(record)+ if record['status'] != 'rendering':+ raise JobError('conflict', 'The scene is not being rendered')+ upload = Path(upload)+ if upload.stat().st_size > MAX_VIDEO_BYTES:+ raise JobError('too_large', 'The rendered video is too large')+ try:+ info, suffix = media.validate_upload(upload)+ except (OSError, ValueError, SyntaxError):+ raise JobError('invalid_media', 'The rendered video cannot be decoded') from None+ # The page records with whatever its browser can encode (H.264, VP9, mp4 or webm): content decides, not the name.+ if info['kind'] != 'video' or suffix not in ('.mp4', '.webm') or not MIN_SECONDS <= info['seconds'] <= MAX_SECONDS:+ raise JobError('invalid_media', 'The rendered video must last %d to %d seconds' % (MIN_SECONDS, MAX_SECONDS))+ day = time.strftime('%Y-%m-%d')+ folder = self.root / 'FreeVideo' / day / uuid.uuid4().hex+ folder.mkdir(parents=True)+ try:+ try:+ if suffix != '.webm':+ raise ValueError('already mp4')+ _remux(upload, folder / 'video.mp4')+ except Exception:+ # Codecs mp4 cannot hold (VP8) keep the recording as it came; the content still decodes.+ shutil.copyfile(upload, folder / 'video.mp4')+ (folder / 'prompt.txt').write_text(record['summary'], encoding='utf-8')+ fps = info.get('fps') or 24.+ report = dict(success=True, geometry=dict(width=info['width'], height=info['height'],+ frames=info.get('frames') or round(info['seconds'] * fps), fps=fps, seconds=info['seconds']),+ request_seconds=0, video={}, encoding={}, whitebox=dict(scene=record['id']))+ request = folder / 'video.request.json'+ temporary = request.with_name(request.name + '.tmp')+ temporary.write_text(json.dumps(report), encoding='utf-8')+ os.replace(temporary, request)+ except BaseException:+ shutil.rmtree(folder, ignore_errors=True)+ raise+ with self.lock:+ record.update(status='ready', work='%s/%s' % (day, folder.name), seconds=info['seconds'])+ self._save(record)+ return self.view(record)diff --git a/freevideo_engine/agent/whitebox_presets.py b/freevideo_engine/agent/whitebox_presets.pynew file mode 100644index 0000000..5ea8f0d--- /dev/null+++ b/freevideo_engine/agent/whitebox_presets.py@@ -0,0 +1,26 @@+"""Ready-made white-model scenes (scene code written to the scene prompt); 'try' and the built-in AI render one without a model writing code."""++PRESETS = (+ dict(id='plane', name='纸飞机飞出教室窗外', shot='一架纸飞机从教室后排飞过一排排课桌,穿过敞开的窗户飞向窗外的操场,镜头跟在纸飞机后面。',+ prompt='A realistic classroom scene: a white paper airplane glides from the back of the classroom over rows of wooden desks and blue chairs, passes through the open window in the far wall and flies out over a sunny sports field with a running track, while the camera follows just behind the plane. Natural daylight, realistic materials, smooth camera motion. Soft ambient classroom sounds fading into a light outdoor breeze.',+ code="export function build(THREE) {\n const scene = new THREE.Scene();\n scene.background = new THREE.Color(0xb7d7f0);\n const camera = new THREE.PerspectiveCamera(65, 960 / 544, 0.1, 200);\n const std = c => new THREE.MeshStandardMaterial({ color: c, roughness: 1, side: THREE.DoubleSide });\n const add = (geo, mat, x, y, z, name) => {\n const m = new THREE.Mesh(geo, mat); m.position.set(x, y, z); m.name = name || '';\n m.castShadow = true; m.receiveShadow = true; scene.add(m); return m;\n };\n scene.add(new THREE.HemisphereLight(0xffffff, 0x8a7f70, 1.2));\n const sun = new THREE.DirectionalLight(0xfff4e0, 2.0);\n sun.position.set(12, 10, -4); sun.castShadow = true;\n sun.shadow.camera.left = -15; sun.shadow.camera.right = 15; sun.shadow.camera.top = 15; sun.shadow.camera.bottom = -15;\n sun.shadow.mapSize.set(2048, 2048); scene.add(sun);\n // room x -4..4, z 0..14 (window wall at z = 0), height 3\n add(new THREE.PlaneGeometry(8, 14).rotateX(-Math.PI / 2), std(0xb08a5e), 0, 0, 7, 'floor');\n add(new THREE.PlaneGeometry(8, 14).rotateX(Math.PI / 2), std(0xffffff), 0, 3, 7, 'ceiling');\n add(new THREE.BoxGeometry(0.2, 3, 14), std(0xe6dfc8), -4, 1.5, 7, 'wall_left');\n add(new THREE.BoxGeometry(0.2, 3, 14), std(0xe6dfc8), 4, 1.5, 7, 'wall_right');\n add(new THREE.BoxGeometry(8, 3, 0.2), std(0xe6dfc8), 0, 1.5, 14, 'wall_back');\n // window wall with a 2.4 x 1.4 opening, sill at 1.0\n add(new THREE.BoxGeometry(8, 1.0, 0.2), std(0xe6dfc8), 0, 0.5, 0, 'wall_below');\n add(new THREE.BoxGeometry(8, 0.6, 0.2), std(0xe6dfc8), 0, 2.7, 0, 'wall_above');\n add(new THREE.BoxGeometry(2.8, 1.4, 0.2), std(0xe6dfc8), -2.6, 1.7, 0, 'wall_l');\n add(new THREE.BoxGeometry(2.8, 1.4, 0.2), std(0xe6dfc8), 2.6, 1.7, 0, 'wall_r');\n add(new THREE.BoxGeometry(2.4, 0.05, 0.3), std(0xffffff), 0, 1.0, 0, 'sill');\n add(new THREE.BoxGeometry(5.5, 1.2, 0.05), std(0x2f5b43), -1.5, 1.6, 13.85, 'blackboard');\n // desks: 3 columns x 4 rows\n for (let c = 0; c < 3; c++) for (let r = 0; r < 4; r++) {\n const x = -2.4 + c * 2.4, z = 4 + r * 2.2;\n add(new THREE.BoxGeometry(1.1, 0.06, 0.6), std(0xc98f4f), x, 0.75, z, 'desk_top');\n add(new THREE.BoxGeometry(0.06, 0.75, 0.06), std(0x777777), x - 0.5, 0.375, z - 0.25, 'desk_leg');\n add(new THREE.BoxGeometry(0.06, 0.75, 0.06), std(0x777777), x + 0.5, 0.375, z + 0.25, 'desk_leg');\n add(new THREE.BoxGeometry(0.45, 0.05, 0.45), std(0x4d7cc9), x, 0.45, z + 0.7, 'chair_seat');\n add(new THREE.BoxGeometry(0.45, 0.45, 0.05), std(0x4d7cc9), x, 0.7, z + 0.92, 'chair_back');\n }\n // outside: grass, running track, goal\n add(new THREE.PlaneGeometry(120, 120).rotateX(-Math.PI / 2), std(0x6fae56), 0, -0.02, -50, 'field');\n add(new THREE.PlaneGeometry(30, 4).rotateX(-Math.PI / 2), std(0xc4654a), 0, 0.0, -12, 'track');\n add(new THREE.BoxGeometry(6, 3, 0.2), std(0xffffff), 0, 1.5, -36, 'goal');\n const plane = new THREE.Group(); plane.name = 'hero_paper_plane'; scene.add(plane);\n const wing = new THREE.Mesh(new THREE.ConeGeometry(0.16, 0.5, 3).rotateX(-Math.PI / 2).scale(1.6, 0.35, 1), std(0xffffff));\n wing.castShadow = true; plane.add(wing);\n const ss = x => x * x * (3 - 2 * x);\n const pts = [[0.4, 1.7, 11.5], [0.2, 1.55, 8.5], [-0.3, 1.5, 6.0], [0.0, 1.55, 2.5], [0.0, 1.6, 0.0], [0.3, 1.5, -4], [0.8, 1.2, -10], [1.4, 0.9, -16]]\n .map(p => new THREE.Vector3(...p));\n const curve = new THREE.CatmullRomCurve3(pts, false, 'centripetal');\n const at = t => curve.getPoint(ss(Math.min(1, t / 5)) * 0.6 + Math.min(1, t / 5) * 0.4);\n return {\n scene, camera,\n update(t) {\n const p = at(t), q = at(t + 0.05);\n plane.position.copy(p); plane.lookAt(q); plane.rotateZ(0.25 * Math.sin(t * 2));\n const behind = at(Math.max(0, t - 0.45));\n camera.position.set(behind.x * 0.5, behind.y + 0.4, behind.z + 1.5);\n camera.lookAt(p.x, p.y, p.z - 2);\n },\n };\n}\n"),+ dict(id='coaster', name='第一人称过山车俯冲', shot='第一人称过山车:先爬到最高点停顿,然后俯冲而下,穿过一个大回环,最后冲向远处的游乐园。',+ prompt='A first-person roller coaster ride at a real amusement park: the cart climbs a steep red steel track to the very top, pauses for a heartbeat, then plunges down in a steep drop, whips through a large vertical loop, and races out toward the colourful rides and a Ferris wheel in the distance. Bright sunny day, green trees, realistic steel and paint, strong sense of speed, smooth camera. Rushing wind and the clatter of the wheels on the track.',+ code='export function build(THREE) {\n const scene = new THREE.Scene();\n const camera = new THREE.PerspectiveCamera(65, 960 / 544, 0.1, 300);\n const std = c => new THREE.MeshStandardMaterial({ color: c, roughness: 1 });\n const add = (geo, mat, x, y, z, name, parent) => {\n const m = new THREE.Mesh(geo, mat); m.position.set(x, y, z); m.name = name || "";\n m.castShadow = true; m.receiveShadow = true; (parent || scene).add(m); return m;\n };\n const ss = x => x * x * (3 - 2 * x);\n const lerp = (a, b, u) => a + (b - a) * u;\n const clamp01 = x => Math.min(1, Math.max(0, x));\n const sun = new THREE.DirectionalLight(0xffffff, 1.8);\n sun.castShadow = true; sun.shadow.mapSize.set(2048, 2048);\n sun.shadow.camera.left = -60; sun.shadow.camera.right = 60; sun.shadow.camera.top = 60; sun.shadow.camera.bottom = -60; sun.shadow.camera.far = 200;\n scene.add(sun);\n\n scene.background = new THREE.Color(0xbfdcf0);\n scene.add(new THREE.HemisphereLight(0xdfeeff, 0x6a7a5a, 1.1)); sun.position.set(-40, 80, 30);\n add(new THREE.PlaneGeometry(600, 600).rotateX(-Math.PI / 2), std(0x7fa86a), 0, 0, 0, "ground");\n // track as a curve: climb, crest, drop, loop, run-out\n const pts = [[0,2,60],[0,12,40],[0,30,20],[0,44,6],[0,46,-4],[0,40,-12],[0,18,-24],[0,4,-34],[0,3,-46],[0,10,-56],[0,18,-50],[0,14,-40],[0,3,-48],[0,2,-70],[0,2,-110]];\n const curve = new THREE.CatmullRomCurve3(pts.map(p => new THREE.Vector3(...p)));\n const N = 120;\n for (let i = 0; i < N; i++) {\n const p = curve.getPoint(i / N), q = curve.getPoint((i + 1) / N);\n const seg = add(new THREE.BoxGeometry(2.6, 0.25, 1.1 * p.distanceTo(q) + 0.2), std(0xd23b2e), p.x, p.y, p.z, "track");\n seg.lookAt(q);\n if (i % 6 === 0 && p.y > 2.5) add(new THREE.BoxGeometry(0.35, p.y, 0.35), std(0xe8e8e8), p.x, p.y / 2 - 0.2, p.z, "pillar");\n }\n for (let i = 0; i < 60; i++) { const a = i * 2.399, r = 20 + (i * 13) % 70; add(new THREE.ConeGeometry(2.2, 7, 6), std(0x3f7a3a), Math.cos(a) * r, 3.5, -40 + Math.sin(a) * r * 1.6, "tree"); }\n // distant park\n const cols = [0xf2c14e, 0x4e9bd6, 0xd65e8b, 0x8bd64e];\n for (let i = 0; i < 14; i++) add(new THREE.BoxGeometry(5 + i % 3, 3 + (i * 5) % 6, 5), std(cols[i % 4]), -30 + i * 5, 2, -150 - (i % 3) * 8, "park");\n const wheel = add(new THREE.TorusGeometry(12, 0.5, 6, 20), std(0xf0f0f0), 30, 14, -140, "ferris");\n add(new THREE.ConeGeometry(2.5, 5, 8), std(0xe8412c), -20, 6, -125, "tent");\n const hero = new THREE.Group(); hero.name = "hero_cart"; scene.add(hero);\n add(new THREE.BoxGeometry(1.6, 0.6, 2.4), std(0xf2c14e), 0, 0, 0, "hero_cart_body", hero);\n const ease = u => u < 0.28 ? 0.55 * (u / 0.28) * 0.5 * 0 + ss(u / 0.28) * 0.46 : u < 0.36 ? 0.46 + (u - 0.28) / 0.08 * 0.02 : 0.48 + ss((u - 0.36) / 0.64) * 0.52;\n return { scene, camera, update(t) {\n const u = ease(clamp01(t / 5));\n wheel.rotation.z = t * 0.2;\n const p = curve.getPoint(u * 0.98), look = curve.getPoint(Math.min(1, u * 0.98 + 0.03));\n hero.position.copy(p); hero.lookAt(look);\n const dir = look.clone().sub(p).normalize(); dir.y *= 0.55; camera.position.set(p.x, p.y + 1.1, p.z); camera.lookAt(p.x + dir.x * 10, p.y + 1.1 + dir.y * 10, p.z + dir.z * 10);\n camera.rotation.z = 0;\n } };\n}\n'),+ dict(id='drone', name='无人机穿过楼间再拉升', shot='无人机贴着两排高楼之间的街道低空飞行,随后拉升冲出楼顶,镜头跟在无人机后上方。',+ prompt="A cinematic drone shot in a real city: a small red quadcopter drone flies low along a narrow street between two rows of tall concrete and glass buildings, then climbs steeply and bursts out above the rooftops into the open blue sky, while the camera follows behind and above the drone. Natural sunlight, soft shadows, realistic materials, smooth camera motion. Ambient city wind sound and the soft hum of the drone's rotors.",+ code="export function build(THREE) {\n const scene = new THREE.Scene();\n scene.background = new THREE.Color(0xbcd6ea);\n const camera = new THREE.PerspectiveCamera(60, 960 / 544, 0.1, 200);\n const std = c => new THREE.MeshStandardMaterial({ color: c, roughness: 1 });\n const add = (geo, mat, x, y, z, name) => {\n const m = new THREE.Mesh(geo, mat); m.position.set(x, y, z); m.name = name || '';\n m.castShadow = true; m.receiveShadow = true; scene.add(m); return m;\n };\n scene.add(new THREE.HemisphereLight(0xdfeeff, 0x666a70, 1.1));\n const sun = new THREE.DirectionalLight(0xffffff, 1.8);\n sun.position.set(-30, 50, 20); sun.castShadow = true;\n sun.shadow.camera.left = -60; sun.shadow.camera.right = 60; sun.shadow.camera.top = 60; sun.shadow.camera.bottom = -60;\n sun.shadow.camera.far = 150; sun.shadow.mapSize.set(2048, 2048);\n scene.add(sun);\n add(new THREE.PlaneGeometry(200, 200).rotateX(-Math.PI / 2), std(0x5d6168), 0, 0, 0, 'ground');\n add(new THREE.PlaneGeometry(9, 200).rotateX(-Math.PI / 2), std(0x3b3e44), 0, 0.02, 0, 'street');\n const palette = [0xc9b8a0, 0x9fb1c4, 0xd6cfc4, 0xa8a39a, 0xb9c4b0];\n for (let side of [-1, 1]) {\n for (let i = 0; i < 9; i++) {\n const z = -80 + i * 17 + (side > 0 ? 6 : 0);\n const h = 14 + ((i * 7 + (side > 0 ? 3 : 0)) % 5) * 5;\n add(new THREE.BoxGeometry(14, h, 14), std(palette[(i + (side > 0 ? 2 : 0)) % 5]), side * 12.5, h / 2, z, 'building');\n }\n }\n const hero = new THREE.Group(); hero.name = 'hero_drone'; scene.add(hero);\n const body = new THREE.Mesh(new THREE.BoxGeometry(0.9, 0.25, 0.9), std(0xe8412c)); body.castShadow = true; hero.add(body);\n for (const [x, z] of [[-0.7, -0.7], [0.7, -0.7], [-0.7, 0.7], [0.7, 0.7]]) {\n const rotor = new THREE.Mesh(new THREE.CylinderGeometry(0.4, 0.4, 0.03, 12), std(0x222222));\n rotor.position.set(x, 0.17, z); hero.add(rotor);\n }\n const ss = x => x * x * (3 - 2 * x);\n const lerp = (a, b, u) => a + (b - a) * u;\n const heroAt = t => {\n const u = Math.min(1, t / 5);\n const z = lerp(60, -60, u);\n const rise = ss(Math.min(1, Math.max(0, (u - 0.55) / 0.35)));\n return new THREE.Vector3(1.5 * Math.sin(u * 6), lerp(3.2, 42, rise), z);\n };\n return {\n scene, camera,\n update(t) {\n const p = heroAt(t);\n hero.position.copy(p);\n const ahead = heroAt(t + 0.1);\n hero.rotation.set(0.25 * Math.min(1, (ahead.z - p.z) / -0.1 * 0.02), 0, 0);\n hero.rotation.x = -0.18; hero.rotation.z = 0.12 * Math.sin(t * 1.3);\n const u = Math.min(1, t / 5);\n const lift = ss(Math.min(1, Math.max(0, (u - 0.55) / 0.35)));\n camera.position.set(p.x * 0.4, p.y + 2.2 + 3 * lift, p.z + 9 + 2 * lift);\n camera.lookAt(p.x, p.y + 0.3, p.z - 4);\n },\n };\n}\n"),+ dict(id='cat', name='橘猫跳上书架', shot='一只橘猫在客厅地板上蹲下蓄力,沿着错落的书架层板一级一级跳上去,最后蹲在最高一层,镜头侧面平移并略微上摇跟随。',+ prompt='A ginger cat crouches on a living room floor, gathers itself and leaps up a staggered wooden bookshelf, jumping from one shelf to the next between rows of books, and finally settles on the top shelf, while the camera pans sideways and tilts up slightly to follow. Warm indoor daylight, realistic fur and wood, soft shadows, smooth camera motion. Soft room tone and the light thud of paws on wood.',+ code='export function build(THREE) {\n const scene = new THREE.Scene();\n const camera = new THREE.PerspectiveCamera(65, 960 / 544, 0.1, 300);\n const std = c => new THREE.MeshStandardMaterial({ color: c, roughness: 1 });\n const add = (geo, mat, x, y, z, name, parent) => {\n const m = new THREE.Mesh(geo, mat); m.position.set(x, y, z); m.name = name || "";\n m.castShadow = true; m.receiveShadow = true; (parent || scene).add(m); return m;\n };\n const ss = x => x * x * (3 - 2 * x);\n const lerp = (a, b, u) => a + (b - a) * u;\n const clamp01 = x => Math.min(1, Math.max(0, x));\n const sun = new THREE.DirectionalLight(0xffffff, 1.8);\n sun.castShadow = true; sun.shadow.mapSize.set(2048, 2048);\n sun.shadow.camera.left = -60; sun.shadow.camera.right = 60; sun.shadow.camera.top = 60; sun.shadow.camera.bottom = -60; sun.shadow.camera.far = 200;\n scene.add(sun);\n\n scene.background = new THREE.Color(0xe9e2d6);\n scene.add(new THREE.HemisphereLight(0xfff4e6, 0x8a7f72, 1.2)); sun.position.set(6, 10, 8);\n add(new THREE.PlaneGeometry(14, 10).rotateX(-Math.PI / 2), std(0xb88f65), 0, 0, 0, "floor");\n add(new THREE.BoxGeometry(14, 4, 0.2), std(0xe9e2d6), 0, 2, -1.2, "wall");\n // bookshelf: two side boards, staggered shelves\n add(new THREE.BoxGeometry(0.1, 2.6, 0.5), std(0x7a5232), -1.4, 1.3, -0.9, "side");\n add(new THREE.BoxGeometry(0.1, 2.6, 0.5), std(0x7a5232), 1.4, 1.3, -0.9, "side");\n const shelves = [[0.6, -0.9], [1.2, -0.4], [1.8, 0.2], [2.4, 0.6]];\n const stepsX = [-0.9, -0.2, 0.5, 1.0], ys = [0.55, 1.05, 1.55, 2.1];\n for (let i = 0; i < 4; i++) {\n add(new THREE.BoxGeometry(1.0, 0.06, 0.5), std(0x9a6b3f), stepsX[i], ys[i] - 0.03, -0.9, "shelf");\n for (let k = 0; k < 3; k++) add(new THREE.BoxGeometry(0.12, 0.28, 0.3), std([0x3c6ea8, 0xb04a4a, 0x4a8a5a][(i + k) % 3]), stepsX[i] - 0.3 + k * 0.2, ys[i] + 0.14, -1.0, "book");\n }\n add(new THREE.BoxGeometry(0.9, 0.5, 0.5), std(0x6b7b8c), -2.6, 0.25, 0.6, "sofa_block");\n const cat = new THREE.Group(); cat.name = "hero_cat"; scene.add(cat);\n const orange = std(0xe58a2e);\n const body = add(new THREE.BoxGeometry(0.42, 0.22, 0.2), orange, 0, 0.18, 0, "hero_cat_body", cat);\n add(new THREE.SphereGeometry(0.11, 10, 8), orange, 0.26, 0.28, 0, "hero_cat_head", cat);\n add(new THREE.ConeGeometry(0.035, 0.08, 4), orange, 0.28, 0.4, 0.06, "hero_cat_ear", cat);\n add(new THREE.ConeGeometry(0.035, 0.08, 4), orange, 0.28, 0.4, -0.06, "hero_cat_ear", cat);\n const tail = add(new THREE.CylinderGeometry(0.03, 0.03, 0.4, 6), orange, -0.34, 0.3, 0, "hero_cat_tail", cat);\n tail.rotation.z = 0.8;\n // waypoints: floor crouch, jump to each shelf\n const wp = [[-1.9, 0.0, 0.1], [-1.9, 0.0, 0.1], [stepsX[0], ys[0], -0.9], [stepsX[1], ys[1], -0.9], [stepsX[2], ys[2], -0.9], [stepsX[3], ys[3], -0.9], [stepsX[3], ys[3], -0.9]];\n const times = [0, 0.7, 1.4, 2.3, 3.2, 4.1, 5.0];\n const pos = t => { let i = 0; while (i < 5 && t > times[i + 1]) i++;\n const u = clamp01((t - times[i]) / (times[i + 1] - times[i])); const a = wp[i], b = wp[i + 1];\n const arc = (i >= 1 && i <= 4) ? Math.sin(Math.PI * u) * 0.45 : 0;\n const e = ss(u); return new THREE.Vector3(lerp(a[0], b[0], e), lerp(a[1], b[1], u) + arc, lerp(a[2], b[2], e)); };\n return { scene, camera, update(t) {\n const p = pos(t); cat.position.copy(p);\n cat.scale.y = (t < 0.7) ? 1 - 0.35 * Math.sin(Math.PI * clamp01(t / 0.7)) : 1;\n cat.rotation.z = (t > 0.7 && t < 4.1) ? 0.35 : 0;\n camera.position.set(p.x * 0.6, 0.9 + p.y * 0.5, 2.7);\n camera.lookAt(p.x * 0.85, 0.35 + p.y * 0.85, -0.9);\n } };\n}\n'),+ dict(id='car', name='赛车入弯漂移环绕', shot='一辆红色赛车在直道尽头高速入弯,车尾漂移甩出,镜头围着赛车做半圈环绕,最后跟着车冲出弯道。',+ prompt='A red race car hits the end of a straight at full speed and throws itself into a corner, the rear sliding out in a controlled drift with a little tyre smoke, while the camera orbits half a circle around the car and then follows it out of the bend. Sunny circuit with red and white kerbs, grandstands and green grass, realistic paint and tarmac, strong sense of speed, smooth camera. Roaring engine and the squeal of tyres.',+ code='export function build(THREE) {\n const scene = new THREE.Scene();\n const camera = new THREE.PerspectiveCamera(65, 960 / 544, 0.1, 300);\n const std = c => new THREE.MeshStandardMaterial({ color: c, roughness: 1 });\n const add = (geo, mat, x, y, z, name, parent) => {\n const m = new THREE.Mesh(geo, mat); m.position.set(x, y, z); m.name = name || "";\n m.castShadow = true; m.receiveShadow = true; (parent || scene).add(m); return m;\n };\n const ss = x => x * x * (3 - 2 * x);\n const lerp = (a, b, u) => a + (b - a) * u;\n const clamp01 = x => Math.min(1, Math.max(0, x));\n const sun = new THREE.DirectionalLight(0xffffff, 1.8);\n sun.castShadow = true; sun.shadow.mapSize.set(2048, 2048);\n sun.shadow.camera.left = -60; sun.shadow.camera.right = 60; sun.shadow.camera.top = 60; sun.shadow.camera.bottom = -60; sun.shadow.camera.far = 200;\n scene.add(sun);\n\n scene.background = new THREE.Color(0xbdd8ee);\n scene.add(new THREE.HemisphereLight(0xdfeeff, 0x6a7a5a, 1.1)); sun.position.set(-30, 60, 25);\n add(new THREE.PlaneGeometry(400, 400).rotateX(-Math.PI / 2), std(0x6f9a58), 0, 0, 0, "grass");\n // road: straight along -z then curving right (to +x)\n const R = 22, cz = -30;\n add(new THREE.BoxGeometry(10, 0.05, 80), std(0x3d4046), 0, 0.03, 10, "road");\n const arc = []; for (let i = 0; i <= 24; i++) { const a = Math.PI * 0.5 * i / 24; arc.push(i); \n add(new THREE.BoxGeometry(10, 0.05, 2 * R * Math.PI / 2 / 24 + 0.6), std(0x3d4046), R - R * Math.cos(a), 0.03, cz - R * Math.sin(a), "bend").rotation.y = -a; }\n add(new THREE.BoxGeometry(10, 0.05, 80), std(0x3d4046), R + 40, 0.03, cz - R, "road2").rotation.y = Math.PI / 2;\n for (let i = 0; i < 16; i++) { const a = Math.PI * 0.5 * i / 15; add(new THREE.BoxGeometry(0.6, 0.6, 0.6), std(i % 2 ? 0xffffff : 0xe8412c), (R + 6) - (R + 6) * Math.cos(a) , 0.3, cz - (R + 6) * Math.sin(a), "kerb"); }\n for (let i = 0; i < 8; i++) add(new THREE.BoxGeometry(3, 5, 3), std(0xc9b8a0), -25 + (i % 2) * 4, 2.5, -60 + i * 6, "stand");\n const hero = new THREE.Group(); hero.name = "hero_car"; scene.add(hero);\n add(new THREE.BoxGeometry(1.8, 0.55, 4.2), std(0xd9261c), 0, 0.5, 0, "hero_car_body", hero);\n add(new THREE.BoxGeometry(1.3, 0.45, 1.6), std(0x20242a), 0, 0.95, -0.2, "hero_car_cabin", hero);\n for (const [x, z] of [[-1, -1.4], [1, -1.4], [-1, 1.4], [1, 1.4]]) { const w = add(new THREE.CylinderGeometry(0.4, 0.4, 0.3, 12), std(0x111111), x, 0.4, z, "hero_car_wheel", hero); w.rotation.z = Math.PI / 2; }\n const path = t => { const u = clamp01(t / 5); const s = u * (80 + R * Math.PI / 2 + 30) - 40; // arclength from z=40\n if (s < 40 - 0) return { x: 0, z: 40 - s, h: Math.PI }; // heading -z (rotation.y = PI points model -z? model faces -z when rotation 0)\n return null; };\n const pos = t => { const u = clamp01(t / 5), L1 = 70, L2 = R * Math.PI / 2, L3 = 40; const s = ss(u * 0.2 + u * 0.8) * (L1 + L2 + L3);\n if (s < L1) return { x: 0, z: 40 - s, yaw: 0 };\n if (s < L1 + L2) { const a = (s - L1) / R; return { x: R - R * Math.cos(a), z: cz - R * Math.sin(a) + 0, yaw: -a }; }\n const d = s - L1 - L2; return { x: R + d, z: cz - R, yaw: -Math.PI / 2 }; };\n return { scene, camera, update(t) {\n const p = pos(t); const drift = Math.sin(Math.PI * clamp01((t - 1.2) / 2.4)) * 0.55;\n hero.position.set(p.x, 0, p.z); hero.rotation.y = p.yaw - drift;\n const orbit = Math.PI * 0.9 * ss(clamp01((t - 1.0) / 3.0));\n const ang = Math.PI * 0.85 - orbit + p.yaw * -0.0;\n const rad = 9 - 2 * Math.sin(Math.PI * clamp01(t / 5));\n camera.position.set(p.x + Math.sin(ang + (p.yaw)) * rad * -1 * -1, 2.2 + 1.5 * Math.sin(Math.PI * clamp01(t / 5)), p.z + Math.cos(ang + p.yaw) * rad);\n camera.lookAt(p.x, 0.8, p.z);\n } };\n}\n'),+ dict(id='door', name='推门长镜头', shot='短剧长镜头:镜头从走廊里缓缓推向一扇半掩的木门,推开门进入昏黄的客厅,一直推到沙发后的落地窗前停住。',+ prompt='A short-drama long take: the camera glides down a dim hallway toward a half-open wooden door, pushes the door open and enters a warmly lit living room, and keeps moving forward past a sofa until it stops in front of the tall window at the far end. Warm tungsten light, soft evening glow from the window, realistic wood, fabric and plaster, smooth steady camera. Quiet room tone and a faint creak of the door.',+ code='export function build(THREE) {\n const scene = new THREE.Scene();\n const camera = new THREE.PerspectiveCamera(65, 960 / 544, 0.1, 300);\n const std = c => new THREE.MeshStandardMaterial({ color: c, roughness: 1 });\n const add = (geo, mat, x, y, z, name, parent) => {\n const m = new THREE.Mesh(geo, mat); m.position.set(x, y, z); m.name = name || "";\n m.castShadow = true; m.receiveShadow = true; (parent || scene).add(m); return m;\n };\n const ss = x => x * x * (3 - 2 * x);\n const lerp = (a, b, u) => a + (b - a) * u;\n const clamp01 = x => Math.min(1, Math.max(0, x));\n const sun = new THREE.DirectionalLight(0xffffff, 1.8);\n sun.castShadow = true; sun.shadow.mapSize.set(2048, 2048);\n sun.shadow.camera.left = -60; sun.shadow.camera.right = 60; sun.shadow.camera.top = 60; sun.shadow.camera.bottom = -60; sun.shadow.camera.far = 200;\n scene.add(sun);\n\n scene.background = new THREE.Color(0x2b2622);\n scene.add(new THREE.HemisphereLight(0xffe9c8, 0x6a5a48, 1.8)); sun.position.set(-6, 8, -4); sun.intensity = 1.6;\n const wall = std(0xcfc4b0), floor = std(0x8a6a4a);\n // corridor z 6 .. 0, living room z 0 .. -9\n add(new THREE.PlaneGeometry(4, 6).rotateX(-Math.PI / 2), floor, 0, 0, 3, "corr_floor");\n add(new THREE.PlaneGeometry(7, 9).rotateX(-Math.PI / 2), floor, 0, 0, -4.5, "room_floor");\n add(new THREE.PlaneGeometry(7, 9).rotateX(Math.PI / 2), std(0xe4dccc), 0, 2.8, -4.5, "room_ceiling");\n add(new THREE.PlaneGeometry(4, 6).rotateX(Math.PI / 2), std(0xe4dccc), 0, 2.8, 3, "corr_ceiling");\n for (const x of [-2, 2]) add(new THREE.BoxGeometry(0.15, 2.8, 6), wall, x, 1.4, 3, "corr_wall");\n for (const x of [-3.5, 3.5]) add(new THREE.BoxGeometry(0.15, 2.8, 9), wall, x, 1.4, -4.5, "room_wall");\n add(new THREE.BoxGeometry(1.0, 0.7, 0.15), wall, 0, 2.45, 0, "door_lintel");\n add(new THREE.BoxGeometry(1.5, 2.8, 0.15), wall, -1.25, 1.4, 0, "door_wall_l");\n add(new THREE.BoxGeometry(1.5, 2.8, 0.15), wall, 1.25, 1.4, 0, "door_wall_r");\n // wall with window at far end\n add(new THREE.BoxGeometry(3.4, 2.8, 0.15), wall, -1.8, 1.4, -9, "far_l"); add(new THREE.BoxGeometry(3.4, 2.8, 0.15), wall, 1.8, 1.4, -9, "far_r");\n add(new THREE.BoxGeometry(0.2, 2.8, 0.15), wall, 0, 1.4, -9, "far_mid");\n add(new THREE.PlaneGeometry(0.1, 0.1), std(0xbfd8f0), 0, 1.4, -9.1, "sky_glow").scale.set(70, 28, 1);\n const hinge = new THREE.Group(); hinge.position.set(-0.5, 0, 0); scene.add(hinge);\n add(new THREE.BoxGeometry(1.0, 2.1, 0.06), std(0x6b4a2e), 0.5, 1.05, 0, "door", hinge);\n hinge.rotation.y = 0.3;\n add(new THREE.BoxGeometry(2.2, 0.8, 0.9), std(0x6b7f94), 0, 0.4, -6.2, "sofa");\n add(new THREE.BoxGeometry(2.2, 0.5, 0.2), std(0x6b7f94), 0, 0.95, -6.6, "sofa_back");\n add(new THREE.BoxGeometry(1.2, 0.35, 0.7), std(0x5a3a22), 0, 0.18, -4.6, "table");\n add(new THREE.CylinderGeometry(0.25, 0.25, 1.6, 10), std(0x3d6b45), 2.6, 0.8, -7.5, "plant");\n add(new THREE.BoxGeometry(0.9, 1.0, 0.1), std(0xb05a3a), -3.4, 1.6, -3, "picture");\n add(new THREE.BoxGeometry(0.5, 1.7, 0.4), std(0x2b2b30), -3.0, 0.85, -1.5, "hero_figure");\n return { scene, camera, update(t) {\n const u = clamp01(t / 5);\n hinge.rotation.y = 0.3 + 1.1 * ss(clamp01((t - 1.0) / 1.2));\n const z = lerp(5.5, -4.6, ss(u * 0.85 + 0.15 * u));\n camera.position.set(0.15 * Math.sin(u * 3), 1.55 - 0.1 * ss(clamp01((u - 0.6) / 0.4)), z);\n camera.lookAt(0.1 * Math.sin(u * 3 + 0.5), 1.4, z - 4);\n } };\n}\n'),+)+++def preset(identity):+ return next((row for row in PRESETS if row['id'] == identity), None)diff --git a/freevideo_engine/comfy_assets.py b/freevideo_engine/comfy_assets.pyindex ff304d4..ecbf311 100644--- a/freevideo_engine/comfy_assets.py+++ b/freevideo_engine/comfy_assets.py@@ -75,7 +75,7 @@ def resolve_assets(serialized, input_directory):root = Path(input_directory).resolve()result = {}for row in rows:- if not isinstance(row, dict) or set(row) - {'file', 'role', 'enabled', 'cast'}:+ if not isinstance(row, dict) or set(row) - {'file', 'role', 'enabled', 'cast', 'early_steps'}:raise ValueError('Invalid media item')# The character an @ mention added the row for; a continued part keeps one picture per character.if 'cast' in row and (set(row['cast'] if isinstance(row['cast'], dict) else ('?',)) != {'id'}@@ -108,6 +108,10 @@ def resolve_assets(serialized, input_directory):reference = {'kind': kind, 'path': str(path)}if cast_id(row) is not None:reference['cast'] = cast_id(row) # only a continued part reads it (comfy_media.continued)+ if 'early_steps' in row:+ from .media_request import validate_early_steps+ validate_early_steps(dict(row, kind=kind))+ reference['early_steps'] = row['early_steps']result.setdefault('references', []).append(reference)else:raise ValueError('Media role must be first, last or reference')