diff --git a/calliope-web/src/lib/components/desk/Artboard.svelte b/calliope-web/src/lib/components/desk/Artboard.svelte index a1f211c5..a97c83aa 100644 --- a/calliope-web/src/lib/components/desk/Artboard.svelte +++ b/calliope-web/src/lib/components/desk/Artboard.svelte @@ -240,7 +240,8 @@ > 第 {job?.id} 次生成{wf?.name ?? '历史作品'}{wf?.name ?? '历史作品'}{#if dims}{dims}{/if} @@ -703,6 +704,8 @@ color: var(--text-2); } .media-tag { + white-space: nowrap; + flex-shrink: 0; display: inline-flex; align-items: center; height: 18px; @@ -713,6 +716,8 @@ color: var(--on-media); } .result-title small { + min-width: 0; + flex-shrink: 1; color: var(--on-media); font-size: 11px; white-space: nowrap; diff --git a/calliope-web/src/lib/components/desk/StudioDesk.svelte b/calliope-web/src/lib/components/desk/StudioDesk.svelte index 61bbc637..e3d14e2d 100644 --- a/calliope-web/src/lib/components/desk/StudioDesk.svelte +++ b/calliope-web/src/lib/components/desk/StudioDesk.svelte @@ -291,7 +291,9 @@ } let actionRevision = 0; + let promptContextEpoch = $state(0); function pickWorkflow(id: number) { + ++promptContextEpoch; bayOpen = false; if (slotBusy || submitting) return; actionRevision++; @@ -1279,6 +1281,8 @@ > + import { page } from '$app/state'; + import { promptTool } from '$lib/components/prompt-tools/state.svelte'; + import ToolButtons from '$lib/components/prompt-tools/ToolButtons.svelte'; + import ResultBand from '$lib/components/prompt-tools/ResultBand.svelte'; import { resolve } from '$app/paths'; import Modal from '$lib/components/ui/Modal.svelte'; import { tick } from 'svelte'; @@ -19,6 +23,8 @@ /** Scrollable creative inputs with a separate, always visible submission footer. */ interface Props { header?: Snippet; + contextEpoch?: number; + contextIdentity?: string; dock?: boolean; ds: DeskSchema; values: FormValues; @@ -47,6 +53,8 @@ let { header, + contextEpoch = 0, + contextIdentity = '', dock = false, ds, values, @@ -108,6 +116,27 @@ const promptVal = $derived( ds.prompt ? String(values[ds.prompt.nodeId] ?? '') : '', ); + const positiveTool = promptTool( + () => promptVal, + () => + `${page.url.href}|${contextIdentity}|${contextEpoch}|${current?.id}|${current?.kind}|${ds.prompt?.nodeId}|positive`, + (v) => { + if (ds.prompt) onSet(ds.prompt.nodeId, v); + }, + '正向提示词', + () => negativeTool.close(), + ); + const negativeTool = promptTool( + () => (ds.negative ? String(values[ds.negative.nodeId] ?? '') : ''), + () => + `${page.url.href}|${contextIdentity}|${contextEpoch}|${current?.id}|${current?.kind}|${ds.negative?.nodeId}|negative`, + (v) => { + if (ds.negative) onSet(ds.negative.nodeId, v); + }, + '反向提示词', + () => positiveTool.close(), + ); + const promptErr = $derived( showErrors && ds.prompt ? errors[ds.prompt.nodeId] : undefined, ); @@ -210,6 +239,7 @@ >{ds.negative ? '正向提示词' : '提示词'} {#if ds.prompt.required}必填{/if} + + {:else if tool.state === "result"} + {#if tool.confirm}替换会覆盖你刚才的修改 + {:else}{#if tool.action === "translate"}{/if}{/if} + {:else}{/if} + + +
+ {#if tool.text.length > 8000}内容较长,处理可能较慢{/if} + {#if tool.state === "loading"}
+ 你可以继续编辑原文;返回前若原文有改动,这次结果会被丢弃 +
+ {:else if tool.state === "stale"}

+ 原文已修改,结果已丢弃 +

+ {:else if tool.state === "error"}

{tool.error}

+ 去指定{tool.action === "translate" ? "翻译" : "润色"}模型 + {:else if tool.result} + {#if tool.changed}

+ 原文已修改,替换会覆盖新改动 +

{/if} +
{tool.result.result}
+
+ {tool.result.model.following_default ? "跟随默认 · " : ""}{tool + .result.model.name} · {tool.result.elapsed_ms} ms技术片段已逐字核对 {tool.result.protected_fragments.length} 处原提示词未改动 · 点「替换」才会写入 +
+ {/if} +
+ +{/if} diff --git a/calliope-web/src/lib/components/prompt-tools/ToolButtons.svelte b/calliope-web/src/lib/components/prompt-tools/ToolButtons.svelte new file mode 100644 index 00000000..1bedf87a --- /dev/null +++ b/calliope-web/src/lib/components/prompt-tools/ToolButtons.svelte @@ -0,0 +1,28 @@ + + +
+ + +
diff --git a/calliope-web/src/lib/components/prompt-tools/prompt-tools.css b/calliope-web/src/lib/components/prompt-tools/prompt-tools.css new file mode 100644 index 00000000..d83ec5c9 --- /dev/null +++ b/calliope-web/src/lib/components/prompt-tools/prompt-tools.css @@ -0,0 +1,182 @@ +/* Approved HEL-837 v2; shared by desk and workbench fields. */ +.pt-buttons { + display: flex; + align-items: center; + gap: 4px; + flex: none; +} +.pt-btn { + height: 22px; + padding: 0 7px; + border-radius: 5px; + display: inline-flex; + align-items: center; + gap: 4px; + font-size: 11.5px; + color: var(--tx-2); + border: 1px solid transparent; + position: relative; + background: transparent; + cursor: pointer; + white-space: nowrap; +} +.pt-btn:hover { + background: rgb(var(--ink) / 0.06); + color: var(--tx-1); +} +.pt-btn.on { + color: var(--tx-1); + border-color: color-mix(in srgb, var(--scene) 50%, transparent); + background: color-mix(in srgb, var(--scene) 12%, transparent); +} +.pt-btn:disabled { + opacity: 0.45; + cursor: not-allowed; +} +.pt-btn small { + font: 500 10px var(--mono); + color: var(--tx-3); +} +.pt-btn.running::after { + content: ""; + position: absolute; + height: 2px; + bottom: 0; + left: 2px; + right: 2px; + background: var(--scene); + animation: pt-progress 1s linear infinite; +} +.ptband { + flex: none; + display: flex; + flex-direction: column; + min-width: 0; + border-top: 1px solid color-mix(in srgb, var(--scene) 30%, var(--line)); + background: color-mix(in srgb, var(--scene) 5%, transparent); + max-height: var(--ptband-max-h); + animation: pt-in var(--t-base) var(--ease-out); +} +.pt-h { + display: flex; + align-items: center; + gap: 8px; + padding: 6px 10px 6px 12px; + flex: none; + min-height: 38px; +} +.pt-h b { + font-size: 12.5px; + white-space: nowrap; +} +.pt-dir { + font: 500 11px var(--mono); + color: var(--tx-2); + padding: 0 6px; + height: 20px; + display: inline-flex; + align-items: center; + border-radius: 4px; + background: rgb(var(--ink) / 0.05); + white-space: nowrap; +} +.pt-b { + flex: 1; + overflow: auto; + padding: 0 12px 8px; + min-height: 0; + display: flex; + flex-direction: column; + gap: 6px; +} +.pt-res { + font-size: 13px; + line-height: 1.6; + color: var(--tx-1); + white-space: pre-wrap; + overflow-wrap: anywhere; + user-select: text; + padding: 6px 10px; + border-radius: 8px; + background: var(--well); + border: 1px solid var(--line); +} +.pt-note { + display: flex; + gap: 12px; + font-size: 11px; + color: var(--tx-3); + flex-wrap: wrap; +} +.pt-verified { + color: var(--ok); +} +.pt-confirm { + font-size: 11.5px; + color: var(--warn); + margin: 0; +} +.pt-error { + color: var(--err); + margin: 0; +} +.pt-wait { + display: flex; + flex-direction: column; + gap: 7px; + font-size: 11.5px; + color: var(--tx-3); +} +.pt-wait i { + height: 10px; + border-radius: 4px; + background: linear-gradient( + 90deg, + rgb(var(--ink) / 0.06), + rgb(var(--ink) / 0.13), + rgb(var(--ink) / 0.06) + ); + background-size: 200% 100%; + animation: pt-skeleton 1.2s linear infinite; +} +.pt-wait i:nth-child(2) { + width: 70%; +} +.prompt-editor { + min-width: 0; +} +.prompt-editor textarea { + width: 100%; + box-sizing: border-box; +} +.prompt-editor-label { + display: flex; + align-items: center; + justify-content: space-between; + font-size: 11.5px; + color: var(--tx-2); + margin-bottom: 4px; +} +@keyframes pt-in { + from { + opacity: 0; + } +} +@keyframes pt-progress { + 50% { + opacity: 0.3; + } +} +@keyframes pt-skeleton { + to { + background-position: -200% 0; + } +} +html[data-motion="calm"] .ptband, +html[data-motion="off"] .ptband, +html[data-motion="calm"] .pt-btn::after, +html[data-motion="off"] .pt-btn::after, +html[data-motion="calm"] .pt-wait i, +html[data-motion="off"] .pt-wait i { + animation: none; +} diff --git a/calliope-web/src/lib/components/prompt-tools/state.svelte.ts b/calliope-web/src/lib/components/prompt-tools/state.svelte.ts new file mode 100644 index 00000000..bc902f57 --- /dev/null +++ b/calliope-web/src/lib/components/prompt-tools/state.svelte.ts @@ -0,0 +1,240 @@ +import { onDestroy, untrack } from "svelte"; +import { showToast, toast } from "$lib/toast"; + +export type Action = "translate" | "polish"; +export type Direction = "auto" | "zh-en" | "en-zh"; +export interface TransformResult { + action: Action; + direction: Exclude | null; + direction_source: "auto" | "explicit" | null; + result: string; + protected_fragments: string[]; + model: { + id: string; + name: string; + model: string; + following_default: boolean; + }; + elapsed_ms: number; +} +export function directionFor(text: string): Exclude { + const body = text + .replace(/<[^<>\n]{1,300}>/g, "") + .replace(/:\s*-?\d+(?:\.\d+)?/g, ""); + return /[\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]/.test(body) + ? "zh-en" + : "en-zh"; +} + +/** One field owns its monotonic history, including edits that restore the same text. */ +export function promptTool( + getText: () => string, + getContext: () => string, + setText: (s: string) => void, + label: string, + onOpen?: () => void, +) { + let state = $state<"idle" | "loading" | "result" | "stale" | "error">("idle"); + let result = $state(null); + let error = $state(""); + let action = $state("translate"); + let confirm = $state(false); + let textVersion = $state(0); + let contextVersion = $state(0); + let ticketTextVersion = $state(0); + let seq = 0; + let abort: AbortController | null = null; + let alive = true; + let observedText = untrack(getText); + let observedContext = untrack(getContext); + + function close() { + ++seq; + abort?.abort(); + abort = null; + state = "idle"; + confirm = false; + } + function edit() { + ++textVersion; + confirm = false; + } + function sync() { + const context = getContext(); + const text = getText(); + if (context !== observedContext) { + observedContext = context; + ++contextVersion; + close(); + } + if (text !== observedText) { + observedText = text; + edit(); + } + } + $effect.pre(() => { + getText(); + getContext(); + untrack(sync); + }); + onDestroy(() => { + alive = false; + ++contextVersion; + close(); + }); + + async function run(next: Action, direction: Direction = "auto") { + sync(); + if (!getText().trim()) return; + if (state === "loading" && next === action) { + close(); + toast.info("已取消,原提示词未改动"); + return; + } + onOpen?.(); + close(); + action = next; + state = "loading"; + error = ""; + result = null; + const id = ++seq; + const textRev = textVersion, + contextRev = contextVersion; + const snapshot = getText(), + context = getContext(); + ticketTextVersion = textRev; + const controller = new AbortController(); + abort = controller; + try { + const response = await fetch("/api/prompt-tools/transform", { + method: "POST", + headers: { "Content-Type": "application/json" }, + signal: controller.signal, + body: JSON.stringify({ + action: next, + text: snapshot, + ...(next === "translate" ? { direction } : {}), + }), + }); + const payload = await response.json(); + sync(); + if ( + !alive || + id !== seq || + controller.signal.aborted || + contextRev !== contextVersion || + context !== getContext() + ) + return; + if (textRev !== textVersion) { + state = "stale"; + return; + } + if (!response.ok) { + const detail = payload.detail; + error = + typeof detail === "object" && detail + ? `${detail.message ?? "处理失败"}${detail.model?.name ? " · " + detail.model.name : ""}` + : "处理失败,请重试"; + state = "error"; + return; + } + result = payload as TransformResult; + state = "result"; + } catch (e) { + sync(); + if ( + !alive || + id !== seq || + controller.signal.aborted || + contextRev !== contextVersion + ) + return; + if (textRev !== textVersion) { + state = "stale"; + return; + } + error = + e instanceof TypeError + ? "无法连接工作台服务器,请检查连接后重试" + : "处理失败,请重试"; + state = "error"; + } finally { + if (id === seq) abort = null; + } + } + function replace() { + sync(); + if (state !== "result" || !result) return; + if (textVersion !== ticketTextVersion && !confirm) { + confirm = true; + return; + } + const before = getText(), + output = result.result, + ctxRev = contextVersion, + ctx = getContext(); + setText(output); + edit(); + observedText = output; + const replaceVersion = textVersion; + close(); + const expires = Date.now() + 5000; + showToast(`已替换${label}`, "success", 5000, { + label: "撤销", + run() { + sync(); + if ( + !alive || + Date.now() > expires || + textVersion !== replaceVersion || + contextVersion !== ctxRev || + getContext() !== ctx + ) { + toast.info("内容已变化或已切换,未自动恢复"); + return; + } + setText(before); + observedText = before; + edit(); + }, + }); + } + return { + get state() { + return state; + }, + get result() { + return result; + }, + get error() { + return error; + }, + get action() { + return action; + }, + get confirm() { + return confirm; + }, + get label() { + return label; + }, + get changed() { + return textVersion !== ticketTextVersion; + }, + get text() { + return getText(); + }, + get direction() { + return directionFor(getText()); + }, + run, + close, + edit, + replace, + cancelConfirm() { + confirm = false; + }, + }; +} +export type PromptTool = ReturnType; diff --git a/calliope-web/src/lib/components/ui/Button.svelte b/calliope-web/src/lib/components/ui/Button.svelte index aec35457..360626a4 100644 --- a/calliope-web/src/lib/components/ui/Button.svelte +++ b/calliope-web/src/lib/components/ui/Button.svelte @@ -4,7 +4,8 @@ interface Props { /** `danger` = outlined (list rows); `danger-solid` = confirm dialogs only. */ - variant?: 'primary' | 'secondary' | 'ghost' | 'danger' | 'danger-solid'; + variant?: + 'primary' | 'secondary' | 'ghost' | 'danger' | 'danger-solid' | 'scene'; size?: 'sm' | 'md' | 'lg'; /** Stretch to the container width (e.g. a panel's main action). */ block?: boolean; diff --git a/calliope-web/src/lib/components/workbench/AssetsStagePanel.svelte b/calliope-web/src/lib/components/workbench/AssetsStagePanel.svelte index 57c2a3d9..4591931a 100644 --- a/calliope-web/src/lib/components/workbench/AssetsStagePanel.svelte +++ b/calliope-web/src/lib/components/workbench/AssetsStagePanel.svelte @@ -1,4 +1,5 @@ {#if editing} @@ -89,11 +101,19 @@
- +
- - + +
{/each} @@ -124,7 +147,11 @@ {t('workbench.asset.add')}
- @@ -152,7 +184,9 @@ {#each assets as asset (asset.id)}
- {t(KIND_LABEL[asset.kind] ?? 'workbench.asset.kindProp')} + {t(KIND_LABEL[asset.kind] ?? 'workbench.asset.kindProp')} {asset.name} {#if asset.job} @@ -167,33 +201,54 @@ {/if} {/if}
- {#if asset.description}

{asset.description}

{/if} + {#if asset.description}

+ {asset.description} +

{/if} {#if asset.job?.status === 'done' && asset.job.output} - - {asset.name} + + {asset.name} {/if} - + {#if asset.negative_prompt} - + {/if}
{/each}
- {t('workbench.asset.summary', { n: assets.length })} + {t('workbench.asset.summary', { n: assets.length })}
{#if stage.status !== 'running'} - - @@ -420,5 +489,4 @@ background: var(--error-dim); color: var(--error); } - diff --git a/calliope-web/src/lib/components/workbench/ShotStagePanel.svelte b/calliope-web/src/lib/components/workbench/ShotStagePanel.svelte index 1baab054..f8678246 100644 --- a/calliope-web/src/lib/components/workbench/ShotStagePanel.svelte +++ b/calliope-web/src/lib/components/workbench/ShotStagePanel.svelte @@ -1,4 +1,5 @@ {#if editing} @@ -81,7 +92,11 @@ {#each draft as shot (shot.id)}
- +
- - + identity={`shot|${shot.id}|prompt`} + label="提示词" + />
{/each}
@@ -115,7 +133,11 @@ {t('workbench.vshot.add')}
{/each}
- {t('workbench.vshot.summary', { n: shots.length })} + {t('workbench.vshot.summary', { n: shots.length })}
{#if stage.status !== 'running'} - - @@ -394,5 +440,4 @@ background: var(--error-dim); color: var(--error); } - diff --git a/calliope-web/src/lib/components/workbench/StyleStagePanel.svelte b/calliope-web/src/lib/components/workbench/StyleStagePanel.svelte index 7335d78b..fcc1b64f 100644 --- a/calliope-web/src/lib/components/workbench/StyleStagePanel.svelte +++ b/calliope-web/src/lib/components/workbench/StyleStagePanel.svelte @@ -1,4 +1,5 @@ {#if editing} @@ -73,7 +85,11 @@
{i + 1} - +
{/each} @@ -107,13 +122,18 @@ {t('workbench.style.add')}
-
{:else} -
+
{#each directions as dir (dir.id)} {/each} @@ -159,11 +195,21 @@ {t('workbench.style.chooseHint')}
{#if stage.status !== 'running'} - - @@ -301,10 +347,6 @@ gap: 8px; } - .a-input.mono { - font-family: var(--font-a-mono); - font-size: 11.5px; - } .a-input.name { font-weight: 600; } @@ -330,5 +372,4 @@ background: var(--error-dim); color: var(--error); } - diff --git a/calliope-web/src/routes/+layout.svelte b/calliope-web/src/routes/+layout.svelte index cd91f504..498c54cb 100644 --- a/calliope-web/src/routes/+layout.svelte +++ b/calliope-web/src/routes/+layout.svelte @@ -1,4 +1,5 @@