diff --git a/docs/guides/user-guide.md b/docs/guides/user-guide.md index ac928883a..4cfaa89fd 100644 --- a/docs/guides/user-guide.md +++ b/docs/guides/user-guide.md @@ -140,6 +140,8 @@ Click a source number to view its full URL in a bordered information card, then Click **Chat with AI** at the bottom right, select a chat model, and describe what you want to create. You can upload, drag and drop, or paste assets, or type `@` to reference canvas content. The assistant can save text as requested and call configured generation models to create assets. +When the latest reply offers next steps, three to five suggested replies appear beneath it and stay visible as you type. Select one to fill the composer or replace the text you have already written. Attachments, references and skills stay in your draft. You can edit the text or undo the replacement. Press **Send** or `Enter` when you are ready. + Use **New conversation** at the top of the panel to start a separate chat. **Conversation history** lets you search and switch between this canvas's conversations. Switching saves your draft and restores the selected conversation's messages, reference attachments, and model choices; the active conversation also survives a page reload. Wait for uploads or replies to finish, or stop the reply, before switching. - Press `Enter` to send, or `Shift+Enter` for a new line. diff --git a/docs/guides/user-guide.zh-CN.md b/docs/guides/user-guide.zh-CN.md index 8b4a0f1ef..babd321a3 100644 --- a/docs/guides/user-guide.zh-CN.md +++ b/docs/guides/user-guide.zh-CN.md @@ -140,6 +140,8 @@ Codex 网页检索引用在具有匹配来源时显示为编号链接;复制 点击右下角“与 AI 对话”,选择对话模型后输入创作要求。可上传、拖放、粘贴素材,或输入 `@` 引用画布中的内容。助手可按要求保存文本,并调用已配置的生成模型制作素材。 +当助手在最新回复中给出后续建议时,回复下方会显示 3~5 个建议回复按钮,输入时仍可参考。点选一条即可填入输入框,替换已有文字,保留附件、引用和技能。你可以继续编辑或撤销替换,准备好后点击「发送」或按 `Enter`。 + 点击对话框顶部“新建”开始独立会话;点击“历史会话”可搜索并切换当前画布的旧会话。切换前会保存草稿,切回后恢复消息、参考附件和模型选择,刷新页面仍保留当前会话。回复或参考上传进行中,请先等待完成或停止回复后再切换。 - `Enter` 发送,`Shift+Enter` 换行。 diff --git a/packages/core/src/opendirector/agent/jiuwenswarm.py b/packages/core/src/opendirector/agent/jiuwenswarm.py index 359c571dd..8495b4934 100644 --- a/packages/core/src/opendirector/agent/jiuwenswarm.py +++ b/packages/core/src/opendirector/agent/jiuwenswarm.py @@ -13,7 +13,7 @@ from opendirector.agent.gateway import ToolGateway from opendirector.agent.skill_snapshot import prepare_skills, copy_skill_tree from opendirector.agent.directory_links import link_directory, unlink_directory -from opendirector.agent.tools import SYSTEM +from opendirector.agent.tools import SUGGESTED_REPLIES, SYSTEM from opendirector.agent import cli_transport from opendirector.agent.cli_discovery import cli_executable from opendirector.agent.process import terminate @@ -194,6 +194,7 @@ async def stream(self, run: RunContext, gateway: ToolGateway): 'Read source assets at their supplied absolute paths; do not move or modify them. ' 'Write intermediate files and outputs into the execution directory. ' 'Use opendirector --help to discover project commands and use opendirector for project changes.\n' + + SUGGESTED_REPLIES + '\n' + json.dumps(run.messages[-1:] if session_id else run.messages, ensure_ascii=False)), 'agent': {'name': 'opendirector', 'instructions': SYSTEM, 'tools': ['skill_tool', 'list_skill', 'read_file', 'bash']}, diff --git a/packages/core/src/opendirector/agent/native.py b/packages/core/src/opendirector/agent/native.py index 4611fc97e..a765bc60b 100644 --- a/packages/core/src/opendirector/agent/native.py +++ b/packages/core/src/opendirector/agent/native.py @@ -19,7 +19,7 @@ from opendirector.agent.citations import search_sources from opendirector.agent.gateway import ToolGateway from opendirector.agent.process import terminate -from opendirector.agent.tools import ROOT, SYSTEM +from opendirector.agent.tools import ROOT, SUGGESTED_REPLIES, SYSTEM from opendirector.installation import BUNDLED from opendirector.agent.skill_snapshot import prepare_skills from opendirector.agent.directory_links import link_directory, unlink_directory @@ -255,7 +255,8 @@ async def stream(self, run: RunContext, gateway: ToolGateway): 'Use inherited PATH to discover installed programs with Get-Command in PowerShell or command -v in POSIX shells. ' 'Read source assets at their supplied absolute paths; do not move or modify them. ' 'Write all intermediate files and outputs into the execution directory. ' - 'Use opendirector --help to discover OpenDirector commands and opendirector --help for usage. Use opendirector for project changes.') + 'Use opendirector --help to discover OpenDirector commands and opendirector --help for usage. Use opendirector for project changes.\n' + + SUGGESTED_REPLIES) prompt = execution_context + '\n' + json.dumps(context[-1:] if session_id else context, ensure_ascii=False) skill_instruction = '' if selected: diff --git a/packages/core/src/opendirector/agent/tools.py b/packages/core/src/opendirector/agent/tools.py index 82468bc1f..4753be1d8 100644 --- a/packages/core/src/opendirector/agent/tools.py +++ b/packages/core/src/opendirector/agent/tools.py @@ -78,6 +78,19 @@ def query_guidance(failure): identifiers and code unchanged. If a tool error is shown in another language, explain it in the user's language. ''' +SUGGESTED_REPLIES = ( + 'When there are useful next steps, finish the reply with 3 to 5 distinct suggestions the user ' + 'could send verbatim. Use single-line plain-text phrases in the language of the user, ' + 'with at most 80 characters per suggestion. ' + 'For example, offer ways to use an image after describing it or ways to develop a storyboard ' + 'after comparing directions. Let the context determine the count. If fewer than three useful ' + 'suggestions fit or you are reporting a fatal error, omit the suggestions. ' + 'Put the suggestions in their own top-level Markdown bullet list, after a prose paragraph ' + 'and a blank line. Use no inline formatting, links, checkboxes or extra commentary in the list. ' + 'After the list, leave a blank line and put the marker shown below on the final line, then end the reply.\n\n' + '[opendirector-replies]: #\n\n' +) + def tool_schema(name: str, description: str, parameters: dict) -> dict: assert description.strip(), f'Missing tool metadata: {name}' diff --git a/src/canvas/AssistantPanel.tsx b/src/canvas/AssistantPanel.tsx index ad1350f23..50d09fdeb 100644 --- a/src/canvas/AssistantPanel.tsx +++ b/src/canvas/AssistantPanel.tsx @@ -126,8 +126,6 @@ export function AssistantPanel({ const pending = pendingReferences.pending; const [checkingReferences, setCheckingReferences] = useState(false); const confirming = useRef(null); - const latest = useRef({ value, selectedNodes, open }); - latest.current = { value, selectedNodes, open }; // Keep the canvas defaulting new nodes to the model this dialog is using. const report = useRef(onModelSelections); report.current = onModelSelections; @@ -139,6 +137,11 @@ export function AssistantPanel({ chat.adding || checkingReferences || (chat.sending && !chat.started); + const latestReply = value.messages.at(-1); + const showSuggestions = open && !locked && !chat.changing && !chat.sending && + chat.canSend && !pending && latestReply?.status === "complete"; + const latest = useRef({ value, selectedNodes, open, showSuggestions }); + latest.current = { value, selectedNodes, open, showSuggestions }; const currentRuntime = chat.runtimes.find((item) => item.id === (value.runtime ?? "default")); const missingAgentModel = needsAgentModel(chat.loaded, currentRuntime, !!chat.agent); const referenceDisabled = @@ -319,7 +322,6 @@ export function AssistantPanel({ } const hoveredAsset = mediaPreview && displayedAssets.find(item => item.id === mediaPreview.assetId); - const latestReply = value.messages.at(-1); const latestUser = value.messages.at(-2); const latestTrace = latestReply?.role === "assistant" && latestUser?.role === "user" && new URLSearchParams({ @@ -438,7 +440,15 @@ export function AssistantPanel({ )}
{message.role === "assistant" ? ( - + { + if (!latest.current.showSuggestions || latest.current.value !== value) return; + editor.current?.replaceText(text); + } : undefined} + /> ) : message.role === "user" && (message.content || message.assets?.length) ? ( referenceContent( diff --git a/src/canvas/AssistantReply.tsx b/src/canvas/AssistantReply.tsx index 553a24b12..caa6b9602 100644 --- a/src/canvas/AssistantReply.tsx +++ b/src/canvas/AssistantReply.tsx @@ -10,6 +10,7 @@ import { activityProgress, replySections, toolLabel } from "./assistantActivity. import { assistantToolText } from "./assistantToolText.ts"; import { assistantCitationFormatter } from "./assistantCitations.ts"; import { defaultTranslate } from "./assistantTranslate.ts"; +import { hidePartialSuggestionMarker, parseSuggestedReplies } from "./suggestedReplies.ts"; const STATUS: Record = { running: "assistant.reply.status.running", @@ -94,10 +95,12 @@ function ToolGroup({ tools, t }: { tools: ChatTool[]; t: Translate }) { export const AssistantReply = memo(function AssistantReply({ message, thinking, + onSuggestion, t = defaultTranslate, }: { message: ChatMessage; thinking: boolean; + onSuggestion?: (text: string) => void; /** Injected by the panel; standalone mounts fall back to the active locale. */ t?: Translate; }) { @@ -128,9 +131,17 @@ export const AssistantReply = memo(function AssistantReply({ {formatText(text)} ); - const sections = replySections(message); + const sections = useMemo(() => replySections(message), [message]); + const lastPart = sections.reply.at(-1) ?? sections.activity.at(-1); + const tail = lastPart?.type === "text" && lastPart.id === "tail" ? lastPart.text : ""; + const suggestionBlock = useMemo(() => parseSuggestedReplies(tail), [tail]); + const parsed = suggestionBlock?.suggestions; + const suggestions = parsed && parsed.options.length >= 3 ? parsed : null; + const suggest = message.status === "complete" ? onSuggestion : undefined; + const copyTail = suggestions?.prefix || (suggestionBlock?.textWithoutMarker ?? hidePartialSuggestionMarker(tail)); + const tailText = suggestions && suggest ? suggestions.prefix : parsed === null ? tail : copyTail; const renderPart = (part: ReturnType["reply"][number]) => part.type === "text" - ? markdown(part.text, part.id) + ? markdown(part.id === "tail" ? tailText : part.text, part.id) : ; return ( <> @@ -155,6 +166,13 @@ export const AssistantReply = memo(function AssistantReply({
{sections.activity.map(renderPart)}
} {sections.reply.map(renderPart)} + {suggestions && suggest && ( +
+ {suggestions.options.map((text) => ( + + ))} +
+ )} {message.status === "streaming" && (
@@ -174,7 +192,8 @@ export const AssistantReply = memo(function AssistantReply({ title={copyLabel} onClick={async () => { try { - await navigator.clipboard.writeText(formatText(message.text)); + const plainReply = tail ? message.text.slice(0, -tail.length) + copyTail : message.text; + await navigator.clipboard.writeText(formatText(plainReply)); setCopyState("copied"); } catch { setCopyState("failed"); diff --git a/src/canvas/ChatReferenceEditor.tsx b/src/canvas/ChatReferenceEditor.tsx index 614104720..ea00a54e7 100644 --- a/src/canvas/ChatReferenceEditor.tsx +++ b/src/canvas/ChatReferenceEditor.tsx @@ -51,6 +51,7 @@ import type { MessageKey } from "../i18n/types.ts"; export interface ChatReferenceEditorHandle { focus: () => void; + replaceText: (text: string) => void; openPicker: () => void; setSkill: (name: string | null, fromSlash?: boolean) => void; caret: () => Caret; @@ -583,6 +584,17 @@ export function ChatReferenceEditor({ useImperativeHandle(ref, () => ({ focus, caret, + replaceText: (text) => { + if (disabled || composing.current || choosing.current) return; + const tokens = current.current.filter(part => part.type !== "text"); + const content: ReferenceContent = [...tokens, { type: "text", text: tokens.length ? "\n" + text : text }]; + const end = editingText(content).length; + const at = { start: end, end }; + if (!publish(content, at)) return; + close(); + focus(at); + updateMention(); + }, setSkill: (name, fromSlash = false) => { if (disabled || !skillsEnabled) return; const range = fromSlash ? slash.current : caret(); diff --git a/src/canvas/assistant-panel.css b/src/canvas/assistant-panel.css index aecc065b2..25b7d2a17 100644 --- a/src/canvas/assistant-panel.css +++ b/src/canvas/assistant-panel.css @@ -300,6 +300,34 @@ .cw-assistant-message-text { white-space: pre-wrap; } +.cw-chat-suggestions { + display: flex; + flex-wrap: wrap; + gap: 6px; + margin: 8px 0; +} +.cw-chat-suggestions button { + max-width: 100%; + padding: 5px 11px; + border: 1px solid var(--chat-border); + border-radius: 999px; + background: transparent; + color: var(--chat-text); + font: inherit; + font-size: 13px; + text-align: start; + white-space: normal; + overflow-wrap: anywhere; + cursor: pointer; +} +.cw-chat-suggestions button:hover, +.cw-chat-suggestions button:focus-visible { + background: var(--chat-hover); +} +.cw-chat-suggestions button:focus-visible { + outline: 1px solid currentColor; + outline-offset: 2px; +} .cw-assistant-message-status { display: block; margin-top: 8px; diff --git a/src/canvas/suggestedReplies.ts b/src/canvas/suggestedReplies.ts new file mode 100644 index 000000000..d8e630185 --- /dev/null +++ b/src/canvas/suggestedReplies.ts @@ -0,0 +1,64 @@ +import { fromMarkdown } from "mdast-util-from-markdown"; +import type { List } from "mdast"; + +const marker = "[opendirector-replies]: #"; + +function suggestionOptions(list: List) { + if (list.children.length < 3 || list.children.length > 5) return null; + const options: string[] = []; + const seen = new Set(); + for (const item of list.children) { + const paragraph = item.children[0]; + if ( + item.children.length !== 1 || + paragraph?.type !== "paragraph" || + paragraph.children.length !== 1 || + paragraph.position?.start.line !== paragraph.position?.end.line + ) return null; + const node = paragraph.children[0]; + if (node.type !== "text") return null; + const option = node.value.trim(); + if (!option || /^\[[ xX]\](?:\s|$)/.test(option) || /[\uE200-\uE202]/.test(option)) return null; + if (Array.from(option).length > 80) continue; + const normalized = option.replace(/\s+/g, " "); + if (seen.has(normalized)) return null; + seen.add(normalized); + options.push(option); + } + return options; +} + +export function parseSuggestedReplies(text: string) { + if (!text.trimEnd().endsWith(marker)) return null; + const { children } = fromMarkdown(text); + const definition = children.at(-1); + const markerStart = definition?.position?.start.offset; + if ( + definition?.type !== "definition" || + definition.identifier !== "opendirector-replies" || + definition.url !== "#" || + definition.title != null || + markerStart === undefined || + text.slice(markerStart).trimEnd() !== marker + ) return null; + + // The marker is recognized even when the preceding list cannot become buttons. + const list = children.at(-2); + const start = list?.position?.start.offset; + const options = list?.type === "list" ? suggestionOptions(list) : null; + return { + textWithoutMarker: text.slice(0, markerStart).trimEnd(), + suggestions: options && start !== undefined + ? { prefix: text.slice(0, start).trimEnd(), options } + : null, + }; +} + +/** A marker can arrive in multiple deltas, including after tool use. */ +export function hidePartialSuggestionMarker(text: string): string { + const lineStart = text.lastIndexOf("\n") + 1; + const tail = text.slice(lineStart).trimEnd(); + if (!tail || !marker.startsWith(tail)) return text; + const before = text.slice(0, lineStart); + return parseSuggestedReplies(before + marker)?.suggestions ? before.trimEnd() : text; +} diff --git a/src/i18n/messages/en-US/assistant.ts b/src/i18n/messages/en-US/assistant.ts index 3e19abda4..fe9eb643b 100644 --- a/src/i18n/messages/en-US/assistant.ts +++ b/src/i18n/messages/en-US/assistant.ts @@ -115,6 +115,8 @@ export const assistant = { "assistant.reply.plan": "Execution plan", "assistant.reply.empty": "This reply produced no content.", "assistant.reply.copy": "Copy", + "assistant.reply.suggestions": "Suggested replies", + "assistant.reply.useSuggestion": "Use this suggestion as the draft text", "assistant.reply.copyReply": "Copy reply", "assistant.reply.copied": "Copied", "assistant.reply.copyFailed": "Copy failed; select the text to copy it", diff --git a/src/i18n/messages/zh-CN/assistant.ts b/src/i18n/messages/zh-CN/assistant.ts index 4c95240b8..632900430 100644 --- a/src/i18n/messages/zh-CN/assistant.ts +++ b/src/i18n/messages/zh-CN/assistant.ts @@ -114,6 +114,8 @@ export const assistant = { "assistant.reply.plan": "执行计划", "assistant.reply.empty": "本次回复未生成内容。", "assistant.reply.copy": "复制", + "assistant.reply.suggestions": "建议回复", + "assistant.reply.useSuggestion": "用这条建议填写草稿", "assistant.reply.copyReply": "复制回复", "assistant.reply.copied": "已复制", "assistant.reply.copyFailed": "复制失败,请选择正文复制", diff --git a/tests/browser/assistant_activity.mjs b/tests/browser/assistant_activity.mjs index e8001fb99..d51eb4b35 100644 --- a/tests/browser/assistant_activity.mjs +++ b/tests/browser/assistant_activity.mjs @@ -73,6 +73,26 @@ try { await render(message([]), true); await page.getByText("Thinking…", { exact: true }).waitFor(); + const suggestionList = "\n\n- Expand the opening\n- Change the ending\n- Save the storyboard\n\n"; + const replyText = prefix + "Done." + suggestionList; + const suggestionCalls = [{ ...tools(1)[0], status: "complete" }]; + await render(message(suggestionCalls, replyText + "[opendirector-rep")); + const execution = page.locator(".cw-chat-execution-history"); + await execution.locator(":scope > summary").click(); + await execution.getByText("Expand the opening", { exact: true }).waitFor(); + assert.ok(!(await execution.innerText()).includes("opendirector-rep")); + const suggestedMessage = { + ...message(suggestionCalls, replyText + "[opendirector-replies]: #"), + status: "complete", + }; + await page.evaluate((value) => window.replyFixture.render(value, false, "en-US", true), suggestedMessage); + const suggestions = page.getByRole("group", { name: "Suggested replies", exact: true }); + await suggestions.getByRole("button", { name: "Save the storyboard" }).waitFor(); + assert.equal(await suggestions.getByRole("button").count(), 3); + await render(suggestedMessage); + await suggestions.waitFor({ state: "detached" }); + assert.equal(await page.getByText("Save the storyboard", { exact: true }).count(), 0); + // Real reply component, shared formatter and clipboard; no model calls. await page.evaluate(() => { Object.defineProperty(navigator, "clipboard", { configurable: true, value: { @@ -107,6 +127,75 @@ try { assert.doesNotMatch(copied, /[\uE200-\uE202]|turn\d/); assert.ok(copied.includes(sourceA.url) && copied.includes(sourceB.url)); + const listOnly = suggestionList.trim(); + const marker = "\n\n[opendirector-replies]: #"; + const copyReply = page.locator(".cw-chat-message-actions button"); + // A list-only reply stays readable and copyable when its buttons are hidden. + const listOnlyReply = { ...suggestedMessage, text: listOnly + marker, tools: [] }; + await page.evaluate((value) => window.replyFixture.render(value, false, "en-US", true), listOnlyReply); + await suggestions.getByRole("button", { name: "Save the storyboard", exact: true }).waitFor(); + await copyReply.click(); + await page.waitForFunction((text) => window.copiedReply === text, listOnly); + await render(listOnlyReply); + await suggestions.waitFor({ state: "detached" }); + const listItems = markdown.getByRole("listitem"); + await listItems.nth(2).waitFor(); + assert.deepEqual(await listItems.allTextContents(), ["Expand the opening", "Change the ending", "Save the storyboard"]); + await page.evaluate(() => { window.copiedReply = undefined; }); + await copyReply.click(); + await page.waitForFunction((text) => window.copiedReply === text, listOnly); + + // Fewer than three eligible options leaves the entire original list as text. + const overlong = "A".repeat(81); + const fallback = "Choose a direction.\n\n- Expand the opening\n- " + overlong + "\n- Save the storyboard"; + await page.evaluate((value) => window.replyFixture.render(value, false, "en-US", true), + { ...listOnlyReply, text: fallback + marker }); + await listItems.getByText(overlong, { exact: true }).waitFor(); + assert.equal(await suggestions.count(), 0); + assert.deepEqual(await listItems.allTextContents(), ["Expand the opening", overlong, "Save the storyboard"]); + await copyReply.click(); + await page.waitForFunction((text) => window.copiedReply === text, fallback); + + const malformedLists = [ + { body: "Compare the edits.", list: "- Widen the shot\n- Hold the cut", item: "Hold the cut" }, + { + body: "Choose a treatment.", + list: "- **Try a warmer grade**\n- Keep the contrast\n- Soften the highlights", + item: "Try a warmer grade", + }, + ]; + const copiedLists = []; + const expectedLists = []; + for (const { body, list, item } of malformedLists) { + const text = body + "\n\n" + list; + await page.evaluate((value) => window.replyFixture.render(value, false, "en-US", true), + { ...listOnlyReply, text: text + marker }); + await listItems.getByText(item, { exact: true }).waitFor(); + assert.equal(await suggestions.count(), 0); + await page.evaluate(() => { window.copiedReply = undefined; }); + await copyReply.click(); + await page.waitForFunction(() => typeof window.copiedReply === "string"); + copiedLists.push(await page.evaluate(() => window.copiedReply)); + expectedLists.push(text); + } + assert.deepEqual(copiedLists, expectedLists); + + await page.evaluate((value) => window.replyFixture.render(value, false, "en-US", true), suggestedMessage); + await suggestions.getByRole("button", { name: "Save the storyboard", exact: true }).waitFor(); + await page.evaluate(() => { window.copiedReply = undefined; }); + await copyReply.click(); + await page.waitForFunction(() => typeof window.copiedReply === "string"); + assert.equal(await page.evaluate(() => window.copiedReply), prefix + "Done."); + + const codeExample = "Markdown example:\n\n```md" + suggestionList + "[opendirector-replies]: #\n```"; + await render({ ...listOnlyReply, text: codeExample }); + await markdown.getByText("Markdown example:", { exact: true }).waitFor(); + assert.equal(await suggestions.count(), 0); + await page.evaluate(() => { window.copiedReply = undefined; }); + await copyReply.click(); + await page.waitForFunction(() => typeof window.copiedReply === "string"); + assert.equal(await page.evaluate(() => window.copiedReply), codeExample); + // A fresh mount represents history reopened from the server's saved message. await page.evaluate(async (value) => { window.replyFixture.unmount(); diff --git a/tests/browser/assistant_activity_fixture.tsx b/tests/browser/assistant_activity_fixture.tsx index 97ea34048..ab9512273 100644 --- a/tests/browser/assistant_activity_fixture.tsx +++ b/tests/browser/assistant_activity_fixture.tsx @@ -11,10 +11,10 @@ export function mountReply(element: HTMLElement) { setActiveLocale("en-US"); const root = createRoot(element); return { - render(message: ChatMessage, thinking = false, locale: Locale = "en-US") { + render(message: ChatMessage, thinking = false, locale: Locale = "en-US", interactive = false) { setActiveLocale(locale); const t = ((key, params) => translate(locale, key, params)) as Translate; - root.render(createElement(AssistantReply, { message, thinking, t })); + root.render(createElement(AssistantReply, { message, thinking, t, onSuggestion: interactive ? () => {} : undefined })); }, unmount() { root.unmount(); }, }; diff --git a/tests/browser/assistant_sessions.mjs b/tests/browser/assistant_sessions.mjs index 9f33e8ac8..8e14f790a 100644 --- a/tests/browser/assistant_sessions.mjs +++ b/tests/browser/assistant_sessions.mjs @@ -279,6 +279,122 @@ try { "New session model context must not contain the old story", ); + // Release the turn only after the full list has arrived in the stream. + const suggestions = panel.getByRole("group", { name: "建议回复", exact: true }); + const send = panel.getByRole("button", { name: "发送消息", exact: true }); + const choice = "展开海边重逢的完整分镜"; + const listItems = panel.locator(".cw-chat-markdown ul").filter({ hasText: choice }).getByRole("listitem"); + await api("/creation-test-assistant-gate", { open: false }); + await editor.fill("建议回复验收"); + await send.click(); + await listItems.nth(3).waitFor(); + assert.equal(await suggestions.count(), 0); + await api("/creation-test-assistant-gate", { open: true }); + const options = suggestions.getByRole("button"); + await options.last().waitFor(); + assert.equal(await options.count(), 3); + + const resize = panel.getByRole("separator", { name: "调整 AI 对话宽度" }); + const bounds = await resize.boundingBox(); + const x = bounds.x + bounds.width / 2; + const y = bounds.y + bounds.height / 2; + const rows = () => options.evaluateAll((buttons) => + new Set(buttons.map((button) => button.getBoundingClientRect().top)).size, + ); + await page.mouse.move(x, y); + await page.mouse.down(); + await page.mouse.move(x - 240, y, { steps: 5 }); + await page.mouse.up(); + const wideRows = await rows(); + const wideBounds = await resize.boundingBox(); + await page.mouse.move(wideBounds.x + wideBounds.width / 2, y); + await page.mouse.down(); + await page.mouse.move(x + 170, y, { steps: 5 }); + await page.mouse.up(); + assert.ok(await rows() > wideRows, "Suggested reply buttons should wrap as the panel narrows"); + await resize.press("End"); + + const choiceButton = suggestions.getByRole("button", { name: choice, exact: true }); + let turnRequests = 0; + page.on("request", (item) => { + if (item.method() === "POST" && item.url().endsWith("/assistant/turns")) turnRequests++; + }); + await editor.fill("我想自己回复"); + const typedDraft = await until(() => api(path), (value) => value.draft === "我想自己回复", "Save typed draft"); + assert.ok(await choiceButton.isVisible(), "Suggestions should remain visible while drafting"); + assert.equal(await choiceButton.getAttribute("title"), "用这条建议填写草稿"); + await choiceButton.click(); + const replacement = await until(() => api(path), (value) => value.draft === choice, "Save suggested text as a draft"); + assert.deepEqual(replacement.messages, typedDraft.messages); + assert.equal(await editorText(), choice); + assert.ok(await editor.evaluate((element) => element === document.activeElement)); + await editor.press("ControlOrMeta+z"); + assert.equal(await editorText(), "我想自己回复"); + await editor.fill(""); + const beforeChoice = await until(() => api(path), (value) => value.draft === "", "Clear previous draft"); + await choiceButton.click(); + assert.equal(await editorText(), choice, "Selecting a suggestion should fill the empty composer"); + const revised = choice + "!"; + await page.keyboard.insertText("!"); + const filled = await until(() => api(path), (value) => value.draft === revised, "Save the user's edit before sending"); + assert.deepEqual(filled.messages, beforeChoice.messages); + assert.equal(turnRequests, 0); + const request = page.waitForRequest((item) => + item.method() === "POST" && item.url().endsWith("/assistant/turns"), + ); + await send.click(); + const submitted = (await request).postDataJSON(); + assert.equal(submitted.text, revised); + assert.deepEqual(submitted.content, filled.draftContent); + assert.deepEqual(submitted.assets, []); + const afterChoice = await until( + () => api(path), + (value) => value.messages.length === beforeChoice.messages.length + 2 && + value.messages.at(-1)?.status === "complete", + "Manually sending a suggested reply must persist one ordinary user turn", + ); + assert.equal(afterChoice.messages.at(-2).role, "user"); + assert.equal(afterChoice.messages.at(-2).text, revised); + assert.equal(turnRequests, 1); + await suggestions.waitFor({ state: "detached" }); + await listItems.nth(3).waitFor(); + + // A reference-only draft is editable too. Keep the attachment through replacement, reload and Send. + await editor.fill("建议回复验收"); + await send.click(); + await choiceButton.waitFor(); + await panel.getByLabel("选择对话参考文件", { exact: true }).setInputFiles({ + name: "shot-notes.txt", mimeType: "text/plain", buffer: Buffer.from("Storyboard notes"), + }); + const attached = await until(() => api(path), (value) => value.assets.length === 1, "Save attachment"); + await choiceButton.click(); + await until(() => api(path), (value) => value.draft.includes(choice), "Save choice with attachment"); + await panel.getByRole("button", { name: "使用技能", exact: true }).click(); + await panel.getByRole("option", { name: /character-replacement/ }).click(); + const alternate = "换一个安静克制的结尾"; + await suggestions.getByRole("button", { name: alternate, exact: true }).click(); + const edited = await until(() => api(path), (value) => value.draft.includes(alternate), "Replace the draft again"); + assert.deepEqual(edited.messages, attached.messages); + assert.deepEqual(edited.assets, attached.assets); + assert.deepEqual(edited.draftContent.filter((part) => part.type === "reference"), + attached.draftContent.filter((part) => part.type === "reference")); + assert.deepEqual(edited.skills, ["character-replacement"]); + assert.deepEqual(edited.draftContent.filter((part) => part.type === "text").map((part) => part.text.trim()), [alternate]); + await page.reload(); + await page.getByText("已保存到本机", { exact: true }).waitFor(); + await show(); + await until(editorText, (text) => text.includes(alternate) && text.includes("shot-notes.txt") && + text.includes("/character-replacement"), "Restore edited draft"); + const manualRequest = page.waitForRequest((item) => item.method() === "POST" && item.url().endsWith("/assistant/turns")); + await send.click(); + const manual = (await manualRequest).postDataJSON(); + assert.equal(manual.text, edited.draft); + assert.deepEqual(manual.content, edited.draftContent); + assert.deepEqual(manual.skills, edited.skills); + assert.deepEqual(manual.assets, edited.assets.map((asset) => asset.id)); + await until(() => api(path), (value) => value.messages.length === edited.messages.length + 2 && + value.messages.at(-1)?.status === "complete", "Complete the manually submitted turn"); + // History stays inside a narrow assistant, closes outside, and all header controls fit. await panel.evaluate((element) => element.style.setProperty("--assistant-width", "300px"), diff --git a/tests/browser/creation_server.py b/tests/browser/creation_server.py index a0b78c9c1..4a8e950b5 100644 --- a/tests/browser/creation_server.py +++ b/tests/browser/creation_server.py @@ -158,6 +158,16 @@ async def generate(self, config, task, submitted, *, result_ready=None): async def fixture_agent(self, run): assistant_contexts.append(run.messages) + if run.messages[-1]['content'].partition('\n')[0] == '建议回复验收': + yield {'type': 'delta', 'text': ( + '- 展开海边重逢的完整分镜\n' + '- 换一个安静克制的结尾\n' + '- 补一版黄昏海边的视觉参考\n' + f'- {"补充整份分镜的镜头和对白" * 8}\n\n' + '[opendirector-replies]: #' + )} + await assistant_gate.wait() + return yield {'type': 'delta', 'text': '已收到创作要求。'} await assistant_gate.wait() yield {'type': 'delta', 'text': '这是隔离测试回复。'} diff --git a/tests/suggestedReplies.test.ts b/tests/suggestedReplies.test.ts new file mode 100644 index 000000000..aacec7878 --- /dev/null +++ b/tests/suggestedReplies.test.ts @@ -0,0 +1,74 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { hidePartialSuggestionMarker, parseSuggestedReplies } from "../src/canvas/suggestedReplies.ts"; + +const marker = "[opendirector-replies]: #"; +const body = "有几个方向可以继续。"; +const options = ["扩写 A", "扩写 B", "试试混合版", "调整光线", "保存脚本"]; +const list = (items: string[]) => items.map((item) => "- " + item).join("\n"); +const marked = (items: string[]) => list(items) + "\n\n" + marker; + +test("reads 3 to 5 suggestions after the reply, or without a prefix", () => { + for (const count of [3, 4, 5]) { + const selected = options.slice(0, count); + const text = body + "\n\n" + list(selected); + assert.deepEqual(parseSuggestedReplies(text + "\n\n" + marker), { + textWithoutMarker: text, + suggestions: { prefix: body, options: selected }, + }); + } + assert.deepEqual(parseSuggestedReplies(marked(options)), { + textWithoutMarker: list(options), + suggestions: { prefix: "", options }, + }); +}); + +test("filters each overlong suggestion and keeps the original Markdown for fallback", () => { + const atLimit = "🌊".repeat(80); + const overLimit = atLimit + "🌊"; + const items = [atLimit, overLimit, options[0], options[1]]; + assert.deepEqual(parseSuggestedReplies(marked(items)), { + textWithoutMarker: list(items), + suggestions: { prefix: "", options: [atLimit, options[0], options[1]] }, + }); + const shortList = items.slice(0, 3); + assert.deepEqual(parseSuggestedReplies(marked(shortList)), { + textWithoutMarker: list(shortList), + suggestions: { prefix: "", options: [atLimit, options[0]] }, + }); +}); + +test("removes the final protocol definition while preserving invalid lists", () => { + for (const markdown of [ + list(options.slice(0, 2)), + list([...options, "第六项"]), + list([options[0], options[0], options[1]]), + list(["**加粗**", options[0], options[1]]), + list(options.slice(0, 2)) + "\n\n[other-reference]: /guide", + ]) { + const text = body + "\n\n" + markdown; + assert.deepEqual(parseSuggestedReplies(text + "\n\n" + marker), { + textWithoutMarker: text, + suggestions: null, + }); + } +}); + +test("ordinary Markdown and literal protocol examples keep their text", () => { + for (const text of [ + list(options), + list(options) + "\n\n[other-reference]: #", + marked(options) + "\n\n后续正文", + '```md\n' + marked(options) + '\n```', + '```md\n' + marked(options), + ]) { + assert.equal(parseSuggestedReplies(text), null, text); + } +}); + +test("hides a streamed marker after a suggestion list and keeps code examples", () => { + const text = body + "\n\n" + list(options); + assert.equal(hidePartialSuggestionMarker(text + "\n\n[opendirector-rep"), text); + const code = '```md\n' + list(options) + '\n\n[opendirector-rep'; + assert.equal(hidePartialSuggestionMarker(code), code); +}); diff --git a/tests/test_deepseek_runtime.py b/tests/test_deepseek_runtime.py index ae280c6a1..33559b26c 100644 --- a/tests/test_deepseek_runtime.py +++ b/tests/test_deepseek_runtime.py @@ -62,6 +62,10 @@ def read(): return json.loads(sys.stdin.readline()) elif req['method']=='session/prompt': assert Path(os.environ['PWD']).samefile(Path.cwd()) blocks=[block['text'] for block in req['params']['prompt']] + execution_context = blocks[0 if resumed else 1] + assert '3 to 5 distinct suggestions' in execution_context, 'missing suggested reply count' + assert 'at most 80 characters per suggestion' in execution_context, 'missing suggested reply length limit' + assert '[opendirector-replies]: #' in execution_context.splitlines(), 'missing suggested reply marker' if resumed: assert blocks[0].startswith('Execution directory:') assert not any('Conversation history' in block or 'You are OpenDirector' in block for block in blocks) diff --git a/tests/test_jiuwenswarm_runtime.py b/tests/test_jiuwenswarm_runtime.py index a1cf4f3a6..856cf6cdb 100644 --- a/tests/test_jiuwenswarm_runtime.py +++ b/tests/test_jiuwenswarm_runtime.py @@ -50,13 +50,17 @@ def emit(kind, **fields): assert 'skill_tool' in request['agent']['tools'] assert request['mcp'] == [] assert request['skills'] == ['example'] +prompt = request['input'] +assert '3 to 5 distinct suggestions' in prompt, 'missing suggested reply count' +assert 'at most 80 characters per suggestion' in prompt, 'missing suggested reply length limit' +assert '[opendirector-replies]: #' in prompt.splitlines(), 'missing suggested reply marker' sid='native-session' -if 'Continue plan' in request['input']: - assert request['session_id']==sid and 'Save plan' not in request['input'] +if 'Continue plan' in prompt: + assert request['session_id']==sid and 'Save plan' not in prompt else: assert 'session_id' not in request workspace_marker = Path('.continuation-test') -if 'Continue plan' in request['input']: +if 'Continue plan' in prompt: assert workspace_marker.read_text() == str(Path.cwd()) else: workspace_marker.write_text(str(Path.cwd())) diff --git a/tests/test_native_runtime.py b/tests/test_native_runtime.py index ce771890c..1151cd76d 100644 --- a/tests/test_native_runtime.py +++ b/tests/test_native_runtime.py @@ -35,6 +35,9 @@ FAKE = '''import json, os, subprocess, sys from pathlib import Path prompt = sys.stdin.read() +assert '3 to 5 distinct suggestions' in prompt, 'missing suggested reply count' +assert 'at most 80 characters per suggestion' in prompt, 'missing suggested reply length limit' +assert '[opendirector-replies]: #' in prompt.splitlines(), 'missing suggested reply marker' assert 'Segment plan' not in prompt if 'Continue plan' in prompt: assert ('Save a plan' in prompt) == (os.environ.get('TEST_MISSING_ROLLOUT') == '1')