Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/guides/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions docs/guides/user-guide.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ Codex 网页检索引用在具有匹配来源时显示为编号链接;复制

点击右下角“与 AI 对话”,选择对话模型后输入创作要求。可上传、拖放、粘贴素材,或输入 `@` 引用画布中的内容。助手可按要求保存文本,并调用已配置的生成模型制作素材。

当助手在最新回复中给出后续建议时,回复下方会显示 3~5 个建议回复按钮,输入时仍可参考。点选一条即可填入输入框,替换已有文字,保留附件、引用和技能。你可以继续编辑或撤销替换,准备好后点击「发送」或按 `Enter`。

点击对话框顶部“新建”开始独立会话;点击“历史会话”可搜索并切换当前画布的旧会话。切换前会保存草稿,切回后恢复消息、参考附件和模型选择,刷新页面仍保留当前会话。回复或参考上传进行中,请先等待完成或停止回复后再切换。

- `Enter` 发送,`Shift+Enter` 换行。
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/opendirector/agent/jiuwenswarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']},
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/opendirector/agent/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <group> <command> --help for usage. Use opendirector for project changes.')
'Use opendirector --help to discover OpenDirector commands and opendirector <group> <command> --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:
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/opendirector/agent/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Expand Down
18 changes: 14 additions & 4 deletions src/canvas/AssistantPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ export function AssistantPanel({
const pending = pendingReferences.pending;
const [checkingReferences, setCheckingReferences] = useState(false);
const confirming = useRef<AbortController | null>(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;
Expand All @@ -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 =
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -438,7 +440,15 @@ export function AssistantPanel({
)}
<div className="cw-assistant-message-text">
{message.role === "assistant" ? (
<AssistantReply message={message} thinking={chat.thinkingNow} t={t} />
<AssistantReply
message={message}
thinking={chat.thinkingNow}
t={t}
onSuggestion={showSuggestions && message === latestReply ? (text) => {
if (!latest.current.showSuggestions || latest.current.value !== value) return;
editor.current?.replaceText(text);
} : undefined}
/>
) : message.role === "user" &&
(message.content || message.assets?.length) ? (
referenceContent(
Expand Down
25 changes: 22 additions & 3 deletions src/canvas/AssistantReply.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChatTool["status"], MessageKey> = {
running: "assistant.reply.status.running",
Expand Down Expand Up @@ -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;
}) {
Expand Down Expand Up @@ -128,9 +131,17 @@ export const AssistantReply = memo(function AssistantReply({
{formatText(text)}
</Streamdown>
);
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<typeof replySections>["reply"][number]) => part.type === "text"
? markdown(part.text, part.id)
? markdown(part.id === "tail" ? tailText : part.text, part.id)
: <ToolGroup key={part.id} tools={part.tools} t={t} />;
return (
<>
Expand All @@ -155,6 +166,13 @@ export const AssistantReply = memo(function AssistantReply({
<div className="cw-chat-activity-detail">{sections.activity.map(renderPart)}</div>
</details>}
{sections.reply.map(renderPart)}
{suggestions && suggest && (
<div className="cw-chat-suggestions" role="group" aria-label={t("assistant.reply.suggestions")}>
{suggestions.options.map((text) => (
<button key={text} type="button" title={t("assistant.reply.useSuggestion")} onClick={() => suggest(text)}>{text}</button>
))}
</div>
)}
{message.status === "streaming" && (
<div className="cw-chat-progress" role="status" aria-live="polite">
<span className="cw-chat-waiting" aria-hidden="true"><i /><i /><i /></span>
Expand All @@ -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");
Expand Down
12 changes: 12 additions & 0 deletions src/canvas/ChatReferenceEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
28 changes: 28 additions & 0 deletions src/canvas/assistant-panel.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
64 changes: 64 additions & 0 deletions src/canvas/suggestedReplies.ts
Original file line number Diff line number Diff line change
@@ -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<string>();
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;
}
2 changes: 2 additions & 0 deletions src/i18n/messages/en-US/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/messages/zh-CN/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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": "复制失败,请选择正文复制",
Expand Down
Loading
Loading