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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion apps/api/src/opendirector_api/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
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.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
Expand Down Expand Up @@ -456,12 +458,24 @@
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 = [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.
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})
Expand Down
446 changes: 446 additions & 0 deletions packages/core/src/opendirector/agent/canvas_links.py

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions packages/core/src/opendirector/agent/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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':
Expand Down
27 changes: 27 additions & 0 deletions packages/core/src/opendirector/domain/creation/chat.py
Original file line number Diff line number Diff line change
@@ -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.')
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
57 changes: 57 additions & 0 deletions scripts/verify_canvas_link_mutations.py
Original file line number Diff line number Diff line change
@@ -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', '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()',
'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)
4 changes: 4 additions & 0 deletions src/backend/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -60,7 +61,10 @@ export interface ChatSource {
url: string;
title: string;
}
/** A resolved canvas reference persisted with an assistant reply. */
export type { ChatCanvasLink, ChatCanvasLinkCandidate };
export interface ChatMessage {
canvasLinks?: ChatCanvasLink[];
sources?: ChatSource[];
plan?: ChatPlanEntry[];
runtime?: AgentRuntime;
Expand Down
4 changes: 3 additions & 1 deletion src/canvas/AssistantPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export function AssistantPanel({
onModelSelections,
onOpenSettings,
onPrepareReferences,
onFocusCanvasNode,
}: {
documentId: string;
documentTitle: string;
Expand All @@ -84,6 +85,7 @@ export function AssistantPanel({
onModelSelections?: (selections: ModelSelections) => void;
onOpenSettings: () => void;
onPrepareReferences: () => Promise<boolean>;
onFocusCanvasNode?: (nodeId: string) => void;
}) {
const t = useT();
const chat = useAssistantChat(documentId, catalog, onTurnEnd, t, open);
Expand Down Expand Up @@ -434,7 +436,7 @@ export function AssistantPanel({
)}
<div className="cw-assistant-message-text">
{message.role === "assistant" ? (
<AssistantReply message={message} thinking={chat.thinkingNow} t={t} />
<AssistantReply message={message} thinking={chat.thinkingNow} t={t} onCanvasLink={onFocusCanvasNode} />
) : message.role === "user" &&
(message.content || message.assets?.length) ? (
referenceContent(
Expand Down
38 changes: 36 additions & 2 deletions src/canvas/AssistantReply.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { memo, useMemo, useState } from "react";
import { Streamdown } from "streamdown";
import type { LinkSafetyModalProps } from "streamdown";
import type { ComponentProps } from "react";
import { Streamdown, defaultRehypePlugins } from "streamdown";
import type { Components, LinkSafetyModalProps, StreamdownProps } from "streamdown";
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 { 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";
import { renderCanvasMentions } from "./assistantCanvasMentions.ts";
import { defaultTranslate } from "./assistantTranslate.ts";

const STATUS: Record<ChatTool["status"], MessageKey> = {
Expand Down Expand Up @@ -94,21 +98,48 @@ 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;
}) {
const [copyState, setCopyState] = useState<"idle" | "copied" | "failed">("idle");
const [canvasHint, setCanvasHint] = useState<string | null>(null);
const formatText = useMemo(() => assistantCitationFormatter(
message.text, message.sources, message.status === "streaming", t,
), [message.text, message.sources, message.status, t]);
const linkSafety = useMemo(() => ({
enabled: true,
renderModal: (props: LinkSafetyModalProps) => props.isOpen ? <AssistantLinkInfo {...props} t={t} /> : 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
// keep Streamdown's sanitize and link-safety behavior.
const components: Components = {
"canvas-reference": ({ href, children }: ComponentProps<"a">) => {
const link = links.get(String(href));
if (!link) return <span>{children}</span>;
return <a href="#" onClick={(event) => {
event.preventDefault();
const hint = applyCanvasLinkClick(link, (nodeId) => onCanvasLink?.(nodeId));
setCanvasHint(hint
? t(hint.hintKey) + (hint.projectId ? ` · ${hint.projectId}` : "")
: null);
}}>{children}</a>;
},
};
const targets = Array.from(links.keys());
const rehypePlugins: StreamdownProps["rehypePlugins"] = [defaultRehypePlugins.raw,
[prepareCanvasReferences, { targets }], defaultRehypePlugins.sanitize,
defaultRehypePlugins.harden, [renderCanvasReferences, { targets }],
[renderCanvasMentions, { targets }]];
return { components, rehypePlugins };
}, [message.canvasLinks, onCanvasLink, t]);
const copyLabel =
copyState === "copied"
? t("assistant.reply.copied")
Expand All @@ -124,6 +155,8 @@ export const AssistantReply = memo(function AssistantReply({
mode={message.status === "streaming" ? "streaming" : "static"}
isAnimating={message.status === "streaming"}
linkSafety={linkSafety}
components={projectLinks.components}
rehypePlugins={projectLinks.rehypePlugins}
>
{formatText(text)}
</Streamdown>
Expand All @@ -134,6 +167,7 @@ export const AssistantReply = memo(function AssistantReply({
: <ToolGroup key={part.id} tools={part.tools} t={t} />;
return (
<>
{canvasHint && <p role="status">{canvasHint}</p>}
{!!message.plan?.length && <details className="cw-chat-plan cw-chat-activity">
<summary>
<CanvasIcon name="sparkle" size={15} />
Expand Down
44 changes: 44 additions & 0 deletions src/canvas/assistantCanvasLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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 };

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) {
return { focus: link.nodeId };
}
const keys: Record<string, MessageKey> = {
"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 }
: {}),
};
}

/**
* 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;
}
Loading
Loading