From d867e3f9b644b364bc97667470b8eda6aff715ef Mon Sep 17 00:00:00 2001 From: Xingkai98 Date: Mon, 16 Nov 2026 13:17:01 +0800 Subject: [PATCH 01/10] feat(assistant): resolve chat links to stable canvas identities (#333) Add a shared backend resolver for assistant Markdown links. Relative destinations are matched through the turn's asset provenance and the canvas items that display the asset; explicit canvas/node/project links are also supported. Results are typed (focused, hidden, materialize, ambiguous, missing, cross-project, external) and store only stable IDs so moving, renaming or regrouping cannot invalidate them. Resolved links are persisted as canvasLinks on the assistant message at turn end. Link resolution never fails an accepted turn, and external links keep the existing policy. Add bilingual outcome copy and tests for resolution, ambiguity, code fences, persistence and mutation coverage. Co-Authored-By: Claude Code --- apps/api/src/opendirector_api/agent.py | 14 +- .../src/opendirector/agent/canvas_links.py | 352 ++++++++++++++++++ src/backend/chat.ts | 27 ++ src/i18n/messages/en-US/assistant.ts | 6 + src/i18n/messages/zh-CN/assistant.ts | 6 + tests/canvasLinkMessages.test.ts | 39 ++ tests/test_canvas_links.py | 274 ++++++++++++++ 7 files changed, 717 insertions(+), 1 deletion(-) create mode 100644 packages/core/src/opendirector/agent/canvas_links.py create mode 100644 tests/canvasLinkMessages.test.ts create mode 100644 tests/test_canvas_links.py diff --git a/apps/api/src/opendirector_api/agent.py b/apps/api/src/opendirector_api/agent.py index 7ef36a60b..1dd5204e7 100644 --- a/apps/api/src/opendirector_api/agent.py +++ b/apps/api/src/opendirector_api/agent.py @@ -13,6 +13,7 @@ from pydantic import BaseModel, ConfigDict, Field, JsonValue from opendirector.agent.catalog import model_choices +from opendirector.agent.canvas_links import canvas_links_for_text from opendirector.agent.context import RunContext, RuntimeId from opendirector.agent.citations import referenced_sources, with_trace_sources from opendirector.agent.runtime import AgentRuntimes, runtime_available, configured_runtimes, runtime_spec, runtime_catalog, runtime_status @@ -456,12 +457,23 @@ async def produce() -> None: for activity in activities: if activity['status'] == 'running': activity['status'] = 'interrupted' + canvas_links = [] + try: + final_snapshot = await asyncio.to_thread(projects.snapshot, project_id) + canvas_links = canvas_links_for_text(text, final_snapshot, value.id) + except Exception: + # Link resolution is navigation metadata; it must not + # fail a turn whose accepted result is already saved. + logger.exception('agent.canvas_links.failed project_id=%s turn_id=%s', + project_id, value.id) saved = await asyncio.to_thread(conversations.update, project_id, finish={ 'id': str(uuid5(value.id, 'assistant')), 'role': 'assistant', 'text': text, 'status': status, 'model': model, 'runtime': value.runtime, 'thinking': value.thinking, 'tools': activities, 'sources': list(sources.values()), - **({'plan': plan} if plan is not None else {})}, conversation_id=value.conversationId) + **({'plan': plan} if plan is not None else {}), + **({'canvasLinks': canvas_links} if canvas_links else {})}, + conversation_id=value.conversationId) else: saved = None events.put({'type': 'end', 'conversation': saved, 'error': error}) diff --git a/packages/core/src/opendirector/agent/canvas_links.py b/packages/core/src/opendirector/agent/canvas_links.py new file mode 100644 index 000000000..1f71289e1 --- /dev/null +++ b/packages/core/src/opendirector/agent/canvas_links.py @@ -0,0 +1,352 @@ +"""Resolve assistant Markdown links to stable canvas item identities. + +An assistant reply may link to canvas content with a relative destination such +as ``[Finished video](output.mp4)``. The file name is matched against project +assets and their recorded provenance, then mapped to the canvas items that +display the asset. The result stores only stable object identities and typed +status codes; current geometry is deliberately not stored, so moving, renaming +or regrouping an item never invalidates a saved link. + +Only inline Markdown links are considered. Fenced code blocks, inline code +spans, images and ordinary external URLs are left untouched. +""" +from __future__ import annotations + +import re +from typing import Any, Mapping +from urllib.parse import unquote, urlsplit +from uuid import UUID + +LINK_STATUSES = ('focused', 'hidden', 'materialize', 'ambiguous', 'missing', + 'cross-project', 'external') + +_INLINE_CODE = re.compile(r'(`+)(?:(?!\1).)*?\1', re.DOTALL) +_FENCE = re.compile(r'^ {0,3}(`{3,}|~{3,})') + + +def _ignored_ranges(text: str) -> list[tuple[int, int]]: + """Fenced blocks and code spans are examples, never navigable links.""" + ranges: list[tuple[int, int]] = [] + fence = None + fence_start = 0 + offset = 0 + for line in text.splitlines(keepends=True): + match = _FENCE.match(line) + if fence is None: + if match is not None: + fence = match.group(1)[0] + fence_start = offset + elif match is not None and match.group(1)[0] == fence: + ranges.append((fence_start, offset + len(line))) + fence = None + offset += len(line) + if fence is not None: + ranges.append((fence_start, len(text))) + for match in _INLINE_CODE.finditer(text): + if not any(start <= match.start() < end for start, end in ranges): + ranges.append((match.start(), match.end())) + return sorted(ranges) + + +def _inside(position: int, ranges: list[tuple[int, int]]) -> bool: + return any(start <= position < end for start, end in ranges) + + +def _matching_bracket(text: str, start: int) -> int | None: + depth = 0 + index = start + while index < len(text): + char = text[index] + if char == '\\': + index += 2 + continue + if char == '[': + depth += 1 + elif char == ']': + depth -= 1 + if depth == 0: + return index + index += 1 + return None + + +def _destination(text: str, start: int) -> tuple[str | None, int]: + """Return the link destination and the offset just after the closing paren.""" + length = len(text) + index = start + while index < length and text[index] in ' \t\r\n': + index += 1 + if index < length and text[index] == '<': + end = text.find('>', index + 1) + if end == -1: + return None, length + close = text.find(')', end + 1) + return text[index + 1:end], (close + 1 if close != -1 else length) + depth = 0 + cursor = index + while cursor < length: + char = text[cursor] + if char == '\\': + cursor += 2 + continue + if char == '(': + depth += 1 + elif char == ')': + if depth == 0: + return text[index:cursor], cursor + 1 + depth -= 1 + elif char in ' \t\r\n' and depth == 0: + close = text.find(')', cursor) + return text[index:cursor], (close + 1 if close != -1 else length) + cursor += 1 + return None, length + + +def link_targets(text: str) -> list[str]: + """Extract inline Markdown link destinations outside code and images.""" + ignored = _ignored_ranges(text) + targets: list[str] = [] + index = 0 + length = len(text) + while index < length: + if _inside(index, ignored): + index = next((end for start, end in ignored if start <= index < end), length) + continue + if text[index] == '\\': + index += 2 + continue + if text[index] == '!' and index + 1 < length and text[index + 1] == '[': + index += 2 + continue + if text[index] == '[': + close = _matching_bracket(text, index) + if close is not None and close + 1 < length and text[close + 1] == '(': + destination, end = _destination(text, close + 2) + if destination is not None: + targets.append(destination) + index = max(end, close + 2) + continue + index += 1 + return targets + + +def _uuid(value: str) -> str | None: + try: + return str(UUID(str(value).strip())) + except (ValueError, TypeError, AttributeError): + return None + + +def _identity(target: str) -> tuple[str, Any]: + """Classify a destination without touching project state or raising.""" + raw = target.strip() + if raw.startswith('<') and raw.endswith('>'): + raw = raw[1:-1].strip() + if raw.startswith(('#', '?')): + # In-page anchors and query-only destinations are not project objects. + return 'external', raw + if raw.startswith('project:'): + project, separator, remainder = raw[len('project:'):].partition('/') + if not separator: + return 'missing', raw + for prefix in ('canvas:', 'node:'): + if remainder.startswith(prefix): + project_id = _uuid(project) + node_id = _uuid(remainder[len(prefix):]) + if project_id is None or node_id is None: + return 'missing', raw + return 'project', (project_id, node_id) + return 'missing', raw + for prefix in ('canvas:', 'node:'): + if raw.startswith(prefix): + node_id = _uuid(raw[len(prefix):]) + return ('node', node_id) if node_id is not None else ('missing', raw) + try: + parsed = urlsplit(raw) + except ValueError: + return 'missing', raw + if parsed.scheme or parsed.netloc: + # http(s) links keep the existing external-link policy; unsafe schemes + # (javascript:, data:, file:) are never treated as project links. + return 'external', raw + path = unquote(parsed.path).replace('\\', '/') + while path.startswith('./'): + path = path[2:] + path = path.lstrip('/') + if not path or path in {'.', '..'} or path.endswith('/'): + return 'missing', raw + name = path.rsplit('/', 1)[-1] + if not name or name in {'.', '..'}: + return 'missing', raw + return 'name', name + + +def format_canvas_link(node_id: str, project_id: str | None = None) -> str: + """Canonical forms consumed by the resolver and future tool guidance.""" + node = _uuid(node_id) + if node is None: + raise ValueError('Canvas item id must be a UUID') + if project_id is None: + return f'canvas:{node}' + project = _uuid(project_id) + if project is None: + raise ValueError('Project id must be a UUID') + return f'project:{project}/canvas:{node}' + + +def _items(snapshot: Any) -> tuple[Any, ...]: + return tuple(getattr(snapshot, 'canvasItems', ()) or ()) + + +def _assets(snapshot: Any) -> tuple[Any, ...]: + return tuple(getattr(snapshot, 'assets', ()) or ()) + + +def _project_id(snapshot: Any) -> str: + project = getattr(snapshot, 'project', None) + return str(getattr(project, 'id', '')) + + +def _base_name(value: str) -> str: + return str(value or '').replace('\\', '/').rsplit('/', 1)[-1] + + +def _asset_matches(name: str, asset: Any) -> bool: + return name in {_base_name(getattr(asset, 'name', '')), + _base_name(getattr(asset, 'path', ''))} + + +def _binding_asset_ids(item: Any) -> list[str]: + binding = getattr(item, 'binding', None) + role = getattr(binding, 'role', None) + reference = (getattr(binding, 'objectRef', None) if role == 'material' + else getattr(binding, 'materialOverride', None) if role == 'generation' + else None) + asset_id = getattr(reference, 'assetId', None) + return [str(asset_id)] if asset_id is not None else [] + + +def _run_id(asset: Any) -> str: + provenance = getattr(asset, 'provenance', None) + return str(getattr(provenance, 'agentRunId', '') or '') + + +def _node_result(item: Any, result: dict) -> dict: + visible = bool(getattr(item, 'visible', True)) + return {**result, + 'status': 'focused' if visible else 'hidden', + 'nodeId': str(getattr(item, 'id', '')), + 'kind': str(getattr(item, 'kind', '')), + **({} if visible else {'action': 'reveal'})} + + +def _node_candidate(item: Any) -> dict: + visible = bool(getattr(item, 'visible', True)) + return {'nodeId': str(getattr(item, 'id', '')), + 'kind': str(getattr(item, 'kind', '')), + 'title': str(getattr(item, 'title', '')), + 'hidden': not visible} + + +def _asset_candidate(asset: Any) -> dict: + return {'assetId': str(getattr(asset, 'id', '')), + 'kind': str(getattr(asset, 'kind', '')), + 'title': str(getattr(asset, 'name', ''))} + + +def _unique(items: list[Any]) -> list[Any]: + seen: set[str] = set() + result = [] + for item in items: + identity = str(getattr(item, 'id', '')) + if identity not in seen: + seen.add(identity) + result.append(item) + return result + + +def _resolve_name(name: str, snapshot: Any, turn_id: str | None, result: dict) -> dict: + assets = [asset for asset in _assets(snapshot) if _asset_matches(name, asset)] + if assets and turn_id is not None: + # The turn's own outputs take precedence over an unrelated asset that + # happens to share a file name. A filename alone never picks silently. + current = [asset for asset in assets if _run_id(asset) == str(turn_id)] + if current: + assets = current + if assets: + asset_ids = {str(getattr(asset, 'id', '')) for asset in assets} + nodes = _unique([item for item in _items(snapshot) + if asset_ids.intersection(_binding_asset_ids(item))]) + if len(assets) == 1 and len(nodes) == 1: + return _node_result(nodes[0], result) + if len(assets) == 1 and not nodes: + asset = assets[0] + return {**result, 'status': 'materialize', + 'assetId': str(getattr(asset, 'id', '')), + 'kind': str(getattr(asset, 'kind', '')), + 'action': 'materialize'} + return {**result, 'status': 'ambiguous', + 'candidates': [*map(_asset_candidate, assets), *map(_node_candidate, nodes)]} + nodes = _unique([item for item in _items(snapshot) + if str(getattr(item, 'title', '')) == name]) + if len(nodes) == 1: + return _node_result(nodes[0], result) + if len(nodes) > 1: + return {**result, 'status': 'ambiguous', + 'candidates': [_node_candidate(item) for item in nodes]} + return {**result, 'status': 'missing'} + + +def resolve_canvas_link(target: str, snapshot: Any, turn_id: str | None = None) -> dict: + """Resolve one destination to a typed, JSON-serializable outcome.""" + raw = str(target) + result = {'target': raw} + try: + kind, value = _identity(raw) + if kind == 'external': + return {**result, 'status': 'external'} + if kind == 'missing': + return {**result, 'status': 'missing'} + if kind == 'project': + project_id, node_id = value + if project_id != _project_id(snapshot): + return {**result, 'status': 'cross-project', + 'projectId': project_id, 'nodeId': node_id} + item = next((item for item in _items(snapshot) + if str(getattr(item, 'id', '')) == node_id), None) + return _node_result(item, result) if item is not None else {**result, 'status': 'missing'} + if kind == 'node': + item = next((item for item in _items(snapshot) + if str(getattr(item, 'id', '')) == value), None) + return _node_result(item, result) if item is not None else {**result, 'status': 'missing'} + return _resolve_name(value, snapshot, turn_id, result) + except Exception: + # A malformed project fact or an unexpected snapshot shape must not + # break an assistant reply; the link simply stays unresolved. + return {**result, 'status': 'missing'} + + +def canvas_links_for_text(text: str, snapshot: Any, turn_id: str | None = None) -> list[dict]: + """Resolve each unique inline link, omitting ordinary external URLs.""" + result: list[dict] = [] + seen: set[str] = set() + for target in link_targets(text): + if target in seen: + continue + seen.add(target) + resolved = resolve_canvas_link(target, snapshot, turn_id) + if resolved['status'] != 'external': + result.append(resolved) + return result + + +def with_canvas_links(message: Mapping[str, Any], snapshot: Any, + turn_id: str | None = None) -> dict: + """Attach resolved references to an assistant message before persistence.""" + value = dict(message) + if value.get('role') != 'assistant': + return value + links = canvas_links_for_text(str(value.get('text', '')), snapshot, turn_id) + if links: + value['canvasLinks'] = links + return value diff --git a/src/backend/chat.ts b/src/backend/chat.ts index f935df4c8..e767b03eb 100644 --- a/src/backend/chat.ts +++ b/src/backend/chat.ts @@ -60,7 +60,34 @@ export interface ChatSource { url: string; title: string; } +/** A resolved canvas reference persisted with an assistant reply. */ +export type ChatCanvasLinkStatus = + | "focused" + | "hidden" + | "materialize" + | "ambiguous" + | "missing" + | "cross-project" + | "external"; +export interface ChatCanvasLinkCandidate { + nodeId?: string; + assetId?: string; + kind?: string; + title?: string; + hidden?: boolean; +} +export interface ChatCanvasLink { + target: string; + status: ChatCanvasLinkStatus; + nodeId?: string; + assetId?: string; + projectId?: string; + kind?: string; + action?: "reveal" | "materialize"; + candidates?: ChatCanvasLinkCandidate[]; +} export interface ChatMessage { + canvasLinks?: ChatCanvasLink[]; sources?: ChatSource[]; plan?: ChatPlanEntry[]; runtime?: AgentRuntime; diff --git a/src/i18n/messages/en-US/assistant.ts b/src/i18n/messages/en-US/assistant.ts index 3e19abda4..1bcc93f85 100644 --- a/src/i18n/messages/en-US/assistant.ts +++ b/src/i18n/messages/en-US/assistant.ts @@ -1,6 +1,12 @@ /** English assistant copy. Keys must match `zh-CN/assistant.ts`. */ export const assistant = { "assistant.reply.sourceUnavailable": "Source unavailable", + "assistant.reply.canvasLink.open": "Go to canvas item", + "assistant.reply.canvasLink.hidden": "Show hidden item", + "assistant.reply.canvasLink.materialize": "Show on canvas", + "assistant.reply.canvasLink.ambiguous": "This link matches several canvas items. Choose one.", + "assistant.reply.canvasLink.missing": "This linked item is not in the project.", + "assistant.reply.canvasLink.crossProject": "This link points to another project.", "assistant.reply.copyLink": "Copy link", "assistant.reply.copyLinkFailed": "Copy failed; select the URL to copy it", "assistant.reply.openLink": "Open link", diff --git a/src/i18n/messages/zh-CN/assistant.ts b/src/i18n/messages/zh-CN/assistant.ts index 4c95240b8..eed07fb90 100644 --- a/src/i18n/messages/zh-CN/assistant.ts +++ b/src/i18n/messages/zh-CN/assistant.ts @@ -1,6 +1,12 @@ /** Assistant panel, reply activity, pickers, references and chat transport. */ export const assistant = { "assistant.reply.sourceUnavailable": "来源不可用", + "assistant.reply.canvasLink.open": "前往画布对象", + "assistant.reply.canvasLink.hidden": "显示隐藏对象", + "assistant.reply.canvasLink.materialize": "在画布中显示", + "assistant.reply.canvasLink.ambiguous": "该链接匹配多个画布对象,请选择。", + "assistant.reply.canvasLink.missing": "链接的对象不在当前工程中。", + "assistant.reply.canvasLink.crossProject": "该链接指向另一个工程。", "assistant.reply.copyLink": "复制链接", "assistant.reply.copyLinkFailed": "复制失败,请选择链接地址复制", "assistant.reply.openLink": "打开链接", diff --git a/tests/canvasLinkMessages.test.ts b/tests/canvasLinkMessages.test.ts new file mode 100644 index 000000000..63ecce492 --- /dev/null +++ b/tests/canvasLinkMessages.test.ts @@ -0,0 +1,39 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { translate } from "../src/i18n/messages.ts"; +import { zhCN } from "../src/i18n/messages/zh-CN/index.ts"; +import { enUS } from "../src/i18n/messages/en-US/index.ts"; + +const keys = [ + "assistant.reply.canvasLink.open", + "assistant.reply.canvasLink.hidden", + "assistant.reply.canvasLink.materialize", + "assistant.reply.canvasLink.ambiguous", + "assistant.reply.canvasLink.missing", + "assistant.reply.canvasLink.crossProject", +] as const; + +test("canvas link messages exist in both catalogs", () => { + for (const key of keys) { + assert.ok(key in zhCN, `missing zh-CN key ${key}`); + assert.ok(key in enUS, `missing en-US key ${key}`); + } +}); + +test("canvas link messages resolve in both locales without leaking the other language", () => { + for (const key of keys) { + const zh = translate("zh-CN", key); + const en = translate("en-US", key); + assert.ok(zh.length > 0 && en.length > 0); + assert.match(zh, /[㐀-鿿]/, `${key} should be Chinese`); + assert.doesNotMatch(en, /[㐀-鿿]/, `${key} must not contain Chinese`); + assert.notEqual(zh, en); + } +}); + +test("canvas link outcome copy names the user-visible action", () => { + assert.equal(translate("en-US", "assistant.reply.canvasLink.hidden"), "Show hidden item"); + assert.equal(translate("en-US", "assistant.reply.canvasLink.materialize"), "Show on canvas"); + assert.equal(translate("zh-CN", "assistant.reply.canvasLink.hidden"), "显示隐藏对象"); + assert.equal(translate("zh-CN", "assistant.reply.canvasLink.materialize"), "在画布中显示"); +}); diff --git a/tests/test_canvas_links.py b/tests/test_canvas_links.py new file mode 100644 index 000000000..c06e53c0f --- /dev/null +++ b/tests/test_canvas_links.py @@ -0,0 +1,274 @@ +"""Canvas link resolution and persistence for assistant replies (#333, backend slice).""" +import json +from pathlib import Path +from types import SimpleNamespace as NS +from uuid import UUID, uuid4, uuid5 + +from fastapi.testclient import TestClient +import pytest + +from opendirector.agent.canvas_links import ( + canvas_links_for_text, + format_canvas_link, + link_targets, + resolve_canvas_link, + with_canvas_links, +) +from opendirector.infrastructure.local_projects import LocalProjects +from opendirector_api.main import create_app +from opendirector_api import agent + + +def asset(asset_id, name, *, kind='text', run=None): + return NS(id=asset_id, name=name, kind=kind, path=f'assets/{asset_id}', + provenance=NS(source='generated', agentRunId=run)) + + +def node(node_id, *, kind='text', title='', visible=True, asset_id=None, + parent_id=None): + binding = NS(role='material', objectRef=NS(assetId=asset_id)) if asset_id else NS(role='empty') + return NS(id=node_id, kind=kind, title=title, visible=visible, + parentId=parent_id, position=NS(x=0, y=0), binding=binding) + + +def snapshot(*, project_id=None, assets=(), nodes=()): + return NS(project=NS(id=project_id or uuid4()), assets=tuple(assets), canvasItems=tuple(nodes)) + + +def test_relative_link_uses_the_turn_provenance_and_never_picks_an_unrelated_duplicate(): + turn, other = uuid4(), uuid4() + current = asset(uuid4(), 'notes.md', run=turn) + unrelated = asset(uuid4(), 'notes.md', run=other) + item = node(uuid4(), title='Finished note', asset_id=current.id) + project = snapshot(assets=[current, unrelated], nodes=[item]) + + resolved = resolve_canvas_link('notes.md', project, turn) + assert resolved == {'target': 'notes.md', 'status': 'focused', + 'nodeId': str(item.id), 'kind': 'text'} + + # Without the turn context, the duplicate filename is ambiguous instead of + # silently selecting one of the two unrelated objects. + assert resolve_canvas_link('notes.md', project)['status'] == 'ambiguous' + + +def test_missing_malformed_and_external_targets_are_typed_and_never_raise(): + project = snapshot() + for target in ('', ' ', 'unknown.mp4', 'javascript:alert(1)', + 'data:text/plain,hello', 'file:///etc/passwd', + '#section', '?query', '../../etc/passwd', 'canvas:not-a-uuid', + 'project:not-a-uuid/canvas:also-bad', 'project:missing'): + result = resolve_canvas_link(target, project) + assert result['status'] in {'missing', 'external'} + assert set(result) >= {'target', 'status'} + assert resolve_canvas_link('https://example.test/a', project) == { + 'target': 'https://example.test/a', 'status': 'external'} + assert link_targets('plain text without links') == [] + + +def test_hidden_materialize_ambiguous_and_explicit_nodes(): + hidden = node(uuid4(), title='Old wording', visible=False) + unplaced = asset(uuid4(), 'draft.md') + shared = asset(uuid4(), 'shared.mp4', kind='video') + first = node(uuid4(), kind='video', title='Take 1', asset_id=shared.id) + second = node(uuid4(), kind='video', title='Take 2', asset_id=shared.id) + project = snapshot(assets=[unplaced, shared], nodes=[hidden, first, second]) + + revealed = resolve_canvas_link(f'canvas:{hidden.id}', project) + assert revealed['status'] == 'hidden' and revealed['action'] == 'reveal' + + materialize = resolve_canvas_link('draft.md', project) + assert materialize['status'] == 'materialize' + assert materialize['assetId'] == str(unplaced.id) + assert materialize['action'] == 'materialize' + + ambiguous = resolve_canvas_link('shared.mp4', project) + assert ambiguous['status'] == 'ambiguous' + assert {candidate['nodeId'] for candidate in ambiguous['candidates'] if 'nodeId' in candidate} == { + str(first.id), str(second.id)} + + explicit = resolve_canvas_link(f'node:{first.id}', project) + assert explicit['status'] == 'focused' and explicit['nodeId'] == str(first.id) + assert resolve_canvas_link(f'canvas:{uuid4()}', project)['status'] == 'missing' + + +def test_cross_project_targets_report_the_destination_project(): + foreign_project = uuid4() + item = node(uuid4()) + project = snapshot(nodes=[item]) + target = format_canvas_link(str(item.id), str(foreign_project)) + resolved = resolve_canvas_link(target, project) + assert resolved['status'] == 'cross-project' + assert resolved['projectId'] == str(foreign_project) + assert resolved['nodeId'] == str(item.id) + # A fully qualified link to this project resolves normally after reload. + own = format_canvas_link(str(item.id), str(project.project.id)) + assert resolve_canvas_link(own, project)['status'] == 'focused' + + +@pytest.mark.parametrize('kind', ['text', 'image', 'video', 'audio', 'subject', + 'timeline', 'group', 'stack', 'tool', 'external']) +def test_explicit_identity_resolves_every_canvas_kind(kind): + item = node(uuid4(), kind=kind, title='Item') + project = snapshot(nodes=[item]) + resolved = resolve_canvas_link(f'canvas:{item.id}', project) + assert resolved == {'target': f'canvas:{item.id}', 'status': 'focused', + 'nodeId': str(item.id), 'kind': kind} + + +def test_title_matching_covers_non_media_nodes_and_keeps_duplicates_ambiguous(): + group = node(uuid4(), kind='group', title='Scene 1') + text = node(uuid4(), kind='text', title='Finished note') + duplicate_a = node(uuid4(), kind='text', title='Same title') + duplicate_b = node(uuid4(), kind='text', title='Same title') + project = snapshot(nodes=[group, text, duplicate_a, duplicate_b]) + + assert resolve_canvas_link('Scene 1', project)['nodeId'] == str(group.id) + assert resolve_canvas_link('Finished note', project)['nodeId'] == str(text.id) + ambiguous = resolve_canvas_link('Same title', project) + assert ambiguous['status'] == 'ambiguous' + assert len(ambiguous['candidates']) == 2 + + +def test_code_examples_are_not_navigable_links(): + fence = '```markdown\n[Example](fenced.md)\n```' + inline = 'Use `[Example](inline.md)` literally.' + text = f'{fence}\n{inline}\nReal [Finished note](notes.md)' + stored = asset(uuid4(), 'notes.md') + item = node(uuid4(), title='Finished note', asset_id=stored.id) + project = snapshot(assets=[stored], nodes=[item]) + assert link_targets(text) == ['notes.md'] + links = canvas_links_for_text(text, project) + assert [link['target'] for link in links] == ['notes.md'] + assert links[0]['nodeId'] == str(item.id) + + +def test_stable_identity_does_not_depend_on_position_or_title(): + item = node(uuid4(), title='Before') + original = snapshot(nodes=[item]) + first = resolve_canvas_link('Before', original) + moved = node(item.id, title='After', parent_id=uuid4()) + moved.position = NS(x=1234, y=-500) + after = snapshot(nodes=[moved]) + second = resolve_canvas_link(f'canvas:{item.id}', after) + assert first['nodeId'] == second['nodeId'] == str(item.id) + assert resolve_canvas_link('Before', after)['status'] == 'missing' + assert resolve_canvas_link('After', after)['nodeId'] == str(item.id) + + +def test_message_enrichment_only_attaches_project_references(): + item = node(uuid4(), title='Finished note') + project = snapshot(nodes=[item]) + message = {'id': 'm', 'role': 'assistant', + 'text': '[local](notes.md) [site](https://example.test) [known]()'} + enriched = with_canvas_links(message, project) + assert [link['status'] for link in enriched['canvasLinks']] == ['missing', 'focused'] + assert 'canvasLinks' not in message + user = with_canvas_links({'id': 'u', 'role': 'user', 'text': '[x](notes.md)'}, project) + assert 'canvasLinks' not in user + + +def frames(response): + return [json.loads(line[6:]) for line in response.text.splitlines() if line.startswith('data: ')] + + +async def empty_skills(*args): + return [] + + +def test_finished_turn_persists_canvas_links_across_move_rename_and_reopen(tmp_path, monkeypatch): + async def stream(self, run): + yield {'type': 'delta', 'text': 'Done [Finished note](notes.md)'} + + monkeypatch.setattr(agent.AgentRuntimes, 'stream', stream) + monkeypatch.setattr(agent, 'runtime_available', lambda _: True) + monkeypatch.setattr(agent, 'skills', empty_skills) + from opendirector.infrastructure.model_configs import ModelConfigs + monkeypatch.setattr(ModelConfigs, 'resolve', lambda *args: {'id': str(uuid4()), 'modelId': 'test-model'}) + + project_id = str(uuid4()) + assistant_path = f'/api/creation/projects/{project_id}/assistant' + root = f'/api/creation/projects/{project_id}' + headers = {'X-OpenDirector-Request': '1', 'X-OpenDirector-Session': str(uuid4())} + + with TestClient(create_app(LocalProjects(tmp_path)), headers=headers) as client: + created = client.post(root + '/commands', json={ + 'type': 'project.create', 'projectId': project_id, + 'commandId': str(uuid4()), 'payload': {'name': 'Links'}}) + assert created.status_code == 200, created.text + assert client.post(root + '/open').status_code == 200 + + item_id, spec_id = str(uuid4()), str(uuid4()) + batch = client.post(root + '/commands', json={ + 'type': 'creation.batch', 'commandId': str(uuid4()), 'projectId': project_id, + 'payload': {'operations': [ + {'type': 'generationSpec.create', 'payload': {'spec': { + 'id': spec_id, 'projectId': project_id, 'kind': 'text', 'purpose': 'draft'}}}, + {'type': 'canvas.show', 'payload': {'item': { + 'id': item_id, 'projectId': project_id, 'kind': 'text', + 'title': 'Finished note', + 'binding': {'role': 'empty', 'draftSpec': {'id': spec_id}}}}}, + ]}}) + assert batch.status_code == 200 and batch.json()['status'] == 'applied', batch.text + written = client.post(root + '/commands', json={ + 'type': 'asset.writeText', 'commandId': str(uuid4()), 'projectId': project_id, + 'payload': {'name': 'notes.md', 'text': 'v1'}}) + assert written.status_code == 200 and written.json()['status'] == 'applied', written.text + saved_asset = client.get(root).json()['assets'][0] + materialized = client.post(root + '/commands', json={ + 'type': 'canvas.materialize', 'commandId': str(uuid4()), 'projectId': project_id, + 'payload': {'itemId': item_id, 'asset': { + 'assetId': saved_asset['id'], 'revision': saved_asset['revision'], + 'fingerprint': saved_asset['fingerprint'], 'kind': saved_asset['kind']}}, + 'expectedVersions': [{'objectType': 'canvas_item', 'id': item_id, 'revision': 1}]}) + assert materialized.status_code == 200, materialized.text + assert materialized.json()['status'] == 'applied', materialized.text + + turn_id = uuid4() + response = client.post(assistant_path + '/turns', json={ + 'id': str(turn_id), 'runtime': 'default', 'text': 'Create the note'}) + assert response.status_code == 200, response.text + events = frames(response) + assert events[-1]['type'] == 'end' and events[-1]['error'] is None + saved = events[-1]['conversation']['messages'][-1] + assert saved['id'] == str(uuid5(turn_id, 'assistant')) + assert saved['canvasLinks'] == [{'target': 'notes.md', 'status': 'focused', + 'nodeId': item_id, 'kind': 'text'}] + + disk = next(tmp_path.glob('**/agent-runs/conversation.json')) + persisted = json.loads(disk.read_text(encoding='utf-8'))['messages'][-1] + assert persisted['canvasLinks'] == saved['canvasLinks'] + + moved = client.post(root + '/commands', json={ + 'type': 'canvas.move', 'commandId': str(uuid4()), 'projectId': project_id, + 'payload': {'items': [{'itemId': item_id, 'position': {'x': 640, 'y': 480}}]}, + 'expectedVersions': [{'objectType': 'canvas_item', 'id': item_id, 'revision': 2}]}) + assert moved.json()['status'] == 'applied', moved.text + renamed = client.post(root + '/commands', json={ + 'type': 'canvas.update', 'commandId': str(uuid4()), 'projectId': project_id, + 'payload': {'itemId': item_id, 'title': 'Renamed note'}, + 'expectedVersions': [{'objectType': 'canvas_item', 'id': item_id, 'revision': 3}]}) + assert renamed.json()['status'] == 'applied', renamed.text + group_id = str(uuid4()) + grouped = client.post(root + '/commands', json={ + 'type': 'creation.batch', 'commandId': str(uuid4()), 'projectId': project_id, + 'expectedVersions': [{'objectType': 'canvas_item', 'id': item_id, 'revision': 4}], + 'payload': {'operations': [ + {'type': 'canvas.show', 'payload': {'item': { + 'id': group_id, 'projectId': project_id, 'kind': 'group'}}}, + {'type': 'canvas.update', 'payload': {'itemId': item_id, 'parentId': group_id}}, + ]}}) + assert grouped.status_code == 200 and grouped.json()['status'] == 'applied', grouped.text + reread = client.get(assistant_path).json() + assert reread['messages'][-1]['canvasLinks'] == saved['canvasLinks'] + + # A fresh process reads the same persisted identity after reopening. + with TestClient(create_app(LocalProjects(tmp_path)), headers=headers) as reopened: + restored = reopened.get(assistant_path).json() + assert restored['messages'][-1]['canvasLinks'] == saved['canvasLinks'] + opened = reopened.post(root + '/open-canvas') + assert opened.status_code == 200, opened.text + restored_item = next(item for item in opened.json()['snapshot']['canvasItems'] + if item['id'] == item_id) + assert restored_item['title'] == 'Renamed note' + assert restored_item['position'] == {'x': 640, 'y': 480} + assert restored_item['parentId'] == group_id From 2f275325d02511fb60e77e1707281c538c9bb38c Mon Sep 17 00:00:00 2001 From: Xingkai98 Date: Mon, 16 Nov 2026 17:36:10 +0800 Subject: [PATCH 02/10] fix(review-r1): address review findings --- .../src/opendirector/agent/canvas_links.py | 184 +++---- packages/core/src/opendirector/agent/tools.py | 3 + .../application/creation_generation.py | 3 + .../src/opendirector/domain/creation/chat.py | 27 ++ .../opendirector/domain/creation/commands.py | 1 + .../domain/creation/generation.py | 1 + scripts/generate_contracts.py | 2 +- src/backend/chat.ts | 27 +- src/canvas/AssistantPanel.tsx | 4 +- src/canvas/AssistantReply.tsx | 18 + src/canvas/workspace/WorkspaceView.tsx | 16 + src/contracts/creation.schema.json | 459 +++++++++++++++++- src/contracts/generated.ts | 42 ++ tests/test_canvas_links.py | 38 ++ 14 files changed, 685 insertions(+), 140 deletions(-) create mode 100644 packages/core/src/opendirector/domain/creation/chat.py diff --git a/packages/core/src/opendirector/agent/canvas_links.py b/packages/core/src/opendirector/agent/canvas_links.py index 1f71289e1..cb66f6e8f 100644 --- a/packages/core/src/opendirector/agent/canvas_links.py +++ b/packages/core/src/opendirector/agent/canvas_links.py @@ -12,121 +12,25 @@ """ from __future__ import annotations -import re from typing import Any, Mapping from urllib.parse import unquote, urlsplit from uuid import UUID +from markdown_it import MarkdownIt LINK_STATUSES = ('focused', 'hidden', 'materialize', 'ambiguous', 'missing', 'cross-project', 'external') -_INLINE_CODE = re.compile(r'(`+)(?:(?!\1).)*?\1', re.DOTALL) -_FENCE = re.compile(r'^ {0,3}(`{3,}|~{3,})') - - -def _ignored_ranges(text: str) -> list[tuple[int, int]]: - """Fenced blocks and code spans are examples, never navigable links.""" - ranges: list[tuple[int, int]] = [] - fence = None - fence_start = 0 - offset = 0 - for line in text.splitlines(keepends=True): - match = _FENCE.match(line) - if fence is None: - if match is not None: - fence = match.group(1)[0] - fence_start = offset - elif match is not None and match.group(1)[0] == fence: - ranges.append((fence_start, offset + len(line))) - fence = None - offset += len(line) - if fence is not None: - ranges.append((fence_start, len(text))) - for match in _INLINE_CODE.finditer(text): - if not any(start <= match.start() < end for start, end in ranges): - ranges.append((match.start(), match.end())) - return sorted(ranges) - - -def _inside(position: int, ranges: list[tuple[int, int]]) -> bool: - return any(start <= position < end for start, end in ranges) - - -def _matching_bracket(text: str, start: int) -> int | None: - depth = 0 - index = start - while index < len(text): - char = text[index] - if char == '\\': - index += 2 - continue - if char == '[': - depth += 1 - elif char == ']': - depth -= 1 - if depth == 0: - return index - index += 1 - return None - - -def _destination(text: str, start: int) -> tuple[str | None, int]: - """Return the link destination and the offset just after the closing paren.""" - length = len(text) - index = start - while index < length and text[index] in ' \t\r\n': - index += 1 - if index < length and text[index] == '<': - end = text.find('>', index + 1) - if end == -1: - return None, length - close = text.find(')', end + 1) - return text[index + 1:end], (close + 1 if close != -1 else length) - depth = 0 - cursor = index - while cursor < length: - char = text[cursor] - if char == '\\': - cursor += 2 - continue - if char == '(': - depth += 1 - elif char == ')': - if depth == 0: - return text[index:cursor], cursor + 1 - depth -= 1 - elif char in ' \t\r\n' and depth == 0: - close = text.find(')', cursor) - return text[index:cursor], (close + 1 if close != -1 else length) - cursor += 1 - return None, length - - def link_targets(text: str) -> list[str]: - """Extract inline Markdown link destinations outside code and images.""" - ignored = _ignored_ranges(text) + """Extract renderer-compatible link destinations, excluding images/code.""" targets: list[str] = [] - index = 0 - length = len(text) - while index < length: - if _inside(index, ignored): - index = next((end for start, end in ignored if start <= index < end), length) - continue - if text[index] == '\\': - index += 2 + for token in MarkdownIt().parse(text): + if token.type != 'inline' or not token.children: continue - if text[index] == '!' and index + 1 < length and text[index + 1] == '[': - index += 2 - continue - if text[index] == '[': - close = _matching_bracket(text, index) - if close is not None and close + 1 < length and text[close + 1] == '(': - destination, end = _destination(text, close + 2) - if destination is not None: - targets.append(destination) - index = max(end, close + 2) - continue - index += 1 + for child in token.children: + if child.type == 'link_open' and child.attrs: + href = child.attrs.get('href') + if href is not None: + targets.append(str(href)) return targets @@ -139,7 +43,7 @@ def _uuid(value: str) -> str | None: def _identity(target: str) -> tuple[str, Any]: """Classify a destination without touching project state or raising.""" - raw = target.strip() + raw = unquote(target.strip()) if raw.startswith('<') and raw.endswith('>'): raw = raw[1:-1].strip() if raw.startswith(('#', '?')): @@ -248,6 +152,31 @@ def _node_candidate(item: Any) -> dict: 'hidden': not visible} +def _effective_visibility(item: Any, items: tuple[Any, ...]) -> bool: + by_id = {str(getattr(value, 'id', '')): value for value in items} + current = item + seen: set[str] = set() + while current is not None: + identity = str(getattr(current, 'id', '')) + if identity in seen or not bool(getattr(current, 'visible', True)): + return False + seen.add(identity) + parent = getattr(current, 'parentId', None) + current = by_id.get(str(parent)) if parent else None + return True + + +def _resolved_node(item: Any, snapshot: Any, result: dict) -> dict: + output = _node_result(item, result) + visible = _effective_visibility(item, _items(snapshot)) + output['status'] = 'focused' if visible else 'hidden' + if visible: + output.pop('action', None) + else: + output['action'] = 'reveal' + return output + + def _asset_candidate(asset: Any) -> dict: return {'assetId': str(getattr(asset, 'id', '')), 'kind': str(getattr(asset, 'kind', '')), @@ -267,6 +196,23 @@ def _unique(items: list[Any]) -> list[Any]: def _resolve_name(name: str, snapshot: Any, turn_id: str | None, result: dict) -> dict: assets = [asset for asset in _assets(snapshot) if _asset_matches(name, asset)] + # An adopted generation node displays the asset selected by its current + # result even though its binding remains a generation spec. + results = {str(getattr(result_version, 'id', '')): result_version + for result_version in (getattr(snapshot, 'results', ()) or ())} + specs = {str(getattr(spec, 'id', '')): spec + for spec in (getattr(snapshot, 'generationSpecs', ()) or ())} + for item in _items(snapshot): + binding = getattr(item, 'binding', None) + spec_ref = (getattr(binding, 'spec', None) or getattr(binding, 'generationSpec', None) + or getattr(binding, 'objectRef', None)) + spec = specs.get(str(getattr(spec_ref, 'id', ''))) + selected = results.get(str(getattr(spec, 'currentResultId', ''))) + asset_ref = getattr(selected, 'asset', None) + asset_id = getattr(asset_ref, 'assetId', None) + asset = next((value for value in _assets(snapshot) if str(getattr(value, 'id', '')) == str(asset_id)), None) + if asset is not None and _asset_matches(name, asset) and asset not in assets: + assets.append(asset) if assets and turn_id is not None: # The turn's own outputs take precedence over an unrelated asset that # happens to share a file name. A filename alone never picks silently. @@ -275,10 +221,26 @@ def _resolve_name(name: str, snapshot: Any, turn_id: str | None, result: dict) - assets = current if assets: asset_ids = {str(getattr(asset, 'id', '')) for asset in assets} + result_assets: dict[str, str] = {} + results = {str(getattr(value, 'id', '')): value + for value in (getattr(snapshot, 'results', ()) or ())} + specs = {str(getattr(value, 'id', '')): value + for value in (getattr(snapshot, 'generationSpecs', ()) or ())} + for item in _items(snapshot): + binding = getattr(item, 'binding', None) + spec_ref = (getattr(binding, 'spec', None) or getattr(binding, 'generationSpec', None) + or getattr(binding, 'objectRef', None)) + spec = specs.get(str(getattr(spec_ref, 'id', ''))) + selected = results.get(str(getattr(spec, 'currentResultId', ''))) + asset_ref = getattr(selected, 'asset', None) + selected_id = getattr(asset_ref, 'assetId', None) + if selected_id is not None: + result_assets[str(getattr(item, 'id', ''))] = str(selected_id) nodes = _unique([item for item in _items(snapshot) - if asset_ids.intersection(_binding_asset_ids(item))]) + if asset_ids.intersection(_binding_asset_ids(item)) or + result_assets.get(str(getattr(item, 'id', ''))) in asset_ids]) if len(assets) == 1 and len(nodes) == 1: - return _node_result(nodes[0], result) + return _resolved_node(nodes[0], snapshot, result) if len(assets) == 1 and not nodes: asset = assets[0] return {**result, 'status': 'materialize', @@ -290,7 +252,7 @@ def _resolve_name(name: str, snapshot: Any, turn_id: str | None, result: dict) - nodes = _unique([item for item in _items(snapshot) if str(getattr(item, 'title', '')) == name]) if len(nodes) == 1: - return _node_result(nodes[0], result) + return _resolved_node(nodes[0], snapshot, result) if len(nodes) > 1: return {**result, 'status': 'ambiguous', 'candidates': [_node_candidate(item) for item in nodes]} @@ -314,11 +276,11 @@ def resolve_canvas_link(target: str, snapshot: Any, turn_id: str | None = None) 'projectId': project_id, 'nodeId': node_id} item = next((item for item in _items(snapshot) if str(getattr(item, 'id', '')) == node_id), None) - return _node_result(item, result) if item is not None else {**result, 'status': 'missing'} + return _resolved_node(item, snapshot, result) if item is not None else {**result, 'status': 'missing'} if kind == 'node': item = next((item for item in _items(snapshot) if str(getattr(item, 'id', '')) == value), None) - return _node_result(item, result) if item is not None else {**result, 'status': 'missing'} + return _resolved_node(item, snapshot, result) if item is not None else {**result, 'status': 'missing'} return _resolve_name(value, snapshot, turn_id, result) except Exception: # A malformed project fact or an unexpected snapshot shape must not diff --git a/packages/core/src/opendirector/agent/tools.py b/packages/core/src/opendirector/agent/tools.py index 64da5bd17..706b291c5 100644 --- a/packages/core/src/opendirector/agent/tools.py +++ b/packages/core/src/opendirector/agent/tools.py @@ -408,6 +408,9 @@ def narrow(value: JsonValue) -> None: raise ProjectError('未知业务工具。') command = parse_command({**arguments, 'projectId': str(project_id), 'commandId': str(uuid5(turn_id, call_id))}) + if command.type == 'generation.submit': + command = command.model_copy(update={ + 'payload': command.payload.model_copy(update={'agentRunId': turn_id})}) if command.type == 'creation.batch' and any(operation.type not in COMMANDS for operation in command.payload.operations): raise ProjectError('批量命令包含未开放的 Agent 操作。', 403) if command.type in {'generation.submit', 'job.cancel', 'job.recover'}: diff --git a/packages/core/src/opendirector/application/creation_generation.py b/packages/core/src/opendirector/application/creation_generation.py index fb5fb207e..a49448b3e 100644 --- a/packages/core/src/opendirector/application/creation_generation.py +++ b/packages/core/src/opendirector/application/creation_generation.py @@ -396,6 +396,8 @@ def accept(before): originItemId=origin, previousJobId=command.payload.previousJobId, adoptionPolicy=command.payload.adoptionPolicy, adoptionResultId=old.currentResultId, adoptionMaterial=material, batchId=command.payload.batchId, showAdditionalResult=command.payload.showAdditionalResult) + if source == 'agent' and command.payload.agentRunId is not None: + stub = stub.model_copy(update={'agentRunId': command.payload.agentRunId}) task = self.task(stub) records = {str(a.id): media_record(a) for a in before.assets} try: @@ -561,6 +563,7 @@ def apply(current, asset): 'canvasItems': (*tuple(adopted_item if item and i.id == item.id else i for i in current.canvasItems), *([additional] if additional else [])), 'generationSpecs': tuple(adopted_spec if s.id == spec.id else s for s in current.generationSpecs)}) provenance = AssetProvenance(source='generated', jobId=job.id, inputFingerprint=job.inputFingerprint, + agentRunId=job.agentRunId, sources=tuple({r.asset.assetId: r.asset for r in job.inputSnapshot.references}.values())) receipt = self.assets.register(command, source='background', provenance=provenance, apply=apply) if receipt.status == 'rejected': diff --git a/packages/core/src/opendirector/domain/creation/chat.py b/packages/core/src/opendirector/domain/creation/chat.py new file mode 100644 index 000000000..01f0ce79c --- /dev/null +++ b/packages/core/src/opendirector/domain/creation/chat.py @@ -0,0 +1,27 @@ +"""Typed navigation metadata persisted with assistant conversation replies.""" +from typing import Literal +from uuid import UUID + +from pydantic import Field + +from .base import Contract + + +class ChatCanvasLinkCandidate(Contract): + nodeId: UUID | None = Field(default=None, description='Canvas item candidate identity.') + assetId: UUID | None = Field(default=None, description='Project asset candidate identity.') + kind: str | None = Field(default=None, description='Candidate business kind.') + title: str | None = Field(default=None, description='Candidate display title.') + hidden: bool | None = Field(default=None, description='Whether the candidate is hidden.') + + +class ChatCanvasLink(Contract): + target: str = Field(description='Original Markdown destination.') + status: Literal['focused', 'hidden', 'materialize', 'ambiguous', 'missing', + 'cross-project', 'external'] = Field(description='Typed link resolution outcome.') + nodeId: UUID | None = Field(default=None, description='Resolved canvas item identity.') + assetId: UUID | None = Field(default=None, description='Resolved project asset identity.') + projectId: UUID | None = Field(default=None, description='Destination project identity.') + kind: str | None = Field(default=None, description='Resolved business kind.') + action: Literal['reveal', 'materialize'] | None = Field(default=None, description='Required presentation action.') + candidates: tuple[ChatCanvasLinkCandidate, ...] | None = Field(default=None, description='Candidates requiring user choice.') diff --git a/packages/core/src/opendirector/domain/creation/commands.py b/packages/core/src/opendirector/domain/creation/commands.py index 2e02ff8ca..cbe7992c7 100644 --- a/packages/core/src/opendirector/domain/creation/commands.py +++ b/packages/core/src/opendirector/domain/creation/commands.py @@ -213,6 +213,7 @@ class GenerationSubmit(Contract): adoptionPolicy: Literal['if_current', 'candidate_only'] = Field(default='if_current', description='Policy for adopting the completed result.') batchId: UUID | None = Field(default=None, description='Generation batch ID.') showAdditionalResult: bool = Field(default=False, description='Whether to display an additional result item.') + agentRunId: UUID | None = Field(default=None, description='Trusted Agent turn that initiated this execution.') @model_validator(mode='after') def presentation(self): diff --git a/packages/core/src/opendirector/domain/creation/generation.py b/packages/core/src/opendirector/domain/creation/generation.py index 91b6f0b40..5a1f6aefb 100644 --- a/packages/core/src/opendirector/domain/creation/generation.py +++ b/packages/core/src/opendirector/domain/creation/generation.py @@ -301,6 +301,7 @@ class Job(ProjectEntity): adoptionMaterial: AssetRef | None = Field(default=None, description='Material version reserved for adoption.') adopted: bool = Field(default=False, description='Whether the generated result was adopted.') adapterParameters: dict[str, JsonValue] = Field(default_factory=dict, description='Fixed provider-specific execution parameters.') + agentRunId: UUID | None = Field(default=None, description='Agent turn that initiated this execution, when applicable.') @model_validator(mode='after') def execution_identity(self): diff --git a/scripts/generate_contracts.py b/scripts/generate_contracts.py index 20426da25..1d9a4b333 100644 --- a/scripts/generate_contracts.py +++ b/scripts/generate_contracts.py @@ -17,7 +17,7 @@ from opendirector.domain.creation.schema import ALIASES, CreationContracts from opendirector.domain.rule_facts import typescript_module -MODULES = ('base', 'assets', 'models', 'generation', 'editors', 'canvas', 'project', 'commands', 'timeline_packages', 'schema') +MODULES = ('base', 'assets', 'models', 'generation', 'editors', 'canvas', 'chat', 'project', 'commands', 'timeline_packages', 'schema') def models(): diff --git a/src/backend/chat.ts b/src/backend/chat.ts index e767b03eb..a162f54d4 100644 --- a/src/backend/chat.ts +++ b/src/backend/chat.ts @@ -2,6 +2,7 @@ import type { ReferenceContent } from "../canvas/chatReferenceContent.ts"; import { defaultTranslate } from "../canvas/assistantTranslate.ts"; import type { Translate } from "../i18n/I18nProvider.tsx"; import { headers, json, request, responseError } from "./api.ts"; +import type { ChatCanvasLink, ChatCanvasLinkCandidate } from "../contracts/generated.ts"; export type AgentRuntime = | "default" @@ -61,31 +62,7 @@ export interface ChatSource { title: string; } /** A resolved canvas reference persisted with an assistant reply. */ -export type ChatCanvasLinkStatus = - | "focused" - | "hidden" - | "materialize" - | "ambiguous" - | "missing" - | "cross-project" - | "external"; -export interface ChatCanvasLinkCandidate { - nodeId?: string; - assetId?: string; - kind?: string; - title?: string; - hidden?: boolean; -} -export interface ChatCanvasLink { - target: string; - status: ChatCanvasLinkStatus; - nodeId?: string; - assetId?: string; - projectId?: string; - kind?: string; - action?: "reveal" | "materialize"; - candidates?: ChatCanvasLinkCandidate[]; -} +export type { ChatCanvasLink, ChatCanvasLinkCandidate }; export interface ChatMessage { canvasLinks?: ChatCanvasLink[]; sources?: ChatSource[]; diff --git a/src/canvas/AssistantPanel.tsx b/src/canvas/AssistantPanel.tsx index 1da3e8e12..84e29a9eb 100644 --- a/src/canvas/AssistantPanel.tsx +++ b/src/canvas/AssistantPanel.tsx @@ -68,6 +68,7 @@ export function AssistantPanel({ onModelSelections, onOpenSettings, onPrepareReferences, + onFocusCanvasNode, }: { documentId: string; documentTitle: string; @@ -84,6 +85,7 @@ export function AssistantPanel({ onModelSelections?: (selections: ModelSelections) => void; onOpenSettings: () => void; onPrepareReferences: () => Promise; + onFocusCanvasNode?: (nodeId: string) => void; }) { const t = useT(); const chat = useAssistantChat(documentId, catalog, onTurnEnd, t, open); @@ -434,7 +436,7 @@ export function AssistantPanel({ )}
{message.role === "assistant" ? ( - + ) : message.role === "user" && (message.content || message.assets?.length) ? ( referenceContent( diff --git a/src/canvas/AssistantReply.tsx b/src/canvas/AssistantReply.tsx index 4c2981b5c..368c14af3 100644 --- a/src/canvas/AssistantReply.tsx +++ b/src/canvas/AssistantReply.tsx @@ -94,10 +94,12 @@ function ToolGroup({ tools, t }: { tools: ChatTool[]; t: Translate }) { export const AssistantReply = memo(function AssistantReply({ message, thinking, + onCanvasLink, t = defaultTranslate, }: { message: ChatMessage; thinking: boolean; + onCanvasLink?: (nodeId: string) => void; /** Injected by the panel; standalone mounts fall back to the active locale. */ t?: Translate; }) { @@ -117,6 +119,21 @@ export const AssistantReply = memo(function AssistantReply({ : t("assistant.reply.copyReply"); const markdown = (text: string, key: string) => text && ( +
{ + if (!onCanvasLink || !message.canvasLinks?.length) return; + const anchor = (event.target as HTMLElement).closest("a[href]"); + if (!anchor) return; + const href = anchor.getAttribute("href") ?? ""; + const link = message.canvasLinks.find((entry) => { + if (!entry.nodeId) return false; + try { return entry.target === href || entry.target === decodeURIComponent(href); } + catch { return entry.target === href; } + }); + if (link?.nodeId) { + event.preventDefault(); + onCanvasLink(link.nodeId); + } + }}> {formatText(text)} +
); const sections = replySections(message); const renderPart = (part: ReturnType["reply"][number]) => part.type === "text" diff --git a/src/canvas/workspace/WorkspaceView.tsx b/src/canvas/workspace/WorkspaceView.tsx index 1905161b0..71fb17f4f 100644 --- a/src/canvas/workspace/WorkspaceView.tsx +++ b/src/canvas/workspace/WorkspaceView.tsx @@ -2248,6 +2248,22 @@ export function WorkspaceView({ rt }: { rt: WorkspaceRuntime }) { ); }} onTurnEnd={() => void syncServiceResults()} + onFocusCanvasNode={(id) => { + void (async () => { + if ((doc.hiddenNodes ?? []).some((node) => node.id === id)) { + const client = creationClient(doc.id); + const item = client.snapshot.canvasItems.find((node) => node.id === id); + if (!item) return; + await client.command("canvas.show", { item }, [objectVersion("canvas_item", item)]); + await client.refresh(); + await new Promise((resolve) => requestAnimationFrame(() => resolve())); + } else if (!doc.nodes.some((node) => node.id === id)) { + return; + } + setSelected([id]); + void flow.fitView({ nodes: [{ id }], padding: 0.35, duration: 250, maxZoom: 1.2 }); + })().catch((error) => notify(errorMessage(error))); + }} onModelSelections={setAssistantModels} onPrepareReferences={() => persist(historyRef.current.present)} onOpenSettings={() => { diff --git a/src/contracts/creation.schema.json b/src/contracts/creation.schema.json index 5eb935515..3e8a7affa 100644 --- a/src/contracts/creation.schema.json +++ b/src/contracts/creation.schema.json @@ -3291,6 +3291,198 @@ "title": "ChangedObject", "type": "object" }, + "ChatCanvasLink": { + "additionalProperties": false, + "properties": { + "target": { + "description": "Original Markdown destination.", + "title": "Target", + "type": "string" + }, + "status": { + "description": "Typed link resolution outcome.", + "enum": [ + "focused", + "hidden", + "materialize", + "ambiguous", + "missing", + "cross-project", + "external" + ], + "title": "Status", + "type": "string" + }, + "nodeId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Resolved canvas item identity.", + "title": "Nodeid" + }, + "assetId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Resolved project asset identity.", + "title": "Assetid" + }, + "projectId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Destination project identity.", + "title": "Projectid" + }, + "kind": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Resolved business kind.", + "title": "Kind" + }, + "action": { + "anyOf": [ + { + "enum": [ + "reveal", + "materialize" + ], + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Required presentation action.", + "title": "Action" + }, + "candidates": { + "anyOf": [ + { + "items": { + "$ref": "#/$defs/ChatCanvasLinkCandidate" + }, + "type": "array" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Candidates requiring user choice.", + "title": "Candidates" + } + }, + "required": [ + "target", + "status" + ], + "title": "ChatCanvasLink", + "type": "object" + }, + "ChatCanvasLinkCandidate": { + "additionalProperties": false, + "properties": { + "nodeId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Canvas item candidate identity.", + "title": "Nodeid" + }, + "assetId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Project asset candidate identity.", + "title": "Assetid" + }, + "kind": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Candidate business kind.", + "title": "Kind" + }, + "title": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Candidate display title.", + "title": "Title" + }, + "hidden": { + "anyOf": [ + { + "type": "boolean" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Whether the candidate is hidden.", + "title": "Hidden" + } + }, + "title": "ChatCanvasLinkCandidate", + "type": "object" + }, "CommandBase": { "additionalProperties": false, "properties": { @@ -5473,6 +5665,20 @@ "description": "Whether to display an additional result item.", "title": "Showadditionalresult", "type": "boolean" + }, + "agentRunId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Trusted Agent turn that initiated this execution.", + "title": "Agentrunid" } }, "required": [ @@ -7182,6 +7388,20 @@ "description": "Fixed provider-specific execution parameters.", "title": "Adapterparameters", "type": "object" + }, + "agentRunId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Agent turn that initiated this execution, when applicable.", + "title": "Agentrunid" } }, "required": [ @@ -18508,6 +18728,211 @@ "title": "ChangedObject", "type": "object" }, + "ChatCanvasLink": { + "additionalProperties": false, + "properties": { + "target": { + "description": "Original Markdown destination.", + "title": "Target", + "type": "string" + }, + "status": { + "description": "Typed link resolution outcome.", + "enum": [ + "focused", + "hidden", + "materialize", + "ambiguous", + "missing", + "cross-project", + "external" + ], + "title": "Status", + "type": "string" + }, + "nodeId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Resolved canvas item identity.", + "title": "Nodeid" + }, + "assetId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Resolved project asset identity.", + "title": "Assetid" + }, + "projectId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Destination project identity.", + "title": "Projectid" + }, + "kind": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Resolved business kind.", + "title": "Kind" + }, + "action": { + "anyOf": [ + { + "enum": [ + "reveal", + "materialize" + ], + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Required presentation action.", + "title": "Action" + }, + "candidates": { + "anyOf": [ + { + "items": { + "$ref": "#/$defs/ChatCanvasLinkCandidate" + }, + "type": "array" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Candidates requiring user choice.", + "title": "Candidates" + } + }, + "required": [ + "target", + "status", + "nodeId", + "assetId", + "projectId", + "kind", + "action", + "candidates" + ], + "title": "ChatCanvasLink", + "type": "object" + }, + "ChatCanvasLinkCandidate": { + "additionalProperties": false, + "properties": { + "nodeId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Canvas item candidate identity.", + "title": "Nodeid" + }, + "assetId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Project asset candidate identity.", + "title": "Assetid" + }, + "kind": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Candidate business kind.", + "title": "Kind" + }, + "title": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Candidate display title.", + "title": "Title" + }, + "hidden": { + "anyOf": [ + { + "type": "boolean" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Whether the candidate is hidden.", + "title": "Hidden" + } + }, + "required": [ + "nodeId", + "assetId", + "kind", + "title", + "hidden" + ], + "title": "ChatCanvasLinkCandidate", + "type": "object" + }, "CommandBase": { "additionalProperties": false, "properties": { @@ -20761,6 +21186,20 @@ "description": "Whether to display an additional result item.", "title": "Showadditionalresult", "type": "boolean" + }, + "agentRunId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Trusted Agent turn that initiated this execution.", + "title": "Agentrunid" } }, "required": [ @@ -20770,7 +21209,8 @@ "previousJobId", "adoptionPolicy", "batchId", - "showAdditionalResult" + "showAdditionalResult", + "agentRunId" ], "title": "GenerationSubmit", "type": "object" @@ -22551,6 +22991,20 @@ "description": "Fixed provider-specific execution parameters.", "title": "Adapterparameters", "type": "object" + }, + "agentRunId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Agent turn that initiated this execution, when applicable.", + "title": "Agentrunid" } }, "required": [ @@ -22580,7 +23034,8 @@ "adoptionResultId", "adoptionMaterial", "adopted", - "adapterParameters" + "adapterParameters", + "agentRunId" ], "title": "Job", "type": "object" diff --git a/src/contracts/generated.ts b/src/contracts/generated.ts index 3e5a30968..939b76042 100644 --- a/src/contracts/generated.ts +++ b/src/contracts/generated.ts @@ -361,6 +361,25 @@ export interface ChangedObjectInput extends ContractInput { readonly change: "created" | "updated" | "hidden" | "deleted"; } +export interface ChatCanvasLinkInput extends ContractInput { + readonly target: string; + readonly status: "focused" | "hidden" | "materialize" | "ambiguous" | "missing" | "cross-project" | "external"; + readonly nodeId?: string | null; + readonly assetId?: string | null; + readonly projectId?: string | null; + readonly kind?: string | null; + readonly action?: "reveal" | "materialize" | null; + readonly candidates?: ReadonlyArray | null; +} + +export interface ChatCanvasLinkCandidateInput extends ContractInput { + readonly nodeId?: string | null; + readonly assetId?: string | null; + readonly kind?: string | null; + readonly title?: string | null; + readonly hidden?: boolean | null; +} + export interface CommandBaseInput extends ContractInput { readonly commandId: string; readonly projectId: string; @@ -557,6 +576,7 @@ export interface GenerationSubmitInput extends ContractInput { readonly adoptionPolicy?: "if_current" | "candidate_only"; readonly batchId?: string | null; readonly showAdditionalResult?: boolean; + readonly agentRunId?: string | null; } export interface GenerationSubmitCommandInput extends CommandBaseInput { @@ -720,6 +740,7 @@ export interface JobInput extends ProjectEntityInput { readonly adoptionMaterial?: AssetRefInput | null; readonly adopted?: boolean; readonly adapterParameters?: Readonly>; + readonly agentRunId?: string | null; } export interface JobCancelCommandInput extends CommandBaseInput { @@ -1936,6 +1957,25 @@ export interface ChangedObject extends Contract { readonly change: "created" | "updated" | "hidden" | "deleted"; } +export interface ChatCanvasLink extends Contract { + readonly target: string; + readonly status: "focused" | "hidden" | "materialize" | "ambiguous" | "missing" | "cross-project" | "external"; + readonly nodeId: string | null; + readonly assetId: string | null; + readonly projectId: string | null; + readonly kind: string | null; + readonly action: "reveal" | "materialize" | null; + readonly candidates: ReadonlyArray | null; +} + +export interface ChatCanvasLinkCandidate extends Contract { + readonly nodeId: string | null; + readonly assetId: string | null; + readonly kind: string | null; + readonly title: string | null; + readonly hidden: boolean | null; +} + export interface CommandBase extends Contract { readonly commandId: string; readonly projectId: string; @@ -2132,6 +2172,7 @@ export interface GenerationSubmit extends Contract { readonly adoptionPolicy: "if_current" | "candidate_only"; readonly batchId: string | null; readonly showAdditionalResult: boolean; + readonly agentRunId: string | null; } export interface GenerationSubmitCommand extends CommandBase { @@ -2295,6 +2336,7 @@ export interface Job extends ProjectEntity { readonly adoptionMaterial: AssetRef | null; readonly adopted: boolean; readonly adapterParameters: Readonly>; + readonly agentRunId: string | null; } export interface JobCancelCommand extends CommandBase { diff --git a/tests/test_canvas_links.py b/tests/test_canvas_links.py index c06e53c0f..80b5748b7 100644 --- a/tests/test_canvas_links.py +++ b/tests/test_canvas_links.py @@ -142,6 +142,44 @@ def test_code_examples_are_not_navigable_links(): assert links[0]['nodeId'] == str(item.id) +def test_markdown_parser_ignores_nested_image_labels_and_malformed_destinations(): + text = ('````markdown\n```\n[example](notes.md)\n````\n' + '![alt [nested](notes.md)](image.png)\n' + '[bad](notes.md unquoted junk) [open](') + assert link_targets(text) == [] + + +def test_markdown_escaped_filename_resolves_to_real_asset(): + stored = asset(uuid4(), 'notes.md') + item = node(uuid4(), title='Finished note', asset_id=stored.id) + resolved, = canvas_links_for_text(r'[note](notes\.md)', snapshot(assets=[stored], nodes=[item])) + assert resolved['status'] == 'focused' + assert resolved['nodeId'] == str(item.id) + + +def test_child_under_hidden_group_is_hidden(): + group = node(uuid4(), kind='group', visible=False) + child = node(uuid4(), title='Child', parent_id=group.id) + resolved = resolve_canvas_link(f'canvas:{child.id}', snapshot(nodes=[group, child])) + assert resolved['status'] == 'hidden' + assert resolved['action'] == 'reveal' + + +def test_adopted_generation_asset_resolves_through_current_result(): + asset_id, spec_id, result_id, item_id = (uuid4() for _ in range(4)) + stored = asset(asset_id, 'adopted.png', kind='image') + item = node(item_id, kind='image', parent_id=None) + item.binding = NS(role='generation', objectRef=NS(id=spec_id)) + result = NS(id=result_id, asset=NS(assetId=asset_id)) + spec = NS(id=spec_id, currentResultId=result_id) + project = snapshot(assets=[stored], nodes=[item]) + project.results = (result,) + project.generationSpecs = (spec,) + resolved = resolve_canvas_link('adopted.png', project) + assert resolved['status'] == 'focused' + assert resolved['nodeId'] == str(item_id) + + def test_stable_identity_does_not_depend_on_position_or_title(): item = node(uuid4(), title='Before') original = snapshot(nodes=[item]) From 10cb2b70bef6d3d4a35a6a4f7d2abf82040b9a5a Mon Sep 17 00:00:00 2001 From: Xingkai98 Date: Mon, 16 Nov 2026 18:17:39 +0800 Subject: [PATCH 03/10] fix(review-r2): address review findings --- apps/api/src/opendirector_api/agent.py | 4 +- .../src/opendirector/agent/canvas_links.py | 50 ++++++++-- scripts/verify_canvas_link_mutations.py | 57 ++++++++++++ src/canvas/AssistantReply.tsx | 92 +++++++++++++++---- tests/canvasLinkRendering.test.mjs | 91 ++++++++++++++++++ tests/fixtures/creation-project.json | 1 + tests/test_canvas_links.py | 89 +++++++++++++++++- 7 files changed, 351 insertions(+), 33 deletions(-) create mode 100644 scripts/verify_canvas_link_mutations.py create mode 100644 tests/canvasLinkRendering.test.mjs diff --git a/apps/api/src/opendirector_api/agent.py b/apps/api/src/opendirector_api/agent.py index 1dd5204e7..0f7b3431d 100644 --- a/apps/api/src/opendirector_api/agent.py +++ b/apps/api/src/opendirector_api/agent.py @@ -14,6 +14,7 @@ from opendirector.agent.catalog import model_choices from opendirector.agent.canvas_links import canvas_links_for_text +from opendirector.domain.creation.chat import ChatCanvasLink from opendirector.agent.context import RunContext, RuntimeId from opendirector.agent.citations import referenced_sources, with_trace_sources from opendirector.agent.runtime import AgentRuntimes, runtime_available, configured_runtimes, runtime_spec, runtime_catalog, runtime_status @@ -460,7 +461,8 @@ async def produce() -> None: canvas_links = [] try: final_snapshot = await asyncio.to_thread(projects.snapshot, project_id) - canvas_links = canvas_links_for_text(text, final_snapshot, value.id) + canvas_links = [ChatCanvasLink.model_validate(link).model_dump(mode='json') + for link in canvas_links_for_text(text, final_snapshot, value.id)] except Exception: # Link resolution is navigation metadata; it must not # fail a turn whose accepted result is already saved. diff --git a/packages/core/src/opendirector/agent/canvas_links.py b/packages/core/src/opendirector/agent/canvas_links.py index cb66f6e8f..380911eb4 100644 --- a/packages/core/src/opendirector/agent/canvas_links.py +++ b/packages/core/src/opendirector/agent/canvas_links.py @@ -7,12 +7,13 @@ status codes; current geometry is deliberately not stored, so moving, renaming or regrouping an item never invalidates a saved link. -Only inline Markdown links are considered. Fenced code blocks, inline code -spans, images and ordinary external URLs are left untouched. +Markdown links (including reference-style links) and plain @title mentions are +considered. Code, images and ordinary external URLs are left untouched. """ from __future__ import annotations from typing import Any, Mapping +import re from urllib.parse import unquote, urlsplit from uuid import UUID from markdown_it import MarkdownIt @@ -34,6 +35,23 @@ def link_targets(text: str) -> list[str]: return targets +def _mention_targets(text: str) -> list[str]: + targets = [] + for token in MarkdownIt().parse(text): + if token.type != 'inline' or not token.children: + continue + in_link = False + for child in token.children: + if child.type == 'link_open': + in_link = True + elif child.type == 'link_close': + in_link = False + elif child.type == 'text' and not in_link: + targets.extend('@' + match.group(1).strip() for match in re.finditer( + r'(?\u3002\uff1b\uff0c\uff01\uff1f]+)', child.content)) + return targets + + def _uuid(value: str) -> str | None: try: return str(UUID(str(value).strip())) @@ -43,12 +61,14 @@ def _uuid(value: str) -> str | None: def _identity(target: str) -> tuple[str, Any]: """Classify a destination without touching project state or raising.""" - raw = unquote(target.strip()) + raw = target.strip() if raw.startswith('<') and raw.endswith('>'): raw = raw[1:-1].strip() if raw.startswith(('#', '?')): # In-page anchors and query-only destinations are not project objects. return 'external', raw + if raw.startswith('@'): + return 'title', raw[1:].strip() if raw.startswith('project:'): project, separator, remainder = raw[len('project:'):].partition('/') if not separator: @@ -204,6 +224,8 @@ def _resolve_name(name: str, snapshot: Any, turn_id: str | None, result: dict) - for spec in (getattr(snapshot, 'generationSpecs', ()) or ())} for item in _items(snapshot): binding = getattr(item, 'binding', None) + if getattr(binding, 'role', None) != 'generation' or getattr(binding, 'materialOverride', None) is not None: + continue spec_ref = (getattr(binding, 'spec', None) or getattr(binding, 'generationSpec', None) or getattr(binding, 'objectRef', None)) spec = specs.get(str(getattr(spec_ref, 'id', ''))) @@ -213,12 +235,12 @@ def _resolve_name(name: str, snapshot: Any, turn_id: str | None, result: dict) - asset = next((value for value in _assets(snapshot) if str(getattr(value, 'id', '')) == str(asset_id)), None) if asset is not None and _asset_matches(name, asset) and asset not in assets: assets.append(asset) - if assets and turn_id is not None: - # The turn's own outputs take precedence over an unrelated asset that - # happens to share a file name. A filename alone never picks silently. - current = [asset for asset in assets if _run_id(asset) == str(turn_id)] - if current: - assets = current + if turn_id is not None: + # A filename in a turn identifies only that turn's output. Historical + # objects need an explicit stable identity (or an explicit @title). + assets = [asset for asset in assets if _run_id(asset) == str(turn_id)] + if not assets: + return {**result, 'status': 'missing'} if assets: asset_ids = {str(getattr(asset, 'id', '')) for asset in assets} result_assets: dict[str, str] = {} @@ -228,6 +250,8 @@ def _resolve_name(name: str, snapshot: Any, turn_id: str | None, result: dict) - for value in (getattr(snapshot, 'generationSpecs', ()) or ())} for item in _items(snapshot): binding = getattr(item, 'binding', None) + if getattr(binding, 'role', None) != 'generation' or getattr(binding, 'materialOverride', None) is not None: + continue spec_ref = (getattr(binding, 'spec', None) or getattr(binding, 'generationSpec', None) or getattr(binding, 'objectRef', None)) spec = specs.get(str(getattr(spec_ref, 'id', ''))) @@ -249,6 +273,10 @@ def _resolve_name(name: str, snapshot: Any, turn_id: str | None, result: dict) - 'action': 'materialize'} return {**result, 'status': 'ambiguous', 'candidates': [*map(_asset_candidate, assets), *map(_node_candidate, nodes)]} + return _resolve_title(name, snapshot, result) + + +def _resolve_title(name: str, snapshot: Any, result: dict) -> dict: nodes = _unique([item for item in _items(snapshot) if str(getattr(item, 'title', '')) == name]) if len(nodes) == 1: @@ -269,6 +297,8 @@ def resolve_canvas_link(target: str, snapshot: Any, turn_id: str | None = None) return {**result, 'status': 'external'} if kind == 'missing': return {**result, 'status': 'missing'} + if kind == 'title': + return _resolve_title(value, snapshot, result) if kind == 'project': project_id, node_id = value if project_id != _project_id(snapshot): @@ -292,7 +322,7 @@ def canvas_links_for_text(text: str, snapshot: Any, turn_id: str | None = None) """Resolve each unique inline link, omitting ordinary external URLs.""" result: list[dict] = [] seen: set[str] = set() - for target in link_targets(text): + for target in [*link_targets(text), *_mention_targets(text)]: if target in seen: continue seen.add(target) diff --git a/scripts/verify_canvas_link_mutations.py b/scripts/verify_canvas_link_mutations.py new file mode 100644 index 000000000..96dac3b0c --- /dev/null +++ b/scripts/verify_canvas_link_mutations.py @@ -0,0 +1,57 @@ +"""Run the same assertions against the repaired baseline or an in-memory mutant. + +PYTHONPATH=/tmp/odstubs:packages/core/src:apps/api/src python3 \ + scripts/verify_canvas_link_mutations.py R1 baseline|mutant +No tracked source is written. Each invocation starts a fresh Python process. +""" +import importlib +from pathlib import Path +import sys + +# scripts/opendirector.py must not shadow the installed/source package. +sys.path = [entry for entry in sys.path + if Path(entry).resolve() != Path(__file__).resolve().parent] + +import pytest + +CASES = { + 'R1': ('opendirector.agent.canvas_links', + "assets = [asset for asset in assets if _run_id(asset) == str(turn_id)]", + 'assets = assets', 'other_turn_filename'), + 'R2': ('opendirector.agent.canvas_links', + " or getattr(binding, 'materialOverride', None) is not None", '', 'material_override_links'), + 'R3a': ('opendirector.agent.canvas_links', 'raw = target.strip()', + 'raw = unquote(target.strip())', 'url_path_is_decoded_once'), + 'R3b': ('opendirector.agent.canvas_links', 'path = unquote(parsed.path)', + 'path = parsed.path', 'url_path_is_decoded_once'), + 'R4': ('opendirector.agent.canvas_links', '*_mention_targets(text)', '', 'plain_mentions_resolve'), + 'R5': ('opendirector.agent.canvas_links', + "def _mention_targets(text: str) -> list[str]:\n targets = []", + "def _mention_targets(text: str) -> list[str]:\n targets = ['@Finished note']", + 'mentions_in_code'), + 'R6': ('opendirector.agent.canvas_links', 'for token in MarkdownIt().parse(text):', + "for token in MarkdownIt().parse(text.split('\\n\\n')[0]):", 'reference_style_markdown'), + 'R7': ('opendirector_api.agent', + "ChatCanvasLink.model_validate(link).model_dump(mode='json')", 'link', + 'finished_turn_persists'), + 'M2': ('opendirector.agent.canvas_links', + "visible = _effective_visibility(item, _items(snapshot))", 'visible = True', + 'hidden_materialize_ambiguous or child_under_hidden'), + 'M4': ('opendirector.agent.canvas_links', + "if token.type != 'inline' or not token.children:", + "if token.type == 'fence':\n targets.append('fenced.md')\n if token.type != 'inline' or not token.children:", + 'code_examples_are_not_navigable'), +} + +case, mode = sys.argv[1:] +assert mode in {'baseline', 'mutant'} +name, old, new, selector = CASES[case] +module = importlib.import_module(name) +if mode == 'mutant': + path = Path(module.__file__) + source = path.read_text() + assert old in source, f'{case}: mutation source absent' + exec(compile(source.replace(old, new), str(path), 'exec'), module.__dict__) +result = pytest.main(['tests/test_canvas_links.py', '-q', '-k', selector, '-p', 'no:cacheprovider']) +print(f'{case} {mode}: pytest exit={result}') +sys.exit(0 if result == (0 if mode == 'baseline' else 1) else 1) diff --git a/src/canvas/AssistantReply.tsx b/src/canvas/AssistantReply.tsx index 368c14af3..91e0f3a1d 100644 --- a/src/canvas/AssistantReply.tsx +++ b/src/canvas/AssistantReply.tsx @@ -1,6 +1,8 @@ import { memo, useMemo, useState } from "react"; -import { Streamdown } from "streamdown"; -import type { LinkSafetyModalProps } from "streamdown"; +import type { ComponentProps } from "react"; +import { Streamdown, defaultRehypePlugins, defaultUrlTransform } from "streamdown"; +import type { Components, LinkSafetyModalProps, StreamdownProps } from "streamdown"; +import type { Element, Root } from "hast"; import type { ChatMessage, ChatTool } from "../backend/chat"; import type { Translate } from "../i18n/I18nProvider.tsx"; import type { MessageKey } from "../i18n/types.ts"; @@ -18,6 +20,38 @@ const STATUS: Record = { interrupted: "assistant.reply.status.interrupted", }; +function canvasReferencePlugin({ targets, prepare }: { targets: string[]; prepare: boolean }) { + const placeholders = new Map(targets.map((target, index) => + [target, `https://opendirector.invalid/canvas-reference/${index}`])); + const originals = new Map(Array.from(placeholders, ([target, placeholder]) => [placeholder, target])); + return (tree: Root) => { + const visit = (parent: Root | Element) => { + for (const child of parent.children) { + if (child.type !== "element") continue; + if (child.tagName === "a") { + const href = String(child.properties.href); + if (prepare && placeholders.has(href)) { + child.properties.href = placeholders.get(href); + } else if (!prepare && originals.has(href)) { + child.tagName = "canvas-reference"; + child.properties.href = originals.get(href); + } + } + visit(child); + } + }; + visit(tree); + }; +} + +function prepareCanvasReferences(options: { targets: string[] }) { + return canvasReferencePlugin({ ...options, prepare: true }); +} + +function renderCanvasReferences(options: { targets: string[] }) { + return canvasReferencePlugin({ ...options, prepare: false }); +} + function ToolActivity({ tool, t }: { tool: ChatTool; t: Translate }) { return (
@@ -104,6 +138,7 @@ export const AssistantReply = memo(function AssistantReply({ t?: Translate; }) { const [copyState, setCopyState] = useState<"idle" | "copied" | "failed">("idle"); + const [canvasHint, setCanvasHint] = useState(null); const formatText = useMemo(() => assistantCitationFormatter( message.text, message.sources, message.status === "streaming", t, ), [message.text, message.sources, message.status, t]); @@ -111,6 +146,38 @@ export const AssistantReply = memo(function AssistantReply({ enabled: true, renderModal: (props: LinkSafetyModalProps) => props.isOpen ? : null, }), [t]); + const projectLinks = useMemo(() => { + const links = new Map(message.canvasLinks?.map((link) => [link.target, link])); + // Give persisted project references their own renderer. Ordinary anchors + // retain Streamdown's URL transform and link-safety behavior. + const components: Components = { + "canvas-reference": ({ href, children }: ComponentProps<"a">) => { + const link = links.get(String(href)); + if (!link) return {children}; + return { + event.preventDefault(); + if ((link.status === "focused" || link.status === "hidden") && link.nodeId) { + onCanvasLink?.(link.nodeId); + setCanvasHint(null); + } else { + const keys: Record = { + "cross-project": "assistant.reply.canvasLink.crossProject", + materialize: "assistant.reply.canvasLink.materialize", + ambiguous: "assistant.reply.canvasLink.ambiguous", + missing: "assistant.reply.canvasLink.missing", + }; + setCanvasHint(t(keys[link.status] ?? "assistant.reply.canvasLink.missing") + + (link.status === "cross-project" && link.projectId ? ` · ${link.projectId}` : "")); + } + }}>{children}; + }, + }; + const targets = Array.from(links.keys()); + const rehypePlugins: StreamdownProps["rehypePlugins"] = [defaultRehypePlugins.raw, + [prepareCanvasReferences, { targets }], defaultRehypePlugins.sanitize, + defaultRehypePlugins.harden, [renderCanvasReferences, { targets }]]; + return { components, rehypePlugins }; + }, [message.canvasLinks, onCanvasLink, t]); const copyLabel = copyState === "copied" ? t("assistant.reply.copied") @@ -119,21 +186,6 @@ export const AssistantReply = memo(function AssistantReply({ : t("assistant.reply.copyReply"); const markdown = (text: string, key: string) => text && ( -
{ - if (!onCanvasLink || !message.canvasLinks?.length) return; - const anchor = (event.target as HTMLElement).closest("a[href]"); - if (!anchor) return; - const href = anchor.getAttribute("href") ?? ""; - const link = message.canvasLinks.find((entry) => { - if (!entry.nodeId) return false; - try { return entry.target === href || entry.target === decodeURIComponent(href); } - catch { return entry.target === href; } - }); - if (link?.nodeId) { - event.preventDefault(); - onCanvasLink(link.nodeId); - } - }}> node.tagName === "canvas-reference" + ? url : defaultUrlTransform(url, attribute, node)} > {formatText(text)} -
); const sections = replySections(message); const renderPart = (part: ReturnType["reply"][number]) => part.type === "text" @@ -152,6 +207,7 @@ export const AssistantReply = memo(function AssistantReply({ : ; return ( <> + {canvasHint &&

{canvasHint}

} {!!message.plan?.length &&
diff --git a/tests/canvasLinkRendering.test.mjs b/tests/canvasLinkRendering.test.mjs new file mode 100644 index 000000000..a28b988fc --- /dev/null +++ b/tests/canvasLinkRendering.test.mjs @@ -0,0 +1,91 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { mkdtemp, readFile, symlink, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { resolve, join } from "node:path"; +import { pathToFileURL } from "node:url"; +import { createRequire } from "node:module"; + +// An existing dependency tree may be supplied on machines with partial installs. +const modules = resolve(process.env.CANVAS_LINK_UI_MODULES ?? "node_modules"); +const requireUI = createRequire(join(modules, "../package.json")); +const { build } = requireUI("esbuild"); +const { JSDOM } = requireUI("jsdom"); +const React = requireUI("react"); +const { act } = React; +const dom = new JSDOM('
', { url: "http://localhost/" }); +Object.assign(globalThis, { window: dom.window, document: dom.window.document, + HTMLElement: dom.window.HTMLElement, IS_REACT_ACT_ENVIRONMENT: true }); +Object.defineProperty(globalThis, "navigator", { value: dom.window.navigator, configurable: true }); +const { createRoot } = requireUI("react-dom/client"); +const directory = await mkdtemp(join(tmpdir(), "canvas-links-")); +await symlink(modules, join(directory, "node_modules")); +await build({ entryPoints: [resolve("src/canvas/AssistantReply.tsx")], + outfile: join(directory, "reply.mjs"), bundle: true, platform: "node", + format: "esm", jsx: "automatic", packages: "external", + plugins: [{ name: "review-mutation", setup(builder) { + if (!process.env.CANVAS_LINK_UI_MUTATION) return; + builder.onLoad({ filter: /AssistantReply\.tsx$/ }, async ({ path }) => { + const mutations = { + U1: ['components={projectLinks.components}', 'components={{}}'], + U2: ['(link.status === "focused" || link.status === "hidden")', '(link.status !== "external")'], + }; + const [before, after] = mutations[process.env.CANVAS_LINK_UI_MUTATION]; + const source = await readFile(path, "utf8"); + assert.ok(source.includes(before), "mutation source must exist"); + return { contents: source.replace(before, after), loader: "tsx" }; + }); + } }], +}); +const { AssistantReply } = await import(pathToFileURL(join(directory, "reply.mjs"))); +const { translate } = await import("../src/i18n/messages.ts"); +const id = "11111111-1111-4111-8111-111111111111"; + +async function renderLink(link, locale, status, check) { + const root = createRoot(document.getElementById("root")); + const calls = []; + try { + await act(async () => root.render(React.createElement(AssistantReply, { + message: { id: "reply", role: "assistant", status, + text: `[Finished note](${link.target})`, canvasLinks: [link] }, + thinking: false, t: (key, params) => translate(locale, key, params), + onCanvasLink: (nodeId) => calls.push(nodeId), + }))); + await check(calls); + } finally { + await act(async () => root.unmount()); + } +} + +test("resolved project links render real anchors and invoke local focus", async () => { + for (const locale of ["en-US", "zh-CN"]) { + for (const status of ["streaming", "complete"]) { + for (const [target, linkStatus] of [["notes.md", "focused"], [`canvas:${id}`, "focused"], [`canvas:${id}`, "hidden"]]) { + await renderLink({ target, status: linkStatus, nodeId: id }, locale, status, async (calls) => { + const anchor = document.querySelector(".cw-chat-markdown a[href]"); + assert.ok(anchor, `resolved project reference must be an anchor: ${document.body.innerHTML}`); + assert.equal(anchor.textContent, "Finished note"); + assert.ok(!document.body.textContent.includes("[blocked]")); + await act(async () => anchor.dispatchEvent(new dom.window.MouseEvent("click", { bubbles: true, cancelable: true }))); + assert.deepEqual(calls, [id]); + }); + } + } + } +}); + +test("cross-project links show a translated destination hint and never focus a colliding local ID", async () => { + const projectId = "22222222-2222-4222-8222-222222222222"; + for (const locale of ["en-US", "zh-CN"]) { + await renderLink({ target: `project:${projectId}/canvas:${id}`, status: "cross-project", projectId, nodeId: id }, locale, "complete", async (calls) => { + const link = document.querySelector(".cw-chat-markdown a[href]"); + assert.ok(link); + await act(async () => link.dispatchEvent(new dom.window.MouseEvent("click", { bubbles: true, cancelable: true }))); + assert.deepEqual(calls, []); + assert.ok(document.body.textContent.includes(translate(locale, "assistant.reply.canvasLink.crossProject"))); + assert.ok(document.body.textContent.includes(projectId)); + }); + } +}); + +test.after(async () => { dom.window.close(); await rm(directory, { recursive: true, force: true }); }); diff --git a/tests/fixtures/creation-project.json b/tests/fixtures/creation-project.json index a040daa0c..c928ec2b1 100644 --- a/tests/fixtures/creation-project.json +++ b/tests/fixtures/creation-project.json @@ -1034,6 +1034,7 @@ "cancelRequested": false, "remoteCancellation": "not_requested", "originItemId": null, + "agentRunId": null, "previousJobId": null, "technicalCheck": null, "adoptionPolicy": "if_current", diff --git a/tests/test_canvas_links.py b/tests/test_canvas_links.py index 80b5748b7..ef97966d0 100644 --- a/tests/test_canvas_links.py +++ b/tests/test_canvas_links.py @@ -193,6 +193,84 @@ def test_stable_identity_does_not_depend_on_position_or_title(): assert resolve_canvas_link('After', after)['nodeId'] == str(item.id) +def validated_example(): + from test_creation_contracts import project_example + return project_example().model_dump(mode='json') + + +def test_other_turn_filename_is_missing_but_explicit_history_identity_works(): + from opendirector.domain.creation import ProjectSnapshot + data = validated_example() + data['assets'][0]['name'] = 'notes.md' + data['assets'][0]['provenance']['agentRunId'] = str(uuid4()) + data['canvasItems'][0]['title'] = 'notes.md' + project = ProjectSnapshot.model_validate(data) + assert resolve_canvas_link('notes.md', project, uuid4())['status'] == 'missing' + assert resolve_canvas_link(f'canvas:{project.canvasItems[0].id}', project, uuid4())['nodeId'] == str(project.canvasItems[0].id) + + +def test_material_override_links_only_to_the_displayed_asset(): + from opendirector.application.creation_canvas import current_asset + from opendirector.domain.creation import ProjectSnapshot + from test_creation_contracts import asset_ref + data = validated_example() + data['assets'][-1]['name'] = 'original.png' + data['assets'][1]['name'] = 'replacement.png' + data['assets'][1]['provenance'] = dict(source='imported', importedFrom='generated') + initial = ProjectSnapshot.model_validate(data) + data['canvasItems'][-3]['binding']['materialOverride'] = asset_ref(initial.assets[1]).model_dump(mode='json') + # Avoid an unrelated material representation of replacement.png. + data['canvasItems'][1]['binding'] = dict(role='empty') + project = ProjectSnapshot.model_validate(data) + item = project.canvasItems[-3] + assert current_asset(project, item).name == 'replacement.png' + assert resolve_canvas_link('original.png', project)['status'] == 'materialize' + assert resolve_canvas_link('replacement.png', project)['nodeId'] == str(item.id) + + +@pytest.mark.parametrize(('target', 'name'), [ + ('report%2520final.md', 'report%20final.md'), + ('folder%252Freport.md', 'folder%2Freport.md'), + ('folder%2Freport.md', 'report.md'), + ('report%23final.md', 'report#final.md'), + ('report%3Ffinal.md', 'report?final.md'), + ('100%25.md', '100%.md'), +]) +def test_url_path_is_decoded_once_after_structure_parsing(target, name): + stored = asset(uuid4(), name) + wrong = asset(uuid4(), 'report final.md') + item = node(uuid4(), asset_id=stored.id) + other = node(uuid4(), asset_id=wrong.id) + links = canvas_links_for_text(f'[file]({target})', snapshot(assets=[stored, wrong], nodes=[item, other])) + assert links[0]['nodeId'] == str(item.id) + + +def test_plain_mentions_resolve_unique_missing_and_duplicate_titles(): + from opendirector.domain.creation import ProjectSnapshot + data = validated_example() + data['canvasItems'][0]['title'] = 'Finished note' + data['canvasItems'][1]['title'] = 'Same title' + data['canvasItems'][2]['title'] = 'Same title' + project = ProjectSnapshot.model_validate(data) + links = canvas_links_for_text('@Finished note\n@No such item\n@Same title', project, uuid4()) + assert [link['status'] for link in links] == ['focused', 'missing', 'ambiguous'] + assert links[0]['nodeId'] == str(project.canvasItems[0].id) + assert len(links[2]['candidates']) == 2 + + +def test_mentions_in_code_images_and_link_labels_are_ignored(): + item = node(uuid4(), title='Finished note') + text = ('```\n@Finished note\n```\n`@Finished note`\n' + '![\u0040Finished note](image.png)\n[label @Finished note](https://example.test)') + assert canvas_links_for_text(text, snapshot(nodes=[item])) == [] + + +def test_reference_style_markdown_links_are_supported(): + item = node(uuid4(), title='Finished note') + links = canvas_links_for_text(f'[Finished note][ref]\n\n[ref]: canvas:{item.id}', snapshot(nodes=[item])) + assert links[0]['nodeId'] == str(item.id) + + def test_message_enrichment_only_attaches_project_references(): item = node(uuid4(), title='Finished note') project = snapshot(nodes=[item]) @@ -215,7 +293,7 @@ async def empty_skills(*args): def test_finished_turn_persists_canvas_links_across_move_rename_and_reopen(tmp_path, monkeypatch): async def stream(self, run): - yield {'type': 'delta', 'text': 'Done [Finished note](notes.md)'} + yield {'type': 'delta', 'text': f'Done [Finished note](canvas:{item_id})'} monkeypatch.setattr(agent.AgentRuntimes, 'stream', stream) monkeypatch.setattr(agent, 'runtime_available', lambda _: True) @@ -269,8 +347,11 @@ async def stream(self, run): assert events[-1]['type'] == 'end' and events[-1]['error'] is None saved = events[-1]['conversation']['messages'][-1] assert saved['id'] == str(uuid5(turn_id, 'assistant')) - assert saved['canvasLinks'] == [{'target': 'notes.md', 'status': 'focused', - 'nodeId': item_id, 'kind': 'text'}] + schema = json.loads((Path(__file__).resolve().parents[1] / 'src/contracts/creation.schema.json').read_text()) + assert set(saved['canvasLinks'][0]) >= set(schema['output']['$defs']['ChatCanvasLink']['required']) + from opendirector.domain.creation.chat import ChatCanvasLink + assert saved['canvasLinks'] == [ChatCanvasLink(target=f'canvas:{item_id}', status='focused', + nodeId=item_id, kind='text').model_dump(mode='json')] disk = next(tmp_path.glob('**/agent-runs/conversation.json')) persisted = json.loads(disk.read_text(encoding='utf-8'))['messages'][-1] @@ -299,7 +380,7 @@ async def stream(self, run): reread = client.get(assistant_path).json() assert reread['messages'][-1]['canvasLinks'] == saved['canvasLinks'] - # A fresh process reads the same persisted identity after reopening. + # A fresh application instance reads the same persisted identity. with TestClient(create_app(LocalProjects(tmp_path)), headers=headers) as reopened: restored = reopened.get(assistant_path).json() assert restored['messages'][-1]['canvasLinks'] == saved['canvasLinks'] From 593f53316aceb0149bc78613c84386e4e5cadddb Mon Sep 17 00:00:00 2001 From: Xingkai98 Date: Mon, 16 Nov 2026 19:10:51 +0800 Subject: [PATCH 04/10] fix(review-r4): address medium and low findings Co-Authored-By: Claude Code --- .../src/opendirector/agent/canvas_links.py | 15 +++-- src/canvas/AssistantReply.tsx | 65 +++++++++++++++++-- src/canvas/workspace/WorkspaceView.tsx | 11 +++- tests/canvasLinkRendering.test.mjs | 22 +++++++ tests/test_canvas_links.py | 54 ++++++++++++--- 5 files changed, 146 insertions(+), 21 deletions(-) diff --git a/packages/core/src/opendirector/agent/canvas_links.py b/packages/core/src/opendirector/agent/canvas_links.py index 380911eb4..b11228af7 100644 --- a/packages/core/src/opendirector/agent/canvas_links.py +++ b/packages/core/src/opendirector/agent/canvas_links.py @@ -8,7 +8,9 @@ or regrouping an item never invalidates a saved link. Markdown links (including reference-style links) and plain @title mentions are -considered. Code, images and ordinary external URLs are left untouched. +considered. Code, images and ordinary external URLs are left untouched. Item +titles resolve only through @title mentions; a bare link destination matches +asset names, not item titles. """ from __future__ import annotations @@ -48,7 +50,7 @@ def _mention_targets(text: str) -> list[str]: in_link = False elif child.type == 'text' and not in_link: targets.extend('@' + match.group(1).strip() for match in re.finditer( - r'(?\u3002\uff1b\uff0c\uff01\uff1f]+)', child.content)) + r"(?.:\"'`\u3002\uff1b\uff0c\uff01\uff1f\uff09]+)", child.content)) return targets @@ -235,9 +237,10 @@ def _resolve_name(name: str, snapshot: Any, turn_id: str | None, result: dict) - asset = next((value for value in _assets(snapshot) if str(getattr(value, 'id', '')) == str(asset_id)), None) if asset is not None and _asset_matches(name, asset) and asset not in assets: assets.append(asset) - if turn_id is not None: - # A filename in a turn identifies only that turn's output. Historical - # objects need an explicit stable identity (or an explicit @title). + if turn_id is not None and len(assets) > 1: + # A filename shared by several assets in a turn identifies only that + # turn's output; a single asset (a user import or an earlier turn's + # result) still resolves by name. assets = [asset for asset in assets if _run_id(asset) == str(turn_id)] if not assets: return {**result, 'status': 'missing'} @@ -273,7 +276,7 @@ def _resolve_name(name: str, snapshot: Any, turn_id: str | None, result: dict) - 'action': 'materialize'} return {**result, 'status': 'ambiguous', 'candidates': [*map(_asset_candidate, assets), *map(_node_candidate, nodes)]} - return _resolve_title(name, snapshot, result) + return {**result, 'status': 'missing'} def _resolve_title(name: str, snapshot: Any, result: dict) -> dict: diff --git a/src/canvas/AssistantReply.tsx b/src/canvas/AssistantReply.tsx index 91e0f3a1d..8f6c0f4c1 100644 --- a/src/canvas/AssistantReply.tsx +++ b/src/canvas/AssistantReply.tsx @@ -1,6 +1,6 @@ import { memo, useMemo, useState } from "react"; import type { ComponentProps } from "react"; -import { Streamdown, defaultRehypePlugins, defaultUrlTransform } from "streamdown"; +import { Streamdown, defaultRehypePlugins } from "streamdown"; import type { Components, LinkSafetyModalProps, StreamdownProps } from "streamdown"; import type { Element, Root } from "hast"; import type { ChatMessage, ChatTool } from "../backend/chat"; @@ -52,6 +52,62 @@ function renderCanvasReferences(options: { targets: string[] }) { return canvasReferencePlugin({ ...options, prepare: false }); } +/** Turn plain @title mentions into the same clickable canvas-reference nodes. */ +function renderCanvasMentions({ targets }: { targets: string[] }) { + // Longer names first so a prefix mention never splits a longer one. + const mentions = targets.filter((target) => target.startsWith("@")) + .sort((a, b) => b.length - a.length); + return (tree: Root) => { + if (!mentions.length) return; + const split = (value: string): Element["children"] => { + const parts: Element["children"] = []; + let rest = value; + while (rest.length) { + let index = -1; + let mention = ""; + for (const target of mentions) { + const found = rest.indexOf(target); + if (found !== -1 && (index === -1 || found < index)) { + index = found; + mention = target; + } + } + if (index === -1) { + parts.push({ type: "text", value: rest }); + break; + } + if (index > 0) parts.push({ type: "text", value: rest.slice(0, index) }); + parts.push({ + type: "element", + tagName: "canvas-reference", + properties: { href: mention }, + children: [{ type: "text", value: mention }], + }); + rest = rest.slice(index + mention.length); + } + return parts; + }; + const visit = (parent: Root | Element) => { + const next: Element["children"] = []; + for (const child of parent.children) { + if (child.type === "element") { + if (child.tagName !== "code" && child.tagName !== "pre" && + child.tagName !== "a" && child.tagName !== "canvas-reference") { + visit(child); + } + next.push(child); + } else if (child.type === "text") { + next.push(...split(child.value)); + } else { + next.push(child as Element["children"][number]); + } + } + parent.children = next; + }; + visit(tree); + }; +} + function ToolActivity({ tool, t }: { tool: ChatTool; t: Translate }) { return (
@@ -149,7 +205,7 @@ export const AssistantReply = memo(function AssistantReply({ const projectLinks = useMemo(() => { const links = new Map(message.canvasLinks?.map((link) => [link.target, link])); // Give persisted project references their own renderer. Ordinary anchors - // retain Streamdown's URL transform and link-safety behavior. + // keep Streamdown's sanitize and link-safety behavior. const components: Components = { "canvas-reference": ({ href, children }: ComponentProps<"a">) => { const link = links.get(String(href)); @@ -175,7 +231,8 @@ export const AssistantReply = memo(function AssistantReply({ const targets = Array.from(links.keys()); const rehypePlugins: StreamdownProps["rehypePlugins"] = [defaultRehypePlugins.raw, [prepareCanvasReferences, { targets }], defaultRehypePlugins.sanitize, - defaultRehypePlugins.harden, [renderCanvasReferences, { targets }]]; + defaultRehypePlugins.harden, [renderCanvasReferences, { targets }], + [renderCanvasMentions, { targets }]]; return { components, rehypePlugins }; }, [message.canvasLinks, onCanvasLink, t]); const copyLabel = @@ -195,8 +252,6 @@ export const AssistantReply = memo(function AssistantReply({ linkSafety={linkSafety} components={projectLinks.components} rehypePlugins={projectLinks.rehypePlugins} - urlTransform={(url, attribute, node) => node.tagName === "canvas-reference" - ? url : defaultUrlTransform(url, attribute, node)} > {formatText(text)} diff --git a/src/canvas/workspace/WorkspaceView.tsx b/src/canvas/workspace/WorkspaceView.tsx index 71fb17f4f..32711da98 100644 --- a/src/canvas/workspace/WorkspaceView.tsx +++ b/src/canvas/workspace/WorkspaceView.tsx @@ -2254,7 +2254,16 @@ export function WorkspaceView({ rt }: { rt: WorkspaceRuntime }) { const client = creationClient(doc.id); const item = client.snapshot.canvasItems.find((node) => node.id === id); if (!item) return; - await client.command("canvas.show", { item }, [objectVersion("canvas_item", item)]); + const versions = [objectVersion("canvas_item", item)]; + let parentId = item.parentId; + while (parentId) { + const parent = client.snapshot.canvasItems.find( + (item) => item.id === parentId, + )!; + versions.push(objectVersion("canvas_item", parent)); + parentId = parent.parentId; + } + await client.command("canvas.show", { item }, versions); await client.refresh(); await new Promise((resolve) => requestAnimationFrame(() => resolve())); } else if (!doc.nodes.some((node) => node.id === id)) { diff --git a/tests/canvasLinkRendering.test.mjs b/tests/canvasLinkRendering.test.mjs index a28b988fc..cfc0216d5 100644 --- a/tests/canvasLinkRendering.test.mjs +++ b/tests/canvasLinkRendering.test.mjs @@ -88,4 +88,26 @@ test("cross-project links show a translated destination hint and never focus a c } }); +test("plain @mentions render as clickable canvas links", async () => { + const root = createRoot(document.getElementById("root")); + const calls = []; + try { + await act(async () => root.render(React.createElement(AssistantReply, { + message: { id: "mention", role: "assistant", status: "complete", + text: "Look at @Finished note for the result.", + canvasLinks: [{ target: "@Finished note", status: "focused", nodeId: id }] }, + thinking: false, t: (key, params) => translate("en-US", key, params), + onCanvasLink: (nodeId) => calls.push(nodeId), + }))); + const anchor = document.querySelector(".cw-chat-markdown a[href]"); + assert.ok(anchor, `@mention must render as an anchor: ${document.body.innerHTML}`); + assert.equal(anchor.textContent, "@Finished note"); + assert.equal(document.body.textContent.includes("[blocked]"), false); + await act(async () => anchor.dispatchEvent(new dom.window.MouseEvent("click", { bubbles: true, cancelable: true }))); + assert.deepEqual(calls, [id]); + } finally { + await act(async () => root.unmount()); + } +}); + test.after(async () => { dom.window.close(); await rm(directory, { recursive: true, force: true }); }); diff --git a/tests/test_canvas_links.py b/tests/test_canvas_links.py index ef97966d0..649d4ef0e 100644 --- a/tests/test_canvas_links.py +++ b/tests/test_canvas_links.py @@ -51,6 +51,22 @@ def test_relative_link_uses_the_turn_provenance_and_never_picks_an_unrelated_dup assert resolve_canvas_link('notes.md', project)['status'] == 'ambiguous' +def test_unique_asset_resolves_by_name_regardless_of_turn_provenance(): + turn = uuid4() + # A user-imported asset has no agentRunId, yet it is the only notes.md. + imported = asset(uuid4(), 'notes.md') + imported_item = node(uuid4(), title='Imported note', asset_id=imported.id) + project = snapshot(assets=[imported], nodes=[imported_item]) + assert resolve_canvas_link('notes.md', project, turn)['nodeId'] == str(imported_item.id) + + # A previous turn's output is equally unique and must not be reported as + # "not in this project" only because its run id differs. + earlier = asset(uuid4(), 'notes.md', run=uuid4()) + earlier_item = node(uuid4(), title='Earlier note', asset_id=earlier.id) + project = snapshot(assets=[earlier], nodes=[earlier_item]) + assert resolve_canvas_link('notes.md', project, turn)['nodeId'] == str(earlier_item.id) + + def test_missing_malformed_and_external_targets_are_typed_and_never_raise(): project = snapshot() for target in ('', ' ', 'unknown.mp4', 'javascript:alert(1)', @@ -122,9 +138,9 @@ def test_title_matching_covers_non_media_nodes_and_keeps_duplicates_ambiguous(): duplicate_b = node(uuid4(), kind='text', title='Same title') project = snapshot(nodes=[group, text, duplicate_a, duplicate_b]) - assert resolve_canvas_link('Scene 1', project)['nodeId'] == str(group.id) - assert resolve_canvas_link('Finished note', project)['nodeId'] == str(text.id) - ambiguous = resolve_canvas_link('Same title', project) + assert resolve_canvas_link('@Scene 1', project)['nodeId'] == str(group.id) + assert resolve_canvas_link('@Finished note', project)['nodeId'] == str(text.id) + ambiguous = resolve_canvas_link('@Same title', project) assert ambiguous['status'] == 'ambiguous' assert len(ambiguous['candidates']) == 2 @@ -183,14 +199,14 @@ def test_adopted_generation_asset_resolves_through_current_result(): def test_stable_identity_does_not_depend_on_position_or_title(): item = node(uuid4(), title='Before') original = snapshot(nodes=[item]) - first = resolve_canvas_link('Before', original) + first = resolve_canvas_link('@Before', original) moved = node(item.id, title='After', parent_id=uuid4()) moved.position = NS(x=1234, y=-500) after = snapshot(nodes=[moved]) second = resolve_canvas_link(f'canvas:{item.id}', after) assert first['nodeId'] == second['nodeId'] == str(item.id) - assert resolve_canvas_link('Before', after)['status'] == 'missing' - assert resolve_canvas_link('After', after)['nodeId'] == str(item.id) + assert resolve_canvas_link('@Before', after)['status'] == 'missing' + assert resolve_canvas_link('@After', after)['nodeId'] == str(item.id) def validated_example(): @@ -198,14 +214,16 @@ def validated_example(): return project_example().model_dump(mode='json') -def test_other_turn_filename_is_missing_but_explicit_history_identity_works(): +def test_unique_other_turn_filename_resolves_and_explicit_history_identity_works(): from opendirector.domain.creation import ProjectSnapshot data = validated_example() data['assets'][0]['name'] = 'notes.md' data['assets'][0]['provenance']['agentRunId'] = str(uuid4()) data['canvasItems'][0]['title'] = 'notes.md' project = ProjectSnapshot.model_validate(data) - assert resolve_canvas_link('notes.md', project, uuid4())['status'] == 'missing' + # A unique asset keeps resolving by filename even when it belongs to an + # earlier turn; the turn preference only disambiguates duplicate names. + assert resolve_canvas_link('notes.md', project, uuid4())['nodeId'] == str(project.canvasItems[0].id) assert resolve_canvas_link(f'canvas:{project.canvasItems[0].id}', project, uuid4())['nodeId'] == str(project.canvasItems[0].id) @@ -265,6 +283,24 @@ def test_mentions_in_code_images_and_link_labels_are_ignored(): assert canvas_links_for_text(text, snapshot(nodes=[item])) == [] +@pytest.mark.parametrize('suffix', ['.', ':', '"', "'", '`', ')']) +def test_mention_trailing_punctuation_does_not_swallow_the_title(suffix): + item = node(uuid4(), title='Finished note') + links = canvas_links_for_text(f'@Finished note{suffix}', snapshot(nodes=[item]), uuid4()) + assert [link['status'] for link in links] == ['focused'] + assert links[0]['nodeId'] == str(item.id) + + +def test_bare_title_name_is_missing_and_only_mentions_resolve_titles(): + item = node(uuid4(), kind='group', title='Scene 1') + project = snapshot(nodes=[item]) + # A bare filename-like destination matches assets, never item titles; the + # production path (agent.py) always supplies a turn id, so this fallback + # used to be dead code. + assert resolve_canvas_link('Scene 1', project, uuid4())['status'] == 'missing' + assert resolve_canvas_link('@Scene 1', project, uuid4())['nodeId'] == str(item.id) + + def test_reference_style_markdown_links_are_supported(): item = node(uuid4(), title='Finished note') links = canvas_links_for_text(f'[Finished note][ref]\n\n[ref]: canvas:{item.id}', snapshot(nodes=[item])) @@ -275,7 +311,7 @@ def test_message_enrichment_only_attaches_project_references(): item = node(uuid4(), title='Finished note') project = snapshot(nodes=[item]) message = {'id': 'm', 'role': 'assistant', - 'text': '[local](notes.md) [site](https://example.test) [known]()'} + 'text': '[local](notes.md) [site](https://example.test) @Finished note'} enriched = with_canvas_links(message, project) assert [link['status'] for link in enriched['canvasLinks']] == ['missing', 'focused'] assert 'canvasLinks' not in message From 1ab7e5bd59c3155619c85197b32a56b7bc352cd3 Mon Sep 17 00:00:00 2001 From: Xingkai98 Date: Mon, 16 Nov 2026 20:38:23 +0800 Subject: [PATCH 05/10] =?UTF-8?q?fix(review-r5):=20=E5=AE=9A=E7=82=B9?= =?UTF-8?q?=E4=BF=AE=E7=9B=AE=E5=BD=95=E5=8C=B9=E9=85=8D=E4=B8=8E=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=8F=AF=E6=89=A7=E8=A1=8C=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit M1: 目录限定链接按完整 name/path 精确匹配,不再静默退化为 basename。 L1: @提及允许标题内标点,仅剥除后随空白/行尾的尾部标点;标题解析匹配最长词前缀。 L2: R1 变异选择器指向重复文件名用例,使其在新行为下红、旧行为下绿。 M2: 将 jsdom 依赖的 UI 点击用例退化为逻辑层断言(canvasLinkClickAction),纳入 node --test;UI 点击未做浏览器级覆盖。 Co-Authored-By: Claude Code --- .../src/opendirector/agent/canvas_links.py | 41 +++++-- scripts/verify_canvas_link_mutations.py | 2 +- src/canvas/AssistantReply.tsx | 16 +-- src/canvas/assistantCanvasLink.ts | 26 ++++ tests/canvasLinkClick.test.ts | 51 ++++++++ tests/canvasLinkRendering.test.mjs | 113 ------------------ tests/test_canvas_links.py | 35 +++++- 7 files changed, 148 insertions(+), 136 deletions(-) create mode 100644 src/canvas/assistantCanvasLink.ts create mode 100644 tests/canvasLinkClick.test.ts delete mode 100644 tests/canvasLinkRendering.test.mjs diff --git a/packages/core/src/opendirector/agent/canvas_links.py b/packages/core/src/opendirector/agent/canvas_links.py index b11228af7..86044e0db 100644 --- a/packages/core/src/opendirector/agent/canvas_links.py +++ b/packages/core/src/opendirector/agent/canvas_links.py @@ -37,6 +37,9 @@ def link_targets(text: str) -> list[str]: return targets +_MENTION_TRAILING = '.:"\'\uff09' + + def _mention_targets(text: str) -> list[str]: targets = [] for token in MarkdownIt().parse(text): @@ -49,8 +52,14 @@ def _mention_targets(text: str) -> list[str]: elif child.type == 'link_close': in_link = False elif child.type == 'text' and not in_link: - targets.extend('@' + match.group(1).strip() for match in re.finditer( - r"(?.:\"'`\u3002\uff1b\uff0c\uff01\uff1f\uff09]+)", child.content)) + for match in re.finditer( + r"(?`\u3002\uff1b\uff0c\uff01\uff1f]+)", + child.content): + name = match.group(1).strip() + following = child.content[match.end():match.end() + 1] + if not following or following.isspace(): + name = name.rstrip(_MENTION_TRAILING) + targets.append('@' + name) return targets @@ -101,8 +110,8 @@ def _identity(target: str) -> tuple[str, Any]: path = path.lstrip('/') if not path or path in {'.', '..'} or path.endswith('/'): return 'missing', raw - name = path.rsplit('/', 1)[-1] - if not name or name in {'.', '..'}: + name = path + if not name or name.rsplit('/', 1)[-1] in {'.', '..'}: return 'missing', raw return 'name', name @@ -138,6 +147,11 @@ def _base_name(value: str) -> str: def _asset_matches(name: str, asset: Any) -> bool: + if '/' in name: + # A directory-qualified destination must match the full name/path; a + # same-basename file in another directory is a different object. + return name in {str(getattr(asset, 'name', '') or '').replace('\\', '/'), + str(getattr(asset, 'path', '') or '').replace('\\', '/')} return name in {_base_name(getattr(asset, 'name', '')), _base_name(getattr(asset, 'path', ''))} @@ -280,13 +294,18 @@ def _resolve_name(name: str, snapshot: Any, turn_id: str | None, result: dict) - def _resolve_title(name: str, snapshot: Any, result: dict) -> dict: - nodes = _unique([item for item in _items(snapshot) - if str(getattr(item, 'title', '')) == name]) - if len(nodes) == 1: - return _resolved_node(nodes[0], snapshot, result) - if len(nodes) > 1: - return {**result, 'status': 'ambiguous', - 'candidates': [_node_candidate(item) for item in nodes]} + candidate = name + while candidate: + nodes = _unique([item for item in _items(snapshot) + if str(getattr(item, 'title', '')) == candidate]) + if len(nodes) == 1: + return _resolved_node(nodes[0], snapshot, {**result, 'target': '@' + candidate}) + if len(nodes) > 1: + return {**result, 'target': '@' + candidate, 'status': 'ambiguous', + 'candidates': [_node_candidate(item) for item in nodes]} + # A mention in running prose can carry trailing words ("Chapter 2.1 + # here"); resolve the longest title that is a word-prefix of the mention. + candidate = candidate.rsplit(' ', 1)[0] if ' ' in candidate else '' return {**result, 'status': 'missing'} diff --git a/scripts/verify_canvas_link_mutations.py b/scripts/verify_canvas_link_mutations.py index 96dac3b0c..b41800d24 100644 --- a/scripts/verify_canvas_link_mutations.py +++ b/scripts/verify_canvas_link_mutations.py @@ -17,7 +17,7 @@ CASES = { 'R1': ('opendirector.agent.canvas_links', "assets = [asset for asset in assets if _run_id(asset) == str(turn_id)]", - 'assets = assets', 'other_turn_filename'), + 'assets = assets', 'relative_link_uses_the_turn_provenance'), 'R2': ('opendirector.agent.canvas_links', " or getattr(binding, 'materialOverride', None) is not None", '', 'material_override_links'), 'R3a': ('opendirector.agent.canvas_links', 'raw = target.strip()', diff --git a/src/canvas/AssistantReply.tsx b/src/canvas/AssistantReply.tsx index 8f6c0f4c1..fd502d447 100644 --- a/src/canvas/AssistantReply.tsx +++ b/src/canvas/AssistantReply.tsx @@ -8,6 +8,7 @@ import type { Translate } from "../i18n/I18nProvider.tsx"; import type { MessageKey } from "../i18n/types.ts"; import { CanvasIcon } from "./CanvasIcon"; import { AssistantLinkInfo } from "./AssistantLinkInfo"; +import { canvasLinkClickAction } from "./assistantCanvasLink"; import { activityProgress, replySections, toolLabel } from "./assistantActivity.ts"; import { assistantToolText } from "./assistantToolText.ts"; import { assistantCitationFormatter } from "./assistantCitations.ts"; @@ -212,18 +213,13 @@ export const AssistantReply = memo(function AssistantReply({ if (!link) return {children}; return { event.preventDefault(); - if ((link.status === "focused" || link.status === "hidden") && link.nodeId) { - onCanvasLink?.(link.nodeId); + const action = canvasLinkClickAction(link); + if ("focus" in action) { + onCanvasLink?.(action.focus); setCanvasHint(null); } else { - const keys: Record = { - "cross-project": "assistant.reply.canvasLink.crossProject", - materialize: "assistant.reply.canvasLink.materialize", - ambiguous: "assistant.reply.canvasLink.ambiguous", - missing: "assistant.reply.canvasLink.missing", - }; - setCanvasHint(t(keys[link.status] ?? "assistant.reply.canvasLink.missing") - + (link.status === "cross-project" && link.projectId ? ` · ${link.projectId}` : "")); + setCanvasHint(t(action.hintKey) + + (action.projectId ? ` · ${action.projectId}` : "")); } }}>{children}; }, diff --git a/src/canvas/assistantCanvasLink.ts b/src/canvas/assistantCanvasLink.ts new file mode 100644 index 000000000..99b271d18 --- /dev/null +++ b/src/canvas/assistantCanvasLink.ts @@ -0,0 +1,26 @@ +import type { ChatCanvasLink } from "../backend/chat"; +import type { MessageKey } from "../i18n/types"; + +/** The decision made when a resolved canvas link is clicked. */ +export type CanvasLinkClick = + | { focus: string } + | { hintKey: MessageKey; projectId?: string }; + +/** Map a resolved link to its click behavior, split out for logic-level tests. */ +export function canvasLinkClickAction(link: ChatCanvasLink): CanvasLinkClick { + if ((link.status === "focused" || link.status === "hidden") && link.nodeId) { + return { focus: link.nodeId }; + } + const keys: Record = { + "cross-project": "assistant.reply.canvasLink.crossProject", + materialize: "assistant.reply.canvasLink.materialize", + ambiguous: "assistant.reply.canvasLink.ambiguous", + missing: "assistant.reply.canvasLink.missing", + }; + return { + hintKey: keys[link.status] ?? "assistant.reply.canvasLink.missing", + ...(link.status === "cross-project" && link.projectId + ? { projectId: link.projectId } + : {}), + }; +} diff --git a/tests/canvasLinkClick.test.ts b/tests/canvasLinkClick.test.ts new file mode 100644 index 000000000..3020501e4 --- /dev/null +++ b/tests/canvasLinkClick.test.ts @@ -0,0 +1,51 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { canvasLinkClickAction } from "../src/canvas/assistantCanvasLink.ts"; +import type { ChatCanvasLink } from "../src/backend/chat.ts"; + +const id = "11111111-1111-4111-8111-111111111111"; + +function link(overrides: Partial): ChatCanvasLink { + return { + target: "notes.md", + status: "focused", + nodeId: null, + assetId: null, + projectId: null, + kind: null, + action: null, + candidates: null, + ...overrides, + }; +} + +test("focused and hidden links focus their node id on click", () => { + assert.deepEqual(canvasLinkClickAction(link({ status: "focused", nodeId: id })), { focus: id }); + assert.deepEqual(canvasLinkClickAction(link({ status: "hidden", nodeId: id, action: "reveal" })), { focus: id }); +}); + +test("a focused link without a node id never focuses", () => { + assert.deepEqual(canvasLinkClickAction(link({ status: "focused", nodeId: null })), { + hintKey: "assistant.reply.canvasLink.missing", + }); +}); + +test("non-focusable outcomes show a translated hint and never focus a colliding id", () => { + const projectId = "22222222-2222-4222-8222-222222222222"; + assert.deepEqual(canvasLinkClickAction(link({ status: "cross-project", nodeId: id, projectId })), { + hintKey: "assistant.reply.canvasLink.crossProject", + projectId, + }); + assert.deepEqual(canvasLinkClickAction(link({ status: "materialize", assetId: id })), { + hintKey: "assistant.reply.canvasLink.materialize", + }); + assert.deepEqual(canvasLinkClickAction(link({ status: "ambiguous" })), { + hintKey: "assistant.reply.canvasLink.ambiguous", + }); + assert.deepEqual(canvasLinkClickAction(link({ status: "missing" })), { + hintKey: "assistant.reply.canvasLink.missing", + }); + assert.deepEqual(canvasLinkClickAction(link({ status: "external" })), { + hintKey: "assistant.reply.canvasLink.missing", + }); +}); diff --git a/tests/canvasLinkRendering.test.mjs b/tests/canvasLinkRendering.test.mjs deleted file mode 100644 index cfc0216d5..000000000 --- a/tests/canvasLinkRendering.test.mjs +++ /dev/null @@ -1,113 +0,0 @@ -import assert from "node:assert/strict"; -import test from "node:test"; -import { mkdtemp, readFile, symlink, rm } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { resolve, join } from "node:path"; -import { pathToFileURL } from "node:url"; -import { createRequire } from "node:module"; - -// An existing dependency tree may be supplied on machines with partial installs. -const modules = resolve(process.env.CANVAS_LINK_UI_MODULES ?? "node_modules"); -const requireUI = createRequire(join(modules, "../package.json")); -const { build } = requireUI("esbuild"); -const { JSDOM } = requireUI("jsdom"); -const React = requireUI("react"); -const { act } = React; -const dom = new JSDOM('
', { url: "http://localhost/" }); -Object.assign(globalThis, { window: dom.window, document: dom.window.document, - HTMLElement: dom.window.HTMLElement, IS_REACT_ACT_ENVIRONMENT: true }); -Object.defineProperty(globalThis, "navigator", { value: dom.window.navigator, configurable: true }); -const { createRoot } = requireUI("react-dom/client"); -const directory = await mkdtemp(join(tmpdir(), "canvas-links-")); -await symlink(modules, join(directory, "node_modules")); -await build({ entryPoints: [resolve("src/canvas/AssistantReply.tsx")], - outfile: join(directory, "reply.mjs"), bundle: true, platform: "node", - format: "esm", jsx: "automatic", packages: "external", - plugins: [{ name: "review-mutation", setup(builder) { - if (!process.env.CANVAS_LINK_UI_MUTATION) return; - builder.onLoad({ filter: /AssistantReply\.tsx$/ }, async ({ path }) => { - const mutations = { - U1: ['components={projectLinks.components}', 'components={{}}'], - U2: ['(link.status === "focused" || link.status === "hidden")', '(link.status !== "external")'], - }; - const [before, after] = mutations[process.env.CANVAS_LINK_UI_MUTATION]; - const source = await readFile(path, "utf8"); - assert.ok(source.includes(before), "mutation source must exist"); - return { contents: source.replace(before, after), loader: "tsx" }; - }); - } }], -}); -const { AssistantReply } = await import(pathToFileURL(join(directory, "reply.mjs"))); -const { translate } = await import("../src/i18n/messages.ts"); -const id = "11111111-1111-4111-8111-111111111111"; - -async function renderLink(link, locale, status, check) { - const root = createRoot(document.getElementById("root")); - const calls = []; - try { - await act(async () => root.render(React.createElement(AssistantReply, { - message: { id: "reply", role: "assistant", status, - text: `[Finished note](${link.target})`, canvasLinks: [link] }, - thinking: false, t: (key, params) => translate(locale, key, params), - onCanvasLink: (nodeId) => calls.push(nodeId), - }))); - await check(calls); - } finally { - await act(async () => root.unmount()); - } -} - -test("resolved project links render real anchors and invoke local focus", async () => { - for (const locale of ["en-US", "zh-CN"]) { - for (const status of ["streaming", "complete"]) { - for (const [target, linkStatus] of [["notes.md", "focused"], [`canvas:${id}`, "focused"], [`canvas:${id}`, "hidden"]]) { - await renderLink({ target, status: linkStatus, nodeId: id }, locale, status, async (calls) => { - const anchor = document.querySelector(".cw-chat-markdown a[href]"); - assert.ok(anchor, `resolved project reference must be an anchor: ${document.body.innerHTML}`); - assert.equal(anchor.textContent, "Finished note"); - assert.ok(!document.body.textContent.includes("[blocked]")); - await act(async () => anchor.dispatchEvent(new dom.window.MouseEvent("click", { bubbles: true, cancelable: true }))); - assert.deepEqual(calls, [id]); - }); - } - } - } -}); - -test("cross-project links show a translated destination hint and never focus a colliding local ID", async () => { - const projectId = "22222222-2222-4222-8222-222222222222"; - for (const locale of ["en-US", "zh-CN"]) { - await renderLink({ target: `project:${projectId}/canvas:${id}`, status: "cross-project", projectId, nodeId: id }, locale, "complete", async (calls) => { - const link = document.querySelector(".cw-chat-markdown a[href]"); - assert.ok(link); - await act(async () => link.dispatchEvent(new dom.window.MouseEvent("click", { bubbles: true, cancelable: true }))); - assert.deepEqual(calls, []); - assert.ok(document.body.textContent.includes(translate(locale, "assistant.reply.canvasLink.crossProject"))); - assert.ok(document.body.textContent.includes(projectId)); - }); - } -}); - -test("plain @mentions render as clickable canvas links", async () => { - const root = createRoot(document.getElementById("root")); - const calls = []; - try { - await act(async () => root.render(React.createElement(AssistantReply, { - message: { id: "mention", role: "assistant", status: "complete", - text: "Look at @Finished note for the result.", - canvasLinks: [{ target: "@Finished note", status: "focused", nodeId: id }] }, - thinking: false, t: (key, params) => translate("en-US", key, params), - onCanvasLink: (nodeId) => calls.push(nodeId), - }))); - const anchor = document.querySelector(".cw-chat-markdown a[href]"); - assert.ok(anchor, `@mention must render as an anchor: ${document.body.innerHTML}`); - assert.equal(anchor.textContent, "@Finished note"); - assert.equal(document.body.textContent.includes("[blocked]"), false); - await act(async () => anchor.dispatchEvent(new dom.window.MouseEvent("click", { bubbles: true, cancelable: true }))); - assert.deepEqual(calls, [id]); - } finally { - await act(async () => root.unmount()); - } -}); - -test.after(async () => { dom.window.close(); await rm(directory, { recursive: true, force: true }); }); diff --git a/tests/test_canvas_links.py b/tests/test_canvas_links.py index 649d4ef0e..53d1b2018 100644 --- a/tests/test_canvas_links.py +++ b/tests/test_canvas_links.py @@ -51,6 +51,24 @@ def test_relative_link_uses_the_turn_provenance_and_never_picks_an_unrelated_dup assert resolve_canvas_link('notes.md', project)['status'] == 'ambiguous' +def test_directory_qualified_link_focuses_its_directory_and_never_a_basename_duplicate(): + turn, other = uuid4(), uuid4() + first_asset = asset(uuid4(), 'folder1/notes.md', run=other) + second_asset = asset(uuid4(), 'folder2/notes.md', run=turn) + first = node(uuid4(), title='Folder 1 note', asset_id=first_asset.id) + second = node(uuid4(), title='Folder 2 note', asset_id=second_asset.id) + project = snapshot(assets=[first_asset, second_asset], nodes=[first, second]) + + # The link names its directory, so it resolves folder1's asset even though + # folder2's same-basename asset belongs to the current turn. + resolved = resolve_canvas_link('folder1/notes.md', project, turn) + assert resolved['nodeId'] == str(first.id) + + # A directory with no such asset must not silently fall back to a + # same-basename file elsewhere. + assert resolve_canvas_link('folder3/notes.md', project, turn)['status'] == 'missing' + + def test_unique_asset_resolves_by_name_regardless_of_turn_provenance(): turn = uuid4() # A user-imported asset has no agentRunId, yet it is the only notes.md. @@ -249,7 +267,7 @@ def test_material_override_links_only_to_the_displayed_asset(): @pytest.mark.parametrize(('target', 'name'), [ ('report%2520final.md', 'report%20final.md'), ('folder%252Freport.md', 'folder%2Freport.md'), - ('folder%2Freport.md', 'report.md'), + ('folder%2Freport.md', 'folder/report.md'), ('report%23final.md', 'report#final.md'), ('report%3Ffinal.md', 'report?final.md'), ('100%25.md', '100%.md'), @@ -291,6 +309,21 @@ def test_mention_trailing_punctuation_does_not_swallow_the_title(suffix): assert links[0]['nodeId'] == str(item.id) +def test_mentions_keep_internal_punctuation_and_strip_only_trailing(): + chapter = node(uuid4(), title='Chapter 2.1') + finished = node(uuid4(), title='Finished note') + project = snapshot(nodes=[chapter, finished]) + + links = canvas_links_for_text('See @Chapter 2.1 here', project) + assert [link['status'] for link in links] == ['focused'] + assert links[0]['nodeId'] == str(chapter.id) + assert links[0]['target'] == '@Chapter 2.1' + + links = canvas_links_for_text('@Finished note.', project) + assert links[0]['target'] == '@Finished note' + assert links[0]['nodeId'] == str(finished.id) + + def test_bare_title_name_is_missing_and_only_mentions_resolve_titles(): item = node(uuid4(), kind='group', title='Scene 1') project = snapshot(nodes=[item]) From 47982d7b11df84595cc482a7f03ee51be0059a12 Mon Sep 17 00:00:00 2001 From: Xingkai98 Date: Mon, 16 Nov 2026 22:36:43 +0800 Subject: [PATCH 06/10] =?UTF-8?q?fix(review-r6):=20=E4=BF=AE=E6=8F=90?= =?UTF-8?q?=E5=8F=8A=E8=BE=B9=E7=95=8C=E4=B8=8E=E7=9B=96=E7=AB=A0=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=BC=BA=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit M1: @提及在句末标点后跟空白时按提及边界截断,@Finished note. Next step 等不再解析为 missing;标题内句点(Chapter 2.1)保持完整。 L1: 前端 renderCanvasMentions 对齐后端 (? --- .../src/opendirector/agent/canvas_links.py | 23 +++- src/canvas/AssistantReply.tsx | 57 +--------- src/canvas/assistantCanvasMentions.ts | 68 ++++++++++++ tests/canvasLinkMessages.test.ts | 45 ++++++++ tests/test_canvas_links.py | 103 ++++++++++++++++++ 5 files changed, 234 insertions(+), 62 deletions(-) create mode 100644 src/canvas/assistantCanvasMentions.ts diff --git a/packages/core/src/opendirector/agent/canvas_links.py b/packages/core/src/opendirector/agent/canvas_links.py index 86044e0db..19b6a5a11 100644 --- a/packages/core/src/opendirector/agent/canvas_links.py +++ b/packages/core/src/opendirector/agent/canvas_links.py @@ -32,7 +32,7 @@ def link_targets(text: str) -> list[str]: for child in token.children: if child.type == 'link_open' and child.attrs: href = child.attrs.get('href') - if href is not None: + if href is not None and str(href).strip(): targets.append(str(href)) return targets @@ -40,6 +40,21 @@ def link_targets(text: str) -> list[str]: _MENTION_TRAILING = '.:"\'\uff09' +def _mention_name(value: str) -> str: + """Trim a mention at the first sentence punctuation that ends it. + + A period or colon followed by whitespace (or the end of the text) ends the + mention, so ``@Finished note. Next step`` resolves ``Finished note``. An + internal period such as ``Chapter 2.1`` stays part of the title because it + is not followed by whitespace. + """ + value = value.strip() + for index, char in enumerate(value): + if char in _MENTION_TRAILING and (index + 1 == len(value) or value[index + 1].isspace()): + return value[:index].rstrip() + return value.rstrip() + + def _mention_targets(text: str) -> list[str]: targets = [] for token in MarkdownIt().parse(text): @@ -55,11 +70,7 @@ def _mention_targets(text: str) -> list[str]: for match in re.finditer( r"(?`\u3002\uff1b\uff0c\uff01\uff1f]+)", child.content): - name = match.group(1).strip() - following = child.content[match.end():match.end() + 1] - if not following or following.isspace(): - name = name.rstrip(_MENTION_TRAILING) - targets.append('@' + name) + targets.append('@' + _mention_name(match.group(1))) return targets diff --git a/src/canvas/AssistantReply.tsx b/src/canvas/AssistantReply.tsx index fd502d447..647f99907 100644 --- a/src/canvas/AssistantReply.tsx +++ b/src/canvas/AssistantReply.tsx @@ -12,6 +12,7 @@ import { canvasLinkClickAction } from "./assistantCanvasLink"; import { activityProgress, replySections, toolLabel } from "./assistantActivity.ts"; import { assistantToolText } from "./assistantToolText.ts"; import { assistantCitationFormatter } from "./assistantCitations.ts"; +import { renderCanvasMentions } from "./assistantCanvasMentions.ts"; import { defaultTranslate } from "./assistantTranslate.ts"; const STATUS: Record = { @@ -53,62 +54,6 @@ function renderCanvasReferences(options: { targets: string[] }) { return canvasReferencePlugin({ ...options, prepare: false }); } -/** Turn plain @title mentions into the same clickable canvas-reference nodes. */ -function renderCanvasMentions({ targets }: { targets: string[] }) { - // Longer names first so a prefix mention never splits a longer one. - const mentions = targets.filter((target) => target.startsWith("@")) - .sort((a, b) => b.length - a.length); - return (tree: Root) => { - if (!mentions.length) return; - const split = (value: string): Element["children"] => { - const parts: Element["children"] = []; - let rest = value; - while (rest.length) { - let index = -1; - let mention = ""; - for (const target of mentions) { - const found = rest.indexOf(target); - if (found !== -1 && (index === -1 || found < index)) { - index = found; - mention = target; - } - } - if (index === -1) { - parts.push({ type: "text", value: rest }); - break; - } - if (index > 0) parts.push({ type: "text", value: rest.slice(0, index) }); - parts.push({ - type: "element", - tagName: "canvas-reference", - properties: { href: mention }, - children: [{ type: "text", value: mention }], - }); - rest = rest.slice(index + mention.length); - } - return parts; - }; - const visit = (parent: Root | Element) => { - const next: Element["children"] = []; - for (const child of parent.children) { - if (child.type === "element") { - if (child.tagName !== "code" && child.tagName !== "pre" && - child.tagName !== "a" && child.tagName !== "canvas-reference") { - visit(child); - } - next.push(child); - } else if (child.type === "text") { - next.push(...split(child.value)); - } else { - next.push(child as Element["children"][number]); - } - } - parent.children = next; - }; - visit(tree); - }; -} - function ToolActivity({ tool, t }: { tool: ChatTool; t: Translate }) { return (
diff --git a/src/canvas/assistantCanvasMentions.ts b/src/canvas/assistantCanvasMentions.ts new file mode 100644 index 000000000..4ad91761c --- /dev/null +++ b/src/canvas/assistantCanvasMentions.ts @@ -0,0 +1,68 @@ +import type { Element, Root } from "hast"; + +const WORD_CHARACTER = /\w/; + +/** Turn plain @title mentions into the same clickable canvas-reference nodes. */ +export function renderCanvasMentions({ targets }: { targets: string[] }): (tree: Root) => void { + // Longer names first so a prefix mention never splits a longer one. + const mentions = targets.filter((target) => target.startsWith("@")) + .sort((a, b) => b.length - a.length); + return (tree: Root) => { + if (!mentions.length) return; + const split = (value: string): Element["children"] => { + const parts: Element["children"] = []; + let rest = value; + while (rest.length) { + let index = -1; + let mention = ""; + for (const target of mentions) { + let found = -1; + let from = 0; + while ((found = rest.indexOf(target, from)) !== -1) { + // Mirror the backend's (? 0) parts.push({ type: "text", value: rest.slice(0, index) }); + parts.push({ + type: "element", + tagName: "canvas-reference", + properties: { href: mention }, + children: [{ type: "text", value: mention }], + }); + rest = rest.slice(index + mention.length); + } + return parts; + }; + const visit = (parent: Root | Element) => { + const next: Element["children"] = []; + for (const child of parent.children) { + if (child.type === "element") { + if (child.tagName !== "code" && child.tagName !== "pre" && + child.tagName !== "a" && child.tagName !== "canvas-reference") { + visit(child); + } + next.push(child); + } else if (child.type === "text") { + next.push(...split(child.value)); + } else { + next.push(child as Element["children"][number]); + } + } + parent.children = next; + }; + visit(tree); + }; +} diff --git a/tests/canvasLinkMessages.test.ts b/tests/canvasLinkMessages.test.ts index 63ecce492..81f7e98de 100644 --- a/tests/canvasLinkMessages.test.ts +++ b/tests/canvasLinkMessages.test.ts @@ -3,6 +3,26 @@ import test from "node:test"; import { translate } from "../src/i18n/messages.ts"; import { zhCN } from "../src/i18n/messages/zh-CN/index.ts"; import { enUS } from "../src/i18n/messages/en-US/index.ts"; +import { renderCanvasMentions } from "../src/canvas/assistantCanvasMentions.ts"; + +type HastText = { type: "text"; value: string }; +type HastElement = { type: "element"; tagName: string; properties: Record; children: HastNode[] }; +type HastNode = HastText | HastElement | { type: string }; +type HastRoot = { type: "root"; children: HastNode[] }; + +function text(value: string): HastText { + return { type: "text", value }; +} + +function element(tagName: string, children: HastNode[], properties: Record = {}): HastElement { + return { type: "element", tagName, properties, children }; +} + +function rendered(value: string, targets: string[]): HastNode[] { + const tree: HastRoot = { type: "root", children: [element("p", [text(value)])] }; + renderCanvasMentions({ targets })(tree as unknown as Parameters>[0]); + return (tree.children[0] as HastElement).children; +} const keys = [ "assistant.reply.canvasLink.open", @@ -37,3 +57,28 @@ test("canvas link outcome copy names the user-visible action", () => { assert.equal(translate("zh-CN", "assistant.reply.canvasLink.hidden"), "显示隐藏对象"); assert.equal(translate("zh-CN", "assistant.reply.canvasLink.materialize"), "在画布中显示"); }); + +test("a standalone mention renders as a clickable canvas reference", () => { + assert.deepEqual(rendered("@Title", ["@Title"]), [ + element("canvas-reference", [text("@Title")], { href: "@Title" }), + ]); +}); + +test("a mention glued to a word character stays plain prose", () => { + assert.deepEqual(rendered("foo@Title", ["@Title"]), [text("foo@Title")]); + assert.deepEqual(rendered("see foo@Title and @Title", ["@Title"]), [ + text("see foo@Title and "), + element("canvas-reference", [text("@Title")], { href: "@Title" }), + ]); +}); + +test("a mention inside an anchor (an autolinked URL) is left untouched", () => { + const tree: HastRoot = { + type: "root", + children: [element("a", [text("https://x/@Title")], { href: "https://x/@Title" })], + }; + renderCanvasMentions({ targets: ["@Title"] })(tree as unknown as Parameters>[0]); + const anchor = tree.children[0] as HastElement; + assert.equal(anchor.tagName, "a"); + assert.deepEqual(anchor.children, [text("https://x/@Title")]); +}); diff --git a/tests/test_canvas_links.py b/tests/test_canvas_links.py index 53d1b2018..07e057e84 100644 --- a/tests/test_canvas_links.py +++ b/tests/test_canvas_links.py @@ -309,6 +309,27 @@ def test_mention_trailing_punctuation_does_not_swallow_the_title(suffix): assert links[0]['nodeId'] == str(item.id) +@pytest.mark.parametrize('text', [ + '@Finished note. Next step', + '@Finished note: Next step', + '@Finished note.', +]) +def test_mention_sentence_punctuation_ends_the_mention_before_running_prose(text): + item = node(uuid4(), title='Finished note') + links = canvas_links_for_text(text, snapshot(nodes=[item]), uuid4()) + assert [link['status'] for link in links] == ['focused'] + assert links[0]['target'] == '@Finished note' + assert links[0]['nodeId'] == str(item.id) + + +def test_empty_link_destinations_are_not_persisted_as_missing_noise(): + item = node(uuid4(), title='Finished note') + project = snapshot(nodes=[item]) + links = canvas_links_for_text('[x]() [y]( ) [z](notes.md)', project) + assert [link['target'] for link in links] == ['notes.md'] + assert link_targets('[x]() [y]( )') == [] + + def test_mentions_keep_internal_punctuation_and_strip_only_trailing(): chapter = node(uuid4(), title='Chapter 2.1') finished = node(uuid4(), title='Finished note') @@ -352,6 +373,88 @@ def test_message_enrichment_only_attaches_project_references(): assert 'canvasLinks' not in user +def test_agent_generation_submit_stamps_provenance_and_relative_link_hits_it(tmp_path): + import asyncio + from opendirector.application.creation_generation import CreationGeneration + from opendirector.domain.creation.commands import parse_command + from opendirector.infrastructure.creation_projects import CreationProjects + from opendirector.infrastructure.model_configs import ModelConfigs + from opendirector.agent.tools import AgentTools + + store = CreationProjects(LocalProjects(tmp_path)) + pid, session = uuid4(), str(uuid4()) + + def send(kind, *, versions=(), **payload): + return store.execute(parse_command({ + 'type': kind, 'projectId': pid, 'commandId': uuid4(), + 'expectedVersions': list(versions), 'payload': payload}), session=session) + + def version(kind, obj): + return {'objectType': kind, 'id': str(obj.id), 'revision': obj.revision} + + assert send('project.create', name='Links').status == 'applied' + store.open(pid, session) + + spec_id, item_id = uuid4(), uuid4() + assert send('creation.batch', operations=[ + {'type': 'generationSpec.create', 'payload': {'spec': { + 'id': str(spec_id), 'projectId': str(pid), 'kind': 'text', 'purpose': 'generation'}}}, + {'type': 'canvas.show', 'payload': {'item': { + 'id': str(item_id), 'projectId': str(pid), 'kind': 'text', 'title': 'Result', + 'binding': {'role': 'generation', 'objectRef': {'id': str(spec_id)}}}}}]).status == 'applied' + + configs = ModelConfigs(store.local.repository) + config = configs.save('agent', 'http://127.0.0.1:12345/v1', 'test-agent', '', + connection_type='self_hosted', protocol='openai', auth_mode='none') + spec = next(s for s in store.snapshot(pid).generationSpecs if s.id == spec_id) + assert send('generationSpec.update', versions=[version('generation_spec', spec)], + spec=spec.model_copy(update={'prompt': 'result.md', + 'modelConfigId': UUID(config['id'])}).model_dump(mode='json')).status == 'applied' + spec = store.snapshot(pid).generationSpecs[-1] + target = next(item for item in store.snapshot(pid).canvasItems if item.id == item_id) + + # A same-named user import: only the stamped generation output carries the turn. + assert send('asset.writeText', name='result.md', text='v1').status == 'applied' + + class Provider: + def __init__(self): + self.finish = asyncio.Event() + + def validate(self, kind, config): + pass + + async def generate(self, config, task, submitted): + await self.finish.wait() + return 'md', b'Actual generated text' + + provider = Provider() + generation = CreationGeneration(store, configs, provider) + business = AgentTools(store, generation, configs) + turn_id = uuid4() + provider.finish.set() + + async def submit_and_wait(): + receipt = await business.execute(pid, turn_id, session, 'submit-call', 'execute_command', { + 'type': 'generation.submit', + 'expectedVersions': [version('canvas_item', target), version('generation_spec', spec)], + 'payload': {'specId': str(spec.id), 'expectedSpecRevision': spec.revision, + 'originItemId': str(target.id)}}, [], None) + assert receipt['status'] == 'accepted', receipt + await asyncio.gather(*(job.future for job in list(generation.jobs.values()))) + + try: + asyncio.run(submit_and_wait()) + finally: + asyncio.run(generation.close()) + + snapshot = store.snapshot(pid) + generated = next(asset for asset in snapshot.assets if asset.provenance.source == 'generated') + assert str(generated.provenance.agentRunId) == str(turn_id) + resolved = resolve_canvas_link('result.md', snapshot, turn_id) + assert resolved['status'] == 'focused' + assert resolved['nodeId'] == str(target.id) + + def frames(response): return [json.loads(line[6:]) for line in response.text.splitlines() if line.startswith('data: ')] From 2f39569cf8cf67dc31f92e911d4a8271d59c1b6e Mon Sep 17 00:00:00 2001 From: Xingkai98 Date: Tue, 17 Nov 2026 01:13:02 +0800 Subject: [PATCH 07/10] fix(review-r7) Co-Authored-By: Claude Code --- .../src/opendirector/agent/canvas_links.py | 20 ++++++++++--- src/canvas/assistantCanvasMentions.ts | 5 ++-- src/i18n/messages/en-US/assistant.ts | 2 -- src/i18n/messages/zh-CN/assistant.ts | 2 -- tests/canvasLinkMessages.test.ts | 15 +++++++--- tests/test_canvas_links.py | 30 +++++++++++++++++++ 6 files changed, 60 insertions(+), 14 deletions(-) diff --git a/packages/core/src/opendirector/agent/canvas_links.py b/packages/core/src/opendirector/agent/canvas_links.py index 19b6a5a11..39ce0c4a7 100644 --- a/packages/core/src/opendirector/agent/canvas_links.py +++ b/packages/core/src/opendirector/agent/canvas_links.py @@ -37,7 +37,7 @@ def link_targets(text: str) -> list[str]: return targets -_MENTION_TRAILING = '.:"\'\uff09' +_MENTION_TRAILING = '.:"\'\uff09\uff1a' def _mention_name(value: str) -> str: @@ -67,8 +67,12 @@ def _mention_targets(text: str) -> list[str]: elif child.type == 'link_close': in_link = False elif child.type == 'text' and not in_link: + # The word boundary is ASCII-only, matching JavaScript's \w: + # CJK prose directly before a mention is not glued to it. + # Full-width punctuation such as the colon ends a mention even + # without following whitespace. for match in re.finditer( - r"(?`\u3002\uff1b\uff0c\uff01\uff1f]+)", + r"(?`\u3002\uff1b\uff0c\uff01\uff1f\uff1a]+)", child.content): targets.append('@' + _mention_name(match.group(1))) return targets @@ -304,6 +308,9 @@ def _resolve_name(name: str, snapshot: Any, turn_id: str | None, result: dict) - return {**result, 'status': 'missing'} +_PROSE_WORD = re.compile(r"[^\W\d_]+(?:['’-][^\W\d_]+)*\Z") + + def _resolve_title(name: str, snapshot: Any, result: dict) -> dict: candidate = name while candidate: @@ -315,8 +322,13 @@ def _resolve_title(name: str, snapshot: Any, result: dict) -> dict: return {**result, 'target': '@' + candidate, 'status': 'ambiguous', 'candidates': [_node_candidate(item) for item in nodes]} # A mention in running prose can carry trailing words ("Chapter 2.1 - # here"); resolve the longest title that is a word-prefix of the mention. - candidate = candidate.rsplit(' ', 1)[0] if ' ' in candidate else '' + # here"). Only strip an ordinary prose word: a number or label such as + # "@Scene 99 does not exist" may be part of a real title, so resolving + # the shorter node "Scene" would reference the wrong object. + head, _, tail = candidate.rpartition(' ') + if not head or not _PROSE_WORD.fullmatch(tail): + break + candidate = head return {**result, 'status': 'missing'} diff --git a/src/canvas/assistantCanvasMentions.ts b/src/canvas/assistantCanvasMentions.ts index 4ad91761c..523839dca 100644 --- a/src/canvas/assistantCanvasMentions.ts +++ b/src/canvas/assistantCanvasMentions.ts @@ -19,8 +19,9 @@ export function renderCanvasMentions({ targets }: { targets: string[] }): (tree: let found = -1; let from = 0; while ((found = rest.indexOf(target, from)) !== -1) { - // Mirror the backend's (? { - assert.equal(translate("en-US", "assistant.reply.canvasLink.hidden"), "Show hidden item"); + // Hidden links focus their node (which the workspace reveals); only + // outcomes that cannot focus have a hint, so there is no hidden hint key. assert.equal(translate("en-US", "assistant.reply.canvasLink.materialize"), "Show on canvas"); - assert.equal(translate("zh-CN", "assistant.reply.canvasLink.hidden"), "显示隐藏对象"); assert.equal(translate("zh-CN", "assistant.reply.canvasLink.materialize"), "在画布中显示"); }); @@ -72,6 +70,15 @@ test("a mention glued to a word character stays plain prose", () => { ]); }); +test("CJK prose directly before a mention is a boundary, like the backend", () => { + // JavaScript \w is ASCII-only, so the Chinese character before @ is prose, + // not part of the mention; the backend now uses the same boundary. + assert.deepEqual(rendered("看这个@Title", ["@Title"]), [ + text("看这个"), + element("canvas-reference", [text("@Title")], { href: "@Title" }), + ]); +}); + test("a mention inside an anchor (an autolinked URL) is left untouched", () => { const tree: HastRoot = { type: "root", diff --git a/tests/test_canvas_links.py b/tests/test_canvas_links.py index 07e057e84..a5bc78635 100644 --- a/tests/test_canvas_links.py +++ b/tests/test_canvas_links.py @@ -345,6 +345,36 @@ def test_mentions_keep_internal_punctuation_and_strip_only_trailing(): assert links[0]['nodeId'] == str(finished.id) +def test_nonexistent_multiword_title_never_falls_back_to_a_shorter_node(): + shorter = node(uuid4(), title='Scene') + project = snapshot(nodes=[shorter]) + # "99 does not exist" is not running prose that may be stripped off a + # title; resolving it to the unrelated node "Scene" references the wrong + # object. Only an ordinary trailing word may be dropped. + for text in ('@Scene 99', '@Scene 99 does not exist'): + link, = canvas_links_for_text(text, project, uuid4()) + assert link['status'] == 'missing' + assert link['target'] == text + + +@pytest.mark.parametrize(('text', 'expected'), [ + ('看这个@标题', '@标题'), + ('@标题:请看', '@标题'), + ('@标题:', '@标题'), +]) +def test_cjk_mention_boundaries_and_fullwidth_colon(text, expected): + item = node(uuid4(), title='标题') + links = canvas_links_for_text(text, snapshot(nodes=[item]), uuid4()) + assert [link['status'] for link in links] == ['focused'] + assert links[0]['target'] == expected + assert links[0]['nodeId'] == str(item.id) + + +def test_ascii_word_glued_to_a_cjk_mention_stays_prose(): + item = node(uuid4(), title='标题') + assert canvas_links_for_text('abc@标题', snapshot(nodes=[item]), uuid4()) == [] + + def test_bare_title_name_is_missing_and_only_mentions_resolve_titles(): item = node(uuid4(), kind='group', title='Scene 1') project = snapshot(nodes=[item]) From 9a2e4d8119c30576a849b9f27770ad571c8ec788 Mon Sep 17 00:00:00 2001 From: Xingkai98 Date: Tue, 17 Nov 2026 12:41:39 +0800 Subject: [PATCH 08/10] =?UTF-8?q?fix(review-r8):=20=E4=BF=AE=E6=8F=90?= =?UTF-8?q?=E5=8F=8A=E7=B2=BE=E7=A1=AE=E5=8C=B9=E9=85=8D=E3=80=81=E5=85=A8?= =?UTF-8?q?=E8=A7=92=E6=A0=87=E7=82=B9=E4=B8=8E=E7=94=BB=E5=B8=83=E9=93=BE?= =?UTF-8?q?=E6=8E=A5=E9=93=BE=E8=B7=AF=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 未截断的原始提及先做精确标题匹配,未命中再按标点/空格回退, 避免 @Dr. Smith 被截成 @Dr 并跳到错误节点 - 全角后置标点(、”…—)」』】》〉)无条件终止提及 - 中文无分隔符场景用已知标题最长前缀解析(仅 CJK 续写) - materialize/ambiguous 提示改为陈述句,去掉无对应动作的指令措辞 - 抽出 canvasReferences 插件并补 prepare→render→点击映射测试 - 补前一条 H/M 的中句、短前缀共存回归用例 Co-Authored-By: Claude Code --- .../src/opendirector/agent/canvas_links.py | 93 ++++++++++--- src/canvas/AssistantReply.tsx | 48 +------ src/canvas/assistantCanvasLink.ts | 18 +++ src/canvas/canvasReferences.ts | 40 ++++++ src/i18n/messages/en-US/assistant.ts | 4 +- src/i18n/messages/zh-CN/assistant.ts | 4 +- tests/canvasLinkMessages.test.ts | 12 +- tests/canvasLinkReferences.test.ts | 122 ++++++++++++++++++ tests/test_canvas_links.py | 61 +++++++++ 9 files changed, 336 insertions(+), 66 deletions(-) create mode 100644 src/canvas/canvasReferences.ts create mode 100644 tests/canvasLinkReferences.test.ts diff --git a/packages/core/src/opendirector/agent/canvas_links.py b/packages/core/src/opendirector/agent/canvas_links.py index 39ce0c4a7..43711e8b3 100644 --- a/packages/core/src/opendirector/agent/canvas_links.py +++ b/packages/core/src/opendirector/agent/canvas_links.py @@ -37,20 +37,29 @@ def link_targets(text: str) -> list[str]: return targets -_MENTION_TRAILING = '.:"\'\uff09\uff1a' +# Full-width punctuation that always ends a mention, even mid-sentence: +# "\u53c2\u89c1@\u6807\u9898\u3001\u7136\u540e\u7ee7\u7eed", "\u201c@\u6807\u9898\u201d\u6548\u679c\u4e0d\u9519", "\uff08@\u6807\u9898\uff09\u8bf7\u7ee7\u7eed". +_MENTION_END_ANY = '\u3001\u201d\u2026\u2014\uff09\u300d\u300f\u3011\u300b\u3009' +# ASCII sentence punctuation only ends a mention at the end of the text or +# before whitespace, so a title such as ``Chapter 2.1`` keeps its punctuation. +_MENTION_END_AFTER_SPACE = '.:"\'\uff1a' -def _mention_name(value: str) -> str: +def _mention_base(value: str) -> str: """Trim a mention at the first sentence punctuation that ends it. A period or colon followed by whitespace (or the end of the text) ends the mention, so ``@Finished note. Next step`` resolves ``Finished note``. An internal period such as ``Chapter 2.1`` stays part of the title because it - is not followed by whitespace. + is not followed by whitespace. Full-width closing punctuation ends the + mention wherever it appears. """ value = value.strip() for index, char in enumerate(value): - if char in _MENTION_TRAILING and (index + 1 == len(value) or value[index + 1].isspace()): + if char in _MENTION_END_ANY: + return value[:index].rstrip() + if char in _MENTION_END_AFTER_SPACE and ( + index + 1 == len(value) or value[index + 1].isspace()): return value[:index].rstrip() return value.rstrip() @@ -69,12 +78,14 @@ def _mention_targets(text: str) -> list[str]: elif child.type == 'text' and not in_link: # The word boundary is ASCII-only, matching JavaScript's \w: # CJK prose directly before a mention is not glued to it. - # Full-width punctuation such as the colon ends a mention even - # without following whitespace. + # Hard sentence punctuation ends the run here; softer trailing + # punctuation (period/colon/full-width closers) is kept in the + # raw mention so an exact title such as "@Dr. Smith" can win + # before the resolver trims it. for match in re.finditer( r"(?`\u3002\uff1b\uff0c\uff01\uff1f\uff1a]+)", child.content): - targets.append('@' + _mention_name(match.group(1))) + targets.append('@' + match.group(1).strip()) return targets @@ -309,11 +320,67 @@ def _resolve_name(name: str, snapshot: Any, turn_id: str | None, result: dict) - _PROSE_WORD = re.compile(r"[^\W\d_]+(?:['’-][^\W\d_]+)*\Z") +_CJK_CHARACTER = re.compile('[㐀-䶿一-鿿豈-﫿' + '぀-ヿ가-힯]') -def _resolve_title(name: str, snapshot: Any, result: dict) -> dict: - candidate = name +def _title_candidates(value: str) -> list[str]: + """Ordered title candidates for a raw mention, most specific first. + + The untrimmed mention is tried first so an exact title that itself + contains sentence punctuation ("@Dr. Smith", "@Note: Intro") is never + truncated to a shorter, different node. Only when no exact title matches + does resolution fall back to punctuation and trailing-prose trimming. + """ + candidates: list[str] = [] + + def add(candidate: str) -> str: + candidate = candidate.strip() + if candidate and candidate not in candidates: + candidates.append(candidate) + return candidate + + add(value) + base = add(_mention_base(value)) + # A mention in running prose can carry trailing words ("Chapter 2.1 + # here"). Only strip an ordinary prose word: a number or label such as + # "@Scene 99 does not exist" may be part of a real title, so resolving + # the shorter node "Scene" would reference the wrong object. + candidate = base while candidate: + head, _, tail = candidate.rpartition(' ') + if not head or not _PROSE_WORD.fullmatch(tail): + break + candidate = add(head) + return candidates + + +def _known_title_prefix(name: str, snapshot: Any) -> str | None: + """Longest known title that starts a mention running into CJK prose. + + Chinese prose has no spaces, so ``用@标题生成视频`` swallows the following + sentence into the mention. Only a CJK continuation enables this fallback; + an ASCII continuation (``@Scene 99``) stays unresolved rather than + referencing the unrelated shorter node ``Scene``. + """ + best = None + for item in _items(snapshot): + title = str(getattr(item, 'title', '') or '') + if not title or len(title) >= len(name) or not name.startswith(title): + continue + if not _CJK_CHARACTER.match(name[len(title):]): + continue + if best is None or len(title) > len(best): + best = title + return best + + +def _resolve_title(name: str, snapshot: Any, result: dict) -> dict: + candidates = _title_candidates(name) + prefix = _known_title_prefix(name, snapshot) + if prefix is not None and prefix not in candidates: + candidates.append(prefix) + for candidate in candidates: nodes = _unique([item for item in _items(snapshot) if str(getattr(item, 'title', '')) == candidate]) if len(nodes) == 1: @@ -321,14 +388,6 @@ def _resolve_title(name: str, snapshot: Any, result: dict) -> dict: if len(nodes) > 1: return {**result, 'target': '@' + candidate, 'status': 'ambiguous', 'candidates': [_node_candidate(item) for item in nodes]} - # A mention in running prose can carry trailing words ("Chapter 2.1 - # here"). Only strip an ordinary prose word: a number or label such as - # "@Scene 99 does not exist" may be part of a real title, so resolving - # the shorter node "Scene" would reference the wrong object. - head, _, tail = candidate.rpartition(' ') - if not head or not _PROSE_WORD.fullmatch(tail): - break - candidate = head return {**result, 'status': 'missing'} diff --git a/src/canvas/AssistantReply.tsx b/src/canvas/AssistantReply.tsx index 647f99907..51f3c3089 100644 --- a/src/canvas/AssistantReply.tsx +++ b/src/canvas/AssistantReply.tsx @@ -2,13 +2,13 @@ import { memo, useMemo, useState } from "react"; import type { ComponentProps } from "react"; import { Streamdown, defaultRehypePlugins } from "streamdown"; import type { Components, LinkSafetyModalProps, StreamdownProps } from "streamdown"; -import type { Element, Root } from "hast"; import type { ChatMessage, ChatTool } from "../backend/chat"; import type { Translate } from "../i18n/I18nProvider.tsx"; import type { MessageKey } from "../i18n/types.ts"; import { CanvasIcon } from "./CanvasIcon"; import { AssistantLinkInfo } from "./AssistantLinkInfo"; -import { canvasLinkClickAction } from "./assistantCanvasLink"; +import { applyCanvasLinkClick } from "./assistantCanvasLink"; +import { prepareCanvasReferences, renderCanvasReferences } from "./canvasReferences.ts"; import { activityProgress, replySections, toolLabel } from "./assistantActivity.ts"; import { assistantToolText } from "./assistantToolText.ts"; import { assistantCitationFormatter } from "./assistantCitations.ts"; @@ -22,38 +22,6 @@ const STATUS: Record = { interrupted: "assistant.reply.status.interrupted", }; -function canvasReferencePlugin({ targets, prepare }: { targets: string[]; prepare: boolean }) { - const placeholders = new Map(targets.map((target, index) => - [target, `https://opendirector.invalid/canvas-reference/${index}`])); - const originals = new Map(Array.from(placeholders, ([target, placeholder]) => [placeholder, target])); - return (tree: Root) => { - const visit = (parent: Root | Element) => { - for (const child of parent.children) { - if (child.type !== "element") continue; - if (child.tagName === "a") { - const href = String(child.properties.href); - if (prepare && placeholders.has(href)) { - child.properties.href = placeholders.get(href); - } else if (!prepare && originals.has(href)) { - child.tagName = "canvas-reference"; - child.properties.href = originals.get(href); - } - } - visit(child); - } - }; - visit(tree); - }; -} - -function prepareCanvasReferences(options: { targets: string[] }) { - return canvasReferencePlugin({ ...options, prepare: true }); -} - -function renderCanvasReferences(options: { targets: string[] }) { - return canvasReferencePlugin({ ...options, prepare: false }); -} - function ToolActivity({ tool, t }: { tool: ChatTool; t: Translate }) { return (
@@ -158,14 +126,10 @@ export const AssistantReply = memo(function AssistantReply({ if (!link) return {children}; return { event.preventDefault(); - const action = canvasLinkClickAction(link); - if ("focus" in action) { - onCanvasLink?.(action.focus); - setCanvasHint(null); - } else { - setCanvasHint(t(action.hintKey) - + (action.projectId ? ` · ${action.projectId}` : "")); - } + const hint = applyCanvasLinkClick(link, (nodeId) => onCanvasLink?.(nodeId)); + setCanvasHint(hint + ? t(hint.hintKey) + (hint.projectId ? ` · ${hint.projectId}` : "") + : null); }}>{children}; }, }; diff --git a/src/canvas/assistantCanvasLink.ts b/src/canvas/assistantCanvasLink.ts index 99b271d18..230e57234 100644 --- a/src/canvas/assistantCanvasLink.ts +++ b/src/canvas/assistantCanvasLink.ts @@ -6,6 +6,8 @@ export type CanvasLinkClick = | { focus: string } | { hintKey: MessageKey; projectId?: string }; +export type CanvasLinkHint = { hintKey: MessageKey; projectId?: string }; + /** Map a resolved link to its click behavior, split out for logic-level tests. */ export function canvasLinkClickAction(link: ChatCanvasLink): CanvasLinkClick { if ((link.status === "focused" || link.status === "hidden") && link.nodeId) { @@ -24,3 +26,19 @@ export function canvasLinkClickAction(link: ChatCanvasLink): CanvasLinkClick { : {}), }; } + +/** + * Apply a click: focus the referenced node, or return the hint to display. + * Split from the React handler so the rendered-reference path can be tested. + */ +export function applyCanvasLinkClick( + link: ChatCanvasLink, + focus: (nodeId: string) => void, +): CanvasLinkHint | null { + const action = canvasLinkClickAction(link); + if ("focus" in action) { + focus(action.focus); + return null; + } + return action; +} diff --git a/src/canvas/canvasReferences.ts b/src/canvas/canvasReferences.ts new file mode 100644 index 000000000..71f704daa --- /dev/null +++ b/src/canvas/canvasReferences.ts @@ -0,0 +1,40 @@ +import type { Element, Root } from "hast"; + +/** + * Rewrite persisted project-reference anchors into a private, sanitizable URL + * before the Markdown sanitizer runs, then restore the original target and + * tag after it. The placeholder is https so a sanitizer that keeps ordinary + * links cannot drop the reference; the reverse pass runs last so no sanitizer + * sees the custom `canvas-reference` element. + */ +function canvasReferencePlugin({ targets, prepare }: { targets: string[]; prepare: boolean }) { + const placeholders = new Map(targets.map((target, index) => + [target, `https://opendirector.invalid/canvas-reference/${index}`])); + const originals = new Map(Array.from(placeholders, ([target, placeholder]) => [placeholder, target])); + return (tree: Root) => { + const visit = (parent: Root | Element) => { + for (const child of parent.children) { + if (child.type !== "element") continue; + if (child.tagName === "a") { + const href = String(child.properties.href); + if (prepare && placeholders.has(href)) { + child.properties.href = placeholders.get(href); + } else if (!prepare && originals.has(href)) { + child.tagName = "canvas-reference"; + child.properties.href = originals.get(href); + } + } + visit(child); + } + }; + visit(tree); + }; +} + +export function prepareCanvasReferences(options: { targets: string[] }) { + return canvasReferencePlugin({ ...options, prepare: true }); +} + +export function renderCanvasReferences(options: { targets: string[] }) { + return canvasReferencePlugin({ ...options, prepare: false }); +} diff --git a/src/i18n/messages/en-US/assistant.ts b/src/i18n/messages/en-US/assistant.ts index 7f8f603aa..f3d067c2e 100644 --- a/src/i18n/messages/en-US/assistant.ts +++ b/src/i18n/messages/en-US/assistant.ts @@ -1,8 +1,8 @@ /** English assistant copy. Keys must match `zh-CN/assistant.ts`. */ export const assistant = { "assistant.reply.sourceUnavailable": "Source unavailable", - "assistant.reply.canvasLink.materialize": "Show on canvas", - "assistant.reply.canvasLink.ambiguous": "This link matches several canvas items. Choose one.", + "assistant.reply.canvasLink.materialize": "This item is not on the canvas yet.", + "assistant.reply.canvasLink.ambiguous": "This link matches several canvas items.", "assistant.reply.canvasLink.missing": "This linked item is not in the project.", "assistant.reply.canvasLink.crossProject": "This link points to another project.", "assistant.reply.copyLink": "Copy link", diff --git a/src/i18n/messages/zh-CN/assistant.ts b/src/i18n/messages/zh-CN/assistant.ts index 3c1614206..828c4388a 100644 --- a/src/i18n/messages/zh-CN/assistant.ts +++ b/src/i18n/messages/zh-CN/assistant.ts @@ -1,8 +1,8 @@ /** Assistant panel, reply activity, pickers, references and chat transport. */ export const assistant = { "assistant.reply.sourceUnavailable": "来源不可用", - "assistant.reply.canvasLink.materialize": "在画布中显示", - "assistant.reply.canvasLink.ambiguous": "该链接匹配多个画布对象,请选择。", + "assistant.reply.canvasLink.materialize": "该对象尚未放置在画布中。", + "assistant.reply.canvasLink.ambiguous": "该链接匹配多个画布对象。", "assistant.reply.canvasLink.missing": "链接的对象不在当前工程中。", "assistant.reply.canvasLink.crossProject": "该链接指向另一个工程。", "assistant.reply.copyLink": "复制链接", diff --git a/tests/canvasLinkMessages.test.ts b/tests/canvasLinkMessages.test.ts index 50085b3b3..97c37de2d 100644 --- a/tests/canvasLinkMessages.test.ts +++ b/tests/canvasLinkMessages.test.ts @@ -49,11 +49,17 @@ test("canvas link messages resolve in both locales without leaking the other lan } }); -test("canvas link outcome copy names the user-visible action", () => { +test("canvas link outcome copy states the condition without promising an action", () => { // Hidden links focus their node (which the workspace reveals); only // outcomes that cannot focus have a hint, so there is no hidden hint key. - assert.equal(translate("en-US", "assistant.reply.canvasLink.materialize"), "Show on canvas"); - assert.equal(translate("zh-CN", "assistant.reply.canvasLink.materialize"), "在画布中显示"); + // Clicking does not materialize an item or open a candidate picker, so the + // copy must not instruct the user to "show" or "choose". + assert.equal(translate("en-US", "assistant.reply.canvasLink.materialize"), + "This item is not on the canvas yet."); + assert.equal(translate("zh-CN", "assistant.reply.canvasLink.materialize"), + "该对象尚未放置在画布中。"); + assert.doesNotMatch(translate("en-US", "assistant.reply.canvasLink.ambiguous"), /choose/i); + assert.doesNotMatch(translate("zh-CN", "assistant.reply.canvasLink.ambiguous"), /请选择/); }); test("a standalone mention renders as a clickable canvas reference", () => { diff --git a/tests/canvasLinkReferences.test.ts b/tests/canvasLinkReferences.test.ts new file mode 100644 index 000000000..72fe59d71 --- /dev/null +++ b/tests/canvasLinkReferences.test.ts @@ -0,0 +1,122 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { prepareCanvasReferences, renderCanvasReferences } from "../src/canvas/canvasReferences.ts"; +import { applyCanvasLinkClick } from "../src/canvas/assistantCanvasLink.ts"; +import type { ChatCanvasLink } from "../src/backend/chat.ts"; + +type HastText = { type: "text"; value: string }; +type HastElement = { + type: "element"; + tagName: string; + properties: Record; + children: HastNode[]; +}; +type HastNode = HastText | HastElement | { type: string }; +type HastRoot = { type: "root"; children: HastNode[] }; + +const nodeId = "11111111-1111-4111-8111-111111111111"; + +function link(overrides: Partial): ChatCanvasLink { + return { + target: `canvas:${nodeId}`, + status: "focused", + nodeId, + assetId: null, + projectId: null, + kind: "text", + action: null, + candidates: null, + ...overrides, + }; +} + +/** What Streamdown's Markdown renderer hands to the rehype chain. */ +function anchorTree(href: string, label = "Finished note"): HastRoot { + return { + type: "root", + children: [{ + type: "element", + tagName: "p", + properties: {}, + children: [{ + type: "element", + tagName: "a", + properties: { href }, + children: [{ type: "text", value: label }], + }], + }], + }; +} + +function anchor(tree: HastRoot): HastElement { + return (tree.children[0] as HastElement).children[0] as HastElement; +} + +function chain(tree: HastRoot, targets: string[]): void { + // Mirrors the production plugin order in AssistantReply: prepare, then the + // third-party sanitize/harden passes (which see only the https placeholder + // asserted below), then render. + prepareCanvasReferences({ targets })(tree as never); + renderCanvasReferences({ targets })(tree as never); +} + +test("a persisted markdown reference becomes a clickable canvas-reference", () => { + const persisted = link({}); + // Use the real message.canvasLinks map the component builds. + const links = new Map([[persisted.target, persisted]]); + const targets = Array.from(links.keys()); + const tree = anchorTree(persisted.target); + + prepareCanvasReferences({ targets })(tree as never); + // The placeholder must be an ordinary https URL so the sanitizer keeps it. + assert.equal(anchor(tree).tagName, "a"); + assert.match(String(anchor(tree).properties.href), + /^https:\/\/opendirector\.invalid\/canvas-reference\/0$/); + + renderCanvasReferences({ targets })(tree as never); + const reference = anchor(tree); + assert.equal(reference.tagName, "canvas-reference"); + assert.equal(reference.properties.href, persisted.target); + assert.deepEqual(reference.children, [{ type: "text", value: "Finished note" }]); + + // The component looks the rendered href back up in the persisted links and + // applies the click action; a focused link must reach the canvas callback. + const focused: string[] = []; + const hint = applyCanvasLinkClick(links.get(String(reference.properties.href))!, (id) => focused.push(id)); + assert.equal(hint, null); + assert.deepEqual(focused, [nodeId]); +}); + +test("a reference target that is not persisted stays an ordinary anchor", () => { + // The component only passes targets it has resolved links for; an anchor + // whose destination is not in message.canvasLinks must not become a + // canvas-reference with a guessed identity. + const links = new Map(); + const targets = Array.from(links.keys()); + const href = `canvas:${nodeId}`; + const tree = anchorTree(href); + + chain(tree, targets); + + const untouched = anchor(tree); + assert.equal(untouched.tagName, "a"); + assert.equal(untouched.properties.href, href); + assert.equal(links.get(String(untouched.properties.href)), undefined); +}); + +test("a materialize reference renders but reports a declarative hint", () => { + const persisted = link({ target: "notes.md", status: "materialize", nodeId: null, + assetId: "22222222-2222-4222-8222-222222222222", action: "materialize" }); + const links = new Map([[persisted.target, persisted]]); + const targets = Array.from(links.keys()); + const tree = anchorTree(persisted.target, "notes.md"); + + chain(tree, targets); + const reference = anchor(tree); + assert.equal(reference.tagName, "canvas-reference"); + + const focused: string[] = []; + const hint = applyCanvasLinkClick(links.get(String(reference.properties.href))!, (id) => focused.push(id)); + assert.deepEqual(focused, []); + assert.deepEqual(hint, { hintKey: "assistant.reply.canvasLink.materialize" }); +}); diff --git a/tests/test_canvas_links.py b/tests/test_canvas_links.py index a5bc78635..b0fed6403 100644 --- a/tests/test_canvas_links.py +++ b/tests/test_canvas_links.py @@ -375,6 +375,67 @@ def test_ascii_word_glued_to_a_cjk_mention_stays_prose(): assert canvas_links_for_text('abc@标题', snapshot(nodes=[item]), uuid4()) == [] +def test_exact_title_wins_before_sentence_punctuation_is_trimmed(): + # "@Dr. Smith" must resolve the full title even when the shorter "Dr" + # exists; trimming first would persist the wrong node id. + short = node(uuid4(), title='Dr') + full = node(uuid4(), title='Dr. Smith') + note = node(uuid4(), title='Note') + intro = node(uuid4(), title='Note: Intro') + project = snapshot(nodes=[short, full, note, intro]) + for text, expected in (('@Dr. Smith', full), ('@Note: Intro', intro)): + link, = canvas_links_for_text(text, project, uuid4()) + assert link['status'] == 'focused' + assert link['target'] == text + assert link['nodeId'] == str(expected.id) + + +@pytest.mark.parametrize(('text', 'title', 'expected_target'), [ + ('参见@标题、然后继续', '标题', '@标题'), + ('“@标题”效果不错', '标题', '@标题'), + ('(@标题)请继续', '标题', '@标题'), + ('@标题…继续', '标题', '@标题'), + ('@标题—继续', '标题', '@标题'), + ('@标题」继续', '标题', '@标题'), + ('@标题】继续', '标题', '@标题'), + ('@Finished note)后续', 'Finished note', '@Finished note'), +]) +def test_fullwidth_punctuation_ends_a_mention_mid_sentence(text, title, expected_target): + item = node(uuid4(), title=title) + link, = canvas_links_for_text(text, snapshot(nodes=[item]), uuid4()) + assert link['status'] == 'focused' + assert link['target'] == expected_target + assert link['nodeId'] == str(item.id) + + +@pytest.mark.parametrize('text', ['用@标题生成视频', '把@标题改一下']) +def test_cjk_prose_after_a_mention_resolves_a_known_title_prefix(text): + # Chinese prose has no word separator, so the mention swallows the rest + # of the phrase; the longest known title prefix still resolves it. + item = node(uuid4(), title='标题') + link, = canvas_links_for_text(text, snapshot(nodes=[item]), uuid4()) + assert link['status'] == 'focused' + assert link['target'] == '@标题' + assert link['nodeId'] == str(item.id) + + +def test_cjk_prefix_fallback_uses_the_longest_known_title(): + short = node(uuid4(), title='标题') + longer = node(uuid4(), title='标题生成') + link, = canvas_links_for_text('用@标题生成视频', snapshot(nodes=[short, longer]), uuid4()) + assert link['target'] == '@标题生成' + assert link['nodeId'] == str(longer.id) + + +def test_ascii_continuation_does_not_use_the_cjk_prefix_fallback(): + # "Scene" is a prefix of "Scene2", but the continuation is not CJK prose; + # silently referencing "Scene" would be the wrong object. + item = node(uuid4(), title='Scene') + link, = canvas_links_for_text('@Scene2', snapshot(nodes=[item]), uuid4()) + assert link['status'] == 'missing' + assert 'nodeId' not in link + + def test_bare_title_name_is_missing_and_only_mentions_resolve_titles(): item = node(uuid4(), kind='group', title='Scene 1') project = snapshot(nodes=[item]) From 0cdee4f4164677dc3c04450035ca1def835b70d8 Mon Sep 17 00:00:00 2001 From: Xingkai98 Date: Tue, 17 Nov 2026 12:46:10 +0800 Subject: [PATCH 09/10] =?UTF-8?q?fix(review-r9):=20=E4=BF=AE=E6=8F=90?= =?UTF-8?q?=E5=8F=8A=E5=9B=9E=E9=80=80=E9=A1=BA=E5=BA=8F=E4=B8=8E=20missin?= =?UTF-8?q?g=20=E7=9B=AE=E6=A0=87=E8=BE=B9=E7=95=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 先删尾部普通词再做标点截断,@Dr. Smith says hello 不再退回 @Dr - 标点前缀按长度从长到短尝试,@Dr. Smith. 命中完整标题 - missing 提及的目标按首个边界截断,避免前端把后续句子渲染成链接 - 补对应回归用例 Co-Authored-By: Claude Code --- .../src/opendirector/agent/canvas_links.py | 89 ++++++++++++++----- tests/test_canvas_links.py | 23 ++++- 2 files changed, 87 insertions(+), 25 deletions(-) diff --git a/packages/core/src/opendirector/agent/canvas_links.py b/packages/core/src/opendirector/agent/canvas_links.py index 43711e8b3..c20a14ee1 100644 --- a/packages/core/src/opendirector/agent/canvas_links.py +++ b/packages/core/src/opendirector/agent/canvas_links.py @@ -45,6 +45,18 @@ def link_targets(text: str) -> list[str]: _MENTION_END_AFTER_SPACE = '.:"\'\uff1a' +def _boundary_indexes(value: str) -> list[int]: + """Indexes where sentence punctuation ends a mention.""" + indexes = [] + for index, char in enumerate(value): + if char in _MENTION_END_ANY: + indexes.append(index) + elif char in _MENTION_END_AFTER_SPACE and ( + index + 1 == len(value) or value[index + 1].isspace()): + indexes.append(index) + return indexes + + def _mention_base(value: str) -> str: """Trim a mention at the first sentence punctuation that ends it. @@ -55,13 +67,8 @@ def _mention_base(value: str) -> str: mention wherever it appears. """ value = value.strip() - for index, char in enumerate(value): - if char in _MENTION_END_ANY: - return value[:index].rstrip() - if char in _MENTION_END_AFTER_SPACE and ( - index + 1 == len(value) or value[index + 1].isspace()): - return value[:index].rstrip() - return value.rstrip() + indexes = _boundary_indexes(value) + return value[:indexes[0]].rstrip() if indexes else value.rstrip() def _mention_targets(text: str) -> list[str]: @@ -324,34 +331,65 @@ def _resolve_name(name: str, snapshot: Any, turn_id: str | None, result: dict) - '぀-ヿ가-힯]') +def _prose_drops(value: str) -> list[str]: + """Trailing ordinary prose words removed one at a time, longest first. + + A number or label such as "@Scene 99 does not exist" may be part of a real + title, so only an ordinary word is dropped; resolving the shorter node + "Scene" would reference the wrong object. + """ + drops = [] + candidate = value + while candidate: + head, _, tail = candidate.rpartition(' ') + if not head or not _PROSE_WORD.fullmatch(tail): + break + candidate = head + drops.append(candidate) + return drops + + +def _punctuation_prefixes(value: str) -> list[str]: + """Mention prefixes cut at every sentence-punctuation boundary, longest first. + + Trying the longest first keeps an internal punctuation mark that belongs + to the title: ``@Dr. Smith.`` must try ``Dr. Smith`` before ``Dr``. + """ + value = value.strip() + prefixes: list[str] = [] + for index in _boundary_indexes(value): + prefix = value[:index].rstrip() + if prefix and prefix not in prefixes: + prefixes.append(prefix) + return sorted(prefixes, key=len, reverse=True) + + def _title_candidates(value: str) -> list[str]: """Ordered title candidates for a raw mention, most specific first. The untrimmed mention is tried first so an exact title that itself contains sentence punctuation ("@Dr. Smith", "@Note: Intro") is never - truncated to a shorter, different node. Only when no exact title matches - does resolution fall back to punctuation and trailing-prose trimming. + truncated to a shorter, different node. Trailing prose is dropped before + punctuation trimming: in "@Dr. Smith says hello" the period after "Dr" is + part of the title, so the full "Dr. Smith" candidate must be tried before + the shorter "Dr" boundary prefix. """ candidates: list[str] = [] - def add(candidate: str) -> str: + def add(candidate: str) -> None: candidate = candidate.strip() if candidate and candidate not in candidates: candidates.append(candidate) - return candidate add(value) - base = add(_mention_base(value)) - # A mention in running prose can carry trailing words ("Chapter 2.1 - # here"). Only strip an ordinary prose word: a number or label such as - # "@Scene 99 does not exist" may be part of a real title, so resolving - # the shorter node "Scene" would reference the wrong object. - candidate = base - while candidate: - head, _, tail = candidate.rpartition(' ') - if not head or not _PROSE_WORD.fullmatch(tail): - break - candidate = add(head) + for candidate in _prose_drops(value): + add(candidate) + prefixes = _punctuation_prefixes(value) + for prefix in prefixes: + add(prefix) + for prefix in prefixes: + for candidate in _prose_drops(prefix): + add(candidate) return candidates @@ -388,7 +426,12 @@ def _resolve_title(name: str, snapshot: Any, result: dict) -> dict: if len(nodes) > 1: return {**result, 'target': '@' + candidate, 'status': 'ambiguous', 'candidates': [_node_candidate(item) for item in nodes]} - return {**result, 'status': 'missing'} + # Keep the persisted target on the mention itself so the renderer never + # turns the rest of the sentence ("@No such item. Next step") into one + # clickable reference. A raw mention with no boundary keeps its full text. + base = _mention_base(name) + return {**result, 'target': '@' + base if base else result['target'], + 'status': 'missing'} def resolve_canvas_link(target: str, snapshot: Any, turn_id: str | None = None) -> dict: diff --git a/tests/test_canvas_links.py b/tests/test_canvas_links.py index b0fed6403..82f669bb9 100644 --- a/tests/test_canvas_links.py +++ b/tests/test_canvas_links.py @@ -383,13 +383,32 @@ def test_exact_title_wins_before_sentence_punctuation_is_trimmed(): note = node(uuid4(), title='Note') intro = node(uuid4(), title='Note: Intro') project = snapshot(nodes=[short, full, note, intro]) - for text, expected in (('@Dr. Smith', full), ('@Note: Intro', intro)): + for text, expected, target in ( + ('@Dr. Smith', full, '@Dr. Smith'), + ('@Dr. Smith.', full, '@Dr. Smith'), + ('@Dr. Smith says hello', full, '@Dr. Smith'), + ('@Note: Intro', intro, '@Note: Intro'), + ('@Note: Intro details', intro, '@Note: Intro')): link, = canvas_links_for_text(text, project, uuid4()) assert link['status'] == 'focused' - assert link['target'] == text + assert link['target'] == target assert link['nodeId'] == str(expected.id) +def test_missing_mentions_keep_the_target_off_following_prose(): + # A missing mention must not wrap the rest of the sentence into the + # clickable reference; the persisted target stops at the mention boundary. + project = snapshot() + for text, target in ( + ('@No such item. Next step', '@No such item'), + ('@No such item.', '@No such item'), + ('@Unknown 标题、继续', '@Unknown 标题')): + link, = canvas_links_for_text(text, project, uuid4()) + assert link['status'] == 'missing' + assert link['target'] == target + assert 'nodeId' not in link + + @pytest.mark.parametrize(('text', 'title', 'expected_target'), [ ('参见@标题、然后继续', '标题', '@标题'), ('“@标题”效果不错', '标题', '@标题'), From 2912375a94ae5c8fcaf2bfeb52f58d705ef994c7 Mon Sep 17 00:00:00 2001 From: Xingkai98 Date: Tue, 17 Nov 2026 12:51:58 +0800 Subject: [PATCH 10/10] =?UTF-8?q?fix(review-r9b):=20=E6=8C=89=E5=B7=B2?= =?UTF-8?q?=E7=9F=A5=E6=A0=87=E9=A2=98=E6=9C=80=E9=95=BF=E5=89=8D=E7=BC=80?= =?UTF-8?q?=E4=B8=8E=E8=BE=B9=E7=95=8C=E8=A7=A3=E6=9E=90=E6=8F=90=E5=8F=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 用快照中的已知标题做最长前缀匹配,替代标点/空格候选列表 - 边界校验:精确、句末标点、全空白后纯普通词、或紧邻 CJK - 更长但边界不可用的已知标题会遮蔽短标题,避免 @Dr. Smith 99 回退到 Dr、@标题生成 99 回退到 标题 - 修复 CJK 紧邻完整标题时被短前缀抢先解析的问题 - 补对应回归用例 Co-Authored-By: Claude Code --- .../src/opendirector/agent/canvas_links.py | 135 ++++++------------ tests/test_canvas_links.py | 24 ++++ 2 files changed, 69 insertions(+), 90 deletions(-) diff --git a/packages/core/src/opendirector/agent/canvas_links.py b/packages/core/src/opendirector/agent/canvas_links.py index c20a14ee1..3ac41abe6 100644 --- a/packages/core/src/opendirector/agent/canvas_links.py +++ b/packages/core/src/opendirector/agent/canvas_links.py @@ -331,101 +331,56 @@ def _resolve_name(name: str, snapshot: Any, turn_id: str | None, result: dict) - '぀-ヿ가-힯]') -def _prose_drops(value: str) -> list[str]: - """Trailing ordinary prose words removed one at a time, longest first. - - A number or label such as "@Scene 99 does not exist" may be part of a real - title, so only an ordinary word is dropped; resolving the shorter node - "Scene" would reference the wrong object. - """ - drops = [] - candidate = value - while candidate: - head, _, tail = candidate.rpartition(' ') - if not head or not _PROSE_WORD.fullmatch(tail): - break - candidate = head - drops.append(candidate) - return drops - - -def _punctuation_prefixes(value: str) -> list[str]: - """Mention prefixes cut at every sentence-punctuation boundary, longest first. - - Trying the longest first keeps an internal punctuation mark that belongs - to the title: ``@Dr. Smith.`` must try ``Dr. Smith`` before ``Dr``. - """ - value = value.strip() - prefixes: list[str] = [] - for index in _boundary_indexes(value): - prefix = value[:index].rstrip() - if prefix and prefix not in prefixes: - prefixes.append(prefix) - return sorted(prefixes, key=len, reverse=True) - - -def _title_candidates(value: str) -> list[str]: - """Ordered title candidates for a raw mention, most specific first. - - The untrimmed mention is tried first so an exact title that itself - contains sentence punctuation ("@Dr. Smith", "@Note: Intro") is never - truncated to a shorter, different node. Trailing prose is dropped before - punctuation trimming: in "@Dr. Smith says hello" the period after "Dr" is - part of the title, so the full "Dr. Smith" candidate must be tried before - the shorter "Dr" boundary prefix. - """ - candidates: list[str] = [] - - def add(candidate: str) -> None: - candidate = candidate.strip() - if candidate and candidate not in candidates: - candidates.append(candidate) - - add(value) - for candidate in _prose_drops(value): - add(candidate) - prefixes = _punctuation_prefixes(value) - for prefix in prefixes: - add(prefix) - for prefix in prefixes: - for candidate in _prose_drops(prefix): - add(candidate) - return candidates - - -def _known_title_prefix(name: str, snapshot: Any) -> str | None: - """Longest known title that starts a mention running into CJK prose. - - Chinese prose has no spaces, so ``用@标题生成视频`` swallows the following - sentence into the mention. Only a CJK continuation enables this fallback; - an ASCII continuation (``@Scene 99``) stays unresolved rather than - referencing the unrelated shorter node ``Scene``. - """ - best = None +def _known_titles(snapshot: Any) -> list[str]: + titles: list[str] = [] for item in _items(snapshot): title = str(getattr(item, 'title', '') or '') - if not title or len(title) >= len(name) or not name.startswith(title): - continue - if not _CJK_CHARACTER.match(name[len(title):]): - continue - if best is None or len(title) > len(best): - best = title - return best + if title and title not in titles: + titles.append(title) + return titles + + +def _title_prefix_accepts(remainder: str) -> bool: + """Whether the text after a known title still lets the mention end there. + + An exact match has no remainder. Sentence punctuation ends the mention; + an ASCII period/colon must be followed by whitespace or the end, so the + internal period of ``Chapter 2.1`` is not a boundary. Trailing prose words + ("Dr. Smith says hello") are allowed, but a number or label is not + ("@Scene 99" may be part of a longer title, so it must not reference the + unrelated shorter node "Scene"). Glued CJK prose ("@标题生成视频") is + allowed because Chinese has no word separator. + """ + if not remainder: + return True + first = remainder[0] + if first in _MENTION_END_ANY: + return True + if first in _MENTION_END_AFTER_SPACE: + return len(remainder) == 1 or remainder[1].isspace() + if first.isspace(): + rest = remainder.strip() + return bool(rest) and all( + _PROSE_WORD.fullmatch(word) for word in rest.split()) + return bool(_CJK_CHARACTER.match(remainder)) def _resolve_title(name: str, snapshot: Any, result: dict) -> dict: - candidates = _title_candidates(name) - prefix = _known_title_prefix(name, snapshot) - if prefix is not None and prefix not in candidates: - candidates.append(prefix) - for candidate in candidates: - nodes = _unique([item for item in _items(snapshot) - if str(getattr(item, 'title', '')) == candidate]) - if len(nodes) == 1: - return _resolved_node(nodes[0], snapshot, {**result, 'target': '@' + candidate}) - if len(nodes) > 1: - return {**result, 'target': '@' + candidate, 'status': 'ambiguous', - 'candidates': [_node_candidate(item) for item in nodes]} + # Choose the longest known title that prefixes the mention, then require a + # clean mention boundary after it. A longer unusable prefix shadows every + # shorter one: resolving "Dr" for "@Dr. Smith 99" would reference a + # different object, so the safer outcome is "missing". + prefixes = [title for title in _known_titles(snapshot) if name.startswith(title)] + if prefixes: + title = max(prefixes, key=len) + if _title_prefix_accepts(name[len(title):]): + nodes = _unique([item for item in _items(snapshot) + if str(getattr(item, 'title', '')) == title]) + if len(nodes) == 1: + return _resolved_node(nodes[0], snapshot, {**result, 'target': '@' + title}) + if len(nodes) > 1: + return {**result, 'target': '@' + title, 'status': 'ambiguous', + 'candidates': [_node_candidate(item) for item in nodes]} # Keep the persisted target on the mention itself so the renderer never # turns the rest of the sentence ("@No such item. Next step") into one # clickable reference. A raw mention with no boundary keeps its full text. diff --git a/tests/test_canvas_links.py b/tests/test_canvas_links.py index 82f669bb9..99f40f15c 100644 --- a/tests/test_canvas_links.py +++ b/tests/test_canvas_links.py @@ -395,6 +395,30 @@ def test_exact_title_wins_before_sentence_punctuation_is_trimmed(): assert link['nodeId'] == str(expected.id) +def test_glued_cjk_prose_after_a_full_title_resolves_the_full_title(): + # The CJK prefix fallback must prefer the longer known title even though + # the shorter one is also a prefix. + short = node(uuid4(), title='Dr') + full = node(uuid4(), title='Dr. Smith') + link, = canvas_links_for_text('(@Dr. Smith后续', + snapshot(nodes=[short, full]), uuid4()) + assert link['target'] == '@Dr. Smith' + assert link['nodeId'] == str(full.id) + + +def test_a_longer_known_title_with_an_unusable_boundary_shadows_shorter_nodes(): + # The longer title exists, but the mention continues with a number that + # may belong to a longer title. Resolving "Dr" / "标题" would reference a + # different object, so the mention stays missing. + nodes = [node(uuid4(), title='Dr'), node(uuid4(), title='Dr. Smith'), + node(uuid4(), title='标题'), node(uuid4(), title='标题生成')] + project = snapshot(nodes=nodes) + for text in ('@Dr. Smith 99', '@标题生成 99'): + link, = canvas_links_for_text(text, project, uuid4()) + assert link['status'] == 'missing' + assert 'nodeId' not in link + + def test_missing_mentions_keep_the_target_off_following_prose(): # A missing mention must not wrap the rest of the sentence into the # clickable reference; the persisted target stops at the mention boundary.