Skip to content

Commit a85f52c

Browse files
[feat] canvas: optimize prompts with the Agent model (#154)
## summary - Follow the Agent panel's runtime, model and thinking level when optimizing a prompt, including the CLI default for Codex and Claude Code. The canvas sends the current selection with the request; the Agent tool passes the same selection, so both entry points share one model choice. - Add a `prompt_rewrite` bridge for CLI runtimes and map H3 official and Seedance 2.0 video profiles to their built-in optimization skills. - Report optimization progress, success and failure through canvas toasts; the running label keeps its three dots, slowly pulses between the muted and normal text colours, and the composer border shows a thin rotating band. - Share the assistant model selection helper between chat sending and prompt optimization, and update both user guides. ## validation - `npx tsc --noEmit` in the PR worktree: clean. - `pytest tests/test_prompt_optimization.py` (main checkout, identical service and tests): 2 passed, 1 skipped. - `python scripts/generate_contracts.py --check`: verified. - `node scripts/check-i18n.mjs`: 0 problems. Refs: #154
1 parent 815dfeb commit a85f52c

26 files changed

Lines changed: 694 additions & 122 deletions

File tree

‎apps/api/src/opendirector_api/creation.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ async def optimize_prompt(project_id: UUID, item_id: UUID, value: PromptOptimiza
8888
x_opendirector_session: UUID = Header()):
8989
return await prompt_optimization.rewrite(
9090
project_id, item_id, str(x_opendirector_session), value.prompt,
91-
value.inlineReferences, value.preferredModelId)
91+
value.inlineReferences, value.preferredModelId, value.assistantModel)
9292

9393
@router.get('/projects')
9494
def list_projects():

‎apps/api/src/opendirector_api/localization.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
'prompt_optimization_empty': 'Enter a prompt to optimize.',
2121
'prompt_optimization_refs': 'Prompt references changed. Try again.',
2222
'prompt_optimization_node': 'Select an image or video node.',
23-
'prompt_optimization_model': 'Configure a text model in Settings before optimizing a prompt.',
24-
'prompt_optimization_vision': 'Enable supported image or video inputs on a text model to optimize prompts with visual references.',
23+
'prompt_optimization_model': 'Choose an available chat model in the Agent panel.',
24+
'prompt_optimization_vision': 'The selected chat model cannot use these visual references. Choose an Agent model with image or video input.',
25+
'prompt_optimization_cli': 'The selected Agent CLI could not optimize the prompt. Check its installation, login, and model.',
26+
'prompt_optimization_cli_unsupported': 'The selected Agent CLI does not support standalone prompt optimization. Switch the Agent or model in the Agent panel.',
2527
'prompt_optimization_media': 'The video reference duration is invalid.',
2628
'prompt_optimization_too_large': 'The references are too large for prompt optimization. Reduce their size or number and try again.',
2729
'prompt_optimization_result': 'The model did not return a usable prompt. Your original text was kept.',

‎docs/guides/user-guide.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ The canvas saves automatically. Before closing it or switching projects, check t
8989
3. Connect a source node that has content to the target node, or type `@` in the input field to select reference assets.
9090
4. Check the references and parameters, then generate. When the task finishes, view its result in the node or **Generation history**.
9191

92-
In an image or video node, enter a draft and click the **Optimize prompt** wand below the editor. OpenDirector uses the draft and its references with a configured text model, applying the built-in skill when the selected video model is H3. In model settings, enable the input types supported by the model's API. OpenDirector sends full-length videos to models with video input and samples frames for models with image input only. Audio files and video soundtracks are sent only when the model accepts audio. Press Ctrl+Z (Command+Z on Mac) to undo the replacement. Edit the text to optimize it again. Optimizing a prompt does not submit a generation job.
92+
In an image or video node, enter a draft and click the **Optimize prompt** wand below the editor. Optimization follows the model selected in the Agent panel, including the CLI default for Codex and Claude Code. The built-in skill applies to H3 and Seedance 2.0 video models. In model settings, enable the input types supported by the model's API. OpenDirector sends full-length videos to models with video input and samples frames for models with image input only. Audio files and video soundtracks are sent only when the model accepts audio. Press Ctrl+Z (Command+Z on Mac) to undo the replacement. Edit the text to optimize it again. Optimizing a prompt does not submit a generation job.
9393

9494
For GPT Image models, open **Generation settings** and choose **Quality** from **Auto**, **Low**, **Medium**, or **High**. **Auto** keeps the service default. The choice is saved with the project and frozen when a task is submitted. Changing it later affects subsequent tasks. Other image services do not show this option. Standalone image tools retain their service default. The Agent can set the same value through `parameters.quality` in the image generation spec.
9595

‎docs/guides/user-guide.zh-CN.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ vLLM-Omni 视频连接支持直接输入小数秒,例如 `6.583333`。CLI 可
8989
3. 将有内容的来源节点连接到目标节点,或在输入框中输入 `@` 选择参考素材。
9090
4. 检查参考和参数,点击生成。完成后在节点或“生成历史”中查看结果。
9191

92-
在图片或视频节点中输入草稿,点输入框下方的「提示词优化」魔法棒。优化会使用项目中的参考素材和已配置的文本模型,H3 视频优先使用内置技能。在模型设置的「输入能力」里按接口实际支持的类型勾选。支持视频输入时发送全长视频,只支持图片输入时才从视频抽帧。只有支持音频输入的模型才会收到音频素材,全长视频也只在这种情况下保留音轨。结果替换草稿后可按 Ctrl+Z(Mac 上按 ⌘Z)撤销。修改文字后可再次优化。优化只修改提示词,不提交生成任务。
92+
在图片或视频节点中输入草稿,点输入框下方的「提示词优化」魔法棒。优化沿用右侧 Agent 当前选择的模型,Codex 和 Claude Code 也可使用 CLI 默认模型。H3 和 Seedance 2.0 视频会使用内置技能。在模型设置的「输入能力」里按接口实际支持的类型勾选。支持视频输入时发送全长视频,只支持图片输入时才从视频抽帧。只有支持音频输入的模型才会收到音频素材,全长视频也只在这种情况下保留音轨。结果替换草稿后可按 Ctrl+Z(Mac 上按 ⌘Z)撤销。修改文字后可再次优化。优化只修改提示词,不提交生成任务。
9393

9494
GPT Image 模型可在“生成参数”中选择“质量”,取值为“自动、低、中、高”。“自动”沿用服务默认行为。选择会保存在项目中,并在任务提交时固定;之后修改只影响后续任务。其他图片服务不显示此选项,独立图片工具仍使用服务默认值。Agent 可通过图片生成规格中的 `parameters.quality` 设置同一参数。
9595

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
---
2+
name: h3-official-prompt-optimization
3+
description: Rewrite a MiniMax H3 video draft into the model's official prompt structure (instruction line plus integrated_multimodal_description, overall_soundscape and non_diegetic_music) while preserving OpenDirector's inline reference markers. Use to compare output against h3-prompt-optimization.
4+
---
5+
6+
# H3 official-structure prompt optimization
7+
8+
Rewrite the draft into the structure MiniMax documents for H3 generation modes
9+
(T2VA, I2VA, FL2VA, L2VA, Ref2VA). Keep the draft's own language for everything
10+
descriptive; the structure and its tokens are English. Return only the rewritten
11+
prompt.
12+
13+
## Hard invariants
14+
15+
- Keep every `[[OD_REF_n]]` marker exactly once and in its existing order. These
16+
markers are the editor's inline references; never delete them, renumber them,
17+
or move them into a different shot.
18+
- Write the description text in the draft's language. A Chinese draft stays
19+
Chinese; do not translate it into English.
20+
- Never translate the structural tokens. Field names, `[Shot N]`, the cue tags
21+
`<d>`, `<scenetrans>`, `<cutoff>`, `<Picture N>`, `<Video N>`, `<Audio N>`,
22+
speaker ids such as `(S1)`, and the mode instruction line stay exactly as this
23+
skill writes them, in English. A translated token is a broken prompt.
24+
- Keep dialogue, lyrics and visible on-screen text verbatim in the language they
25+
were supplied in.
26+
- Do not invent references, dialogue, sound or shots that the draft and its
27+
references do not support.
28+
29+
## Skeleton
30+
31+
```text
32+
<optional mode instruction line>
33+
34+
integrated_multimodal_description: [Shot 1] ...
35+
36+
overall_soundscape: ...
37+
38+
non_diegetic_music: ...
39+
```
40+
41+
Leave one blank line between the three fields. Ref2VA replaces them with its six
42+
sections in order: `subject_definitions`, `summary`, `retention_analysis`,
43+
`detailed_description`, `overall_soundscape`, `non_diegetic_music`.
44+
45+
## Mode instruction line
46+
47+
The instruction line is a fixed English sentence. Write it as given; only the
48+
field contents below it follow the draft's language.
49+
50+
T2VA has no instruction line; start directly with
51+
`integrated_multimodal_description:`.
52+
53+
First-frame mode: the first line is exactly
54+
55+
```text
56+
For the target video, at 0.00 seconds into the target video, <Picture 1> (from [Shot 1]) is fully referenced.
57+
```
58+
59+
First-and-last-frame mode: the first line states how both pictures align, naming
60+
the shot each picture comes from and the exact second mark, formatted to two
61+
decimals.
62+
63+
Last-frame mode: the first line aligns the single picture with the final
64+
`[Shot N]` at the duration's second mark. That picture belongs to the last shot,
65+
not to Shot 1.
66+
67+
## Shots
68+
69+
- `[Shot 1]` carries no timestamp. Later shots are `[Shot 2] At 00:03.500,
70+
镜头切到…`, each with a strictly increasing time inside the duration. The time
71+
notation and `[Shot N]` stay in English; the sentence after them follows the
72+
draft's language.
73+
- Ordinary cuts use the draft's language: `镜头切到`, `画面转到`, `镜头转换为`,
74+
or, in an English draft, `the camera cuts to`, `the shot cuts to`, `the shot
75+
transitions to`, `the shot changes to`, `the shot switches to`. Use
76+
cross-dissolve, fade or wipe only when the draft asks for them.
77+
- A cut must bring new information: subject, space, state, viewpoint or time. To
78+
change only distance or a slight angle, use camera motion instead.
79+
- Open `[Shot 1]` with the overall style and the initial composition, written in
80+
the draft's language: `写实电影感,中景,画面框住…` or
81+
`Live-action, cinematic, a medium-wide shot frames …`.
82+
83+
## Camera motion
84+
85+
Write motion as a natural action inside the sentence, never as tags appended to
86+
the end: motion type first, then amplitude and speed only when they matter. Omit
87+
amplitude and speed for ordinary movement. Use the draft's language.
88+
89+
```text
90+
The camera pushes in with small amplitude at slow speed toward the folded letter.
91+
镜头缓慢推近,小幅,推向她手中的信。
92+
```
93+
94+
Motion types, in the draft's language: 推近 / 拉远, 横摇左 / 右, 横移左 / 右,
95+
上摇 / 下摇, 升降, 环绕, 跟拍, 固定镜头, 轻微抖动 / 强烈抖动, 主观视角,
96+
顺时针 / 逆时针旋转. The English set is zoom in/out, push in/pull out, pan
97+
left/right, truck left/right, tilt up/down, pedestal up/down, arc shot, tracking
98+
shot, static shot, shake slightly/strongly, POV, roll clockwise/counterclockwise.
99+
Amplitude: `小幅` / `大幅` (small/large amplitude). Speed: `慢速` / `快速`
100+
(slow/fast speed).
101+
102+
## Speakers and dialogue
103+
104+
- Give every speaking, singing or off-screen voice a stable id `(S1)`, `(S2)`;
105+
joint lines use `(S1,S2)`. Characters who never vocalize get no id.
106+
- Outside `<d>`: the identifying phrase, the id, the action and the delivery.
107+
Inside `<d>`: only the language tag and the draft's exact words, untranslated.
108+
- Off-screen narration keeps the official English phrase `says in an off-screen
109+
voiceover`, and must add, in the draft's language, that the on-screen
110+
character's lips stay closed.
111+
- A line crossing a cut uses `<scenetrans>` at both ends and states that the
112+
audio continues; speech truncated by the video end uses `<cutoff>`.
113+
114+
```text
115+
The young woman (S1) says: <d>[Chinese] 没事,我下站就到。
116+
年轻女子(S1)轻声说:<d>[Chinese] 没事,我下站就到。
117+
```
118+
119+
## On-screen text
120+
121+
Put visible banners, signs, labels or neon text in ASCII double quotes rather than
122+
CJK corner brackets, and keep the original characters and punctuation
123+
untranslated.
124+
125+
## overall_soundscape
126+
127+
One continuous paragraph of 1–4 sentences in the draft's language, covering
128+
ambience, physical action sounds and non-verbal human sounds across the whole
129+
video. Do not repeat dialogue, singing or diegetic music here. Use `N/A` only
130+
when the draft asks for complete silence.
131+
132+
## non_diegetic_music
133+
134+
1–3 sentences in the draft's language describing music only the audience hears:
135+
instrumentation, tempo, rhythm and dynamic changes. No abstract mood words and no
136+
explanation of the score's emotional purpose. Audio the characters can hear
137+
belongs in the multimodal description instead. Use `N/A` when there is none.
138+
139+
## References
140+
141+
- Cite the official tokens (`<Picture 1>`, `<Video 1>`, `<Audio 1>`) where the
142+
structure calls for them; the application appends the numbered reference list
143+
that maps each token to an uploaded asset.
144+
- Place the matching `[[OD_REF_n]]` marker where that reference drives the action,
145+
so the editor keeps its inline position.
146+
- Keep one role per reference and use it consistently across all fields; never
147+
reference a token the draft did not supply.
148+
149+
## Output
150+
151+
Return the prompt body only: no analysis, no heading, no code fence, no notes
152+
about what changed.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
name: seedance-prompt-optimization
3+
description: Refine an existing Seedance 2.0 video prompt while preserving its references, timing, dialogue and audio constraints. Use for prompt rewriting, not generation or an end-to-end production workflow.
4+
---
5+
6+
# Seedance 2.0 prompt optimization
7+
8+
Use these instructions when refining a video draft for Seedance 2.0
9+
(`doubao-seedance-2.0`, `-mini`, `-fast`). Keep the output in the user's
10+
language and return only the rewritten prompt.
11+
12+
## References
13+
14+
- Keep every `[[OD_REF_n]]` marker exactly once and in its existing order.
15+
Never rewrite a marker as `@图片1` or `[asset-…]`, and never cite a marker the
16+
draft did not supply.
17+
- Give each reference one role and state it next to its marker at first use:
18+
`[[OD_REF_1]] 作为首帧`, `[[OD_REF_2]] 的角色形象`, `参考 [[OD_REF_3]] 的运镜`.
19+
Do not swap, merge or invent roles.
20+
- Keep literal provider tokens such as `<Picture 1>`, `<Video 1>` and
21+
`<Audio 1>` untouched.
22+
23+
## Structure
24+
25+
Write the prompt in this order:
26+
27+
1. **Global setup** — lock the subject, wardrobe, environment and the reference
28+
mapping. Declare the first/last-frame constraint here when the mode supplies
29+
one.
30+
2. **Time-sliced storyboard** — split the requested duration into slices, each
31+
written as time range, shot size, subject, action, environment, lighting and
32+
sound. Use the user's notation for time (`0-3秒:` for Chinese drafts,
33+
`0-3s:` for English ones). The slices must sum to the duration in the
34+
generation settings.
35+
3. **Edit instruction** — only when the draft asks for replacement, on-screen
36+
text, extension or removal. Name the time range and the screen position of
37+
the change.
38+
4. **Stability constraints** — close with the anti-breakdown clause when the shot
39+
contains characters or fast motion: stable faces with clear features, natural
40+
non-rigid motion, no stutter and no flicker.
41+
42+
## Craft
43+
44+
- One camera move per slice. When the shot needs a push and an orbit, split them
45+
into two slices instead of stacking them in one.
46+
- Order actions chronologically and keep each slice legible: a 4–8 second draft
47+
carries one beat, 13–15 seconds carries three or four slices.
48+
- Keep quoted dialogue exactly as written and name who speaks and when. Keep the
49+
audio, music and sound-effect requirements the draft states; never invent
50+
dialogue or sound.
51+
- Make character, wardrobe, lighting and scene continuity explicit when the
52+
input depends on it.
53+
- In first/last-frame mode describe how the action travels from the selected
54+
first frame to the last. In reference or continuation mode preserve the
55+
previous clip's endpoint and momentum.
56+
- For a requested replacement, name the source subject and its replacement and
57+
preserve positions, poses, timing and the untouched subjects and scene. Add no
58+
replacement instruction when none was requested.
59+
- Do not add resolution, frame rate, subtitles, watermarks, logos or stylistic
60+
defaults the draft did not ask for. Resolution and duration are generation
61+
parameters, not prompt text.
62+
- Do not infer movement, dialogue or sound from reference metadata alone.
63+
- Do not use emoji or decorative symbols inside the prompt.

‎packages/core/src/opendirector/agent/gateway.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ async def execute(self, call_id: str, name: str, arguments: dict[str, JsonValue]
9797
value = await self.business.execute(self.run.project_id, self.run.turn_id, self.run.session,
9898
call_id, name, arguments, self.roots, self.run.config,
9999
self.run.runtime if self.run.config is None else None,
100-
self.execution_directory)
100+
self.execution_directory, **({'runtime_model': self.run.runtime_model, 'thinking': self.run.thinking}
101+
if name == 'prompt_optimize' else {}))
101102
if name == 'read_project':
102103
value['modelSelections'] = dict(self.model_selections)
103104
error = None

0 commit comments

Comments
 (0)