fix(agent-threads): hide prompt reference directives in thread titles
Add a shared visiblePromptText helper and strip reference tokens, image-generator directives, and model-selection lines from agent thread summaries and the assistant panel header, so titles show the user's actual prompt instead of internal directives. Covered by a server test. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -75,6 +75,22 @@ export function serializePromptImageReference(reference: Pick<PromptImageReferen
|
|||||||
return `[@image:#${fallbackIndex}:${name}:${reference.url}]`;
|
return `[@image:#${fallbackIndex}:${name}:${reference.url}]`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function visiblePromptText(content: string) {
|
||||||
|
const text = parsePromptImageReferences(content)
|
||||||
|
.map((part) => (part.type === "text" ? part.text : ""))
|
||||||
|
.join("");
|
||||||
|
return text
|
||||||
|
.split("\n")
|
||||||
|
.map((line) => line.trim())
|
||||||
|
.filter((line) => {
|
||||||
|
const lower = line.toLowerCase();
|
||||||
|
return line && !lower.startsWith("image generator target node:") && !lower.startsWith("selected model:") && !line.startsWith("模型选择");
|
||||||
|
})
|
||||||
|
.join(" ")
|
||||||
|
.replace(/\s{2,}/g, " ")
|
||||||
|
.trim();
|
||||||
|
}
|
||||||
|
|
||||||
function collectReferenceMatches(content: string) {
|
function collectReferenceMatches(content: string) {
|
||||||
const matches: ReferenceMatch[] = [];
|
const matches: ReferenceMatch[] = [];
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import { loadMotevaFontCatalog } from "@/ui/components/TextStyleToolbar/fontCata
|
|||||||
import { WorkspaceTitle } from "@/ui/components/WorkspaceTitle";
|
import { WorkspaceTitle } from "@/ui/components/WorkspaceTitle";
|
||||||
import { autoImageAdjustments, defaultImageAdjustments, parseImageAdjustments, serializeImageAdjustments } from "@/ui/lib/imageAdjustments";
|
import { autoImageAdjustments, defaultImageAdjustments, parseImageAdjustments, serializeImageAdjustments } from "@/ui/lib/imageAdjustments";
|
||||||
import { canvasImageUrl } from "@/ui/lib/imageDelivery";
|
import { canvasImageUrl } from "@/ui/lib/imageDelivery";
|
||||||
|
import { visiblePromptText } from "@/ui/lib/promptImageReferences";
|
||||||
import { isImageGeneratorThreadMessage, visibleAgentMessages } from "./canvas/agentMessageFilters";
|
import { isImageGeneratorThreadMessage, visibleAgentMessages } from "./canvas/agentMessageFilters";
|
||||||
import { CanvasColorPopover } from "./canvas/CanvasColorPopover";
|
import { CanvasColorPopover } from "./canvas/CanvasColorPopover";
|
||||||
import { buildAlignmentGuides, clamp, minGroupNodeScale, nodeBounds, nodeIntersectsRect, normalizeScreenRect, pointInsideNode, scaleGroupNode, screenBoundsForNodes, screenRectToWorldRect, type AlignmentGuide, type NodeAlignment, type NodeDistribution, type NodeTransform, type ResizeHandle, type SelectionBox } from "@/ui/pages/CanvasWorkspace/canvas/canvasGeometry";
|
import { buildAlignmentGuides, clamp, minGroupNodeScale, nodeBounds, nodeIntersectsRect, normalizeScreenRect, pointInsideNode, scaleGroupNode, screenBoundsForNodes, screenRectToWorldRect, type AlignmentGuide, type NodeAlignment, type NodeDistribution, type NodeTransform, type ResizeHandle, type SelectionBox } from "@/ui/pages/CanvasWorkspace/canvas/canvasGeometry";
|
||||||
@@ -3756,7 +3757,7 @@ export function CanvasWorkspace({ projectId }: { projectId: string }) {
|
|||||||
<aside className="assistant-panel">
|
<aside className="assistant-panel">
|
||||||
<div className="assistant-header">
|
<div className="assistant-header">
|
||||||
<div className="assistant-header-title">
|
<div className="assistant-header-title">
|
||||||
<strong>{project.brief || project.title || t("canvasUntitled")}</strong>
|
<strong>{agentHeaderTitle(messages, project, t("canvasUntitled"))}</strong>
|
||||||
</div>
|
</div>
|
||||||
<div className="assistant-header-actions">
|
<div className="assistant-header-actions">
|
||||||
<button className="assistant-header-icon" type="button" aria-label={t("newChat")} title={t("newChat")} onClick={beginNewAgentConversation} disabled={!hasCurrentConversation}>
|
<button className="assistant-header-icon" type="button" aria-label={t("newChat")} title={t("newChat")} onClick={beginNewAgentConversation} disabled={!hasCurrentConversation}>
|
||||||
@@ -5289,7 +5290,7 @@ function fallbackAgentThreads(project: Project | null, hiddenThreadIds: Readonly
|
|||||||
}
|
}
|
||||||
|
|
||||||
function agentThreadTitle(thread: AgentThreadSummary) {
|
function agentThreadTitle(thread: AgentThreadSummary) {
|
||||||
return thread.text.trim() || "新对话";
|
return visiblePromptText(thread.text) || "新对话";
|
||||||
}
|
}
|
||||||
|
|
||||||
function latestAgentThreadId(project: Project | null, hiddenThreadIds: ReadonlySet<string>) {
|
function latestAgentThreadId(project: Project | null, hiddenThreadIds: ReadonlySet<string>) {
|
||||||
@@ -5298,14 +5299,14 @@ function latestAgentThreadId(project: Project | null, hiddenThreadIds: ReadonlyS
|
|||||||
}
|
}
|
||||||
|
|
||||||
function agentThreadTitleText(content: string) {
|
function agentThreadTitleText(content: string) {
|
||||||
const lines = content
|
return visiblePromptText(content);
|
||||||
.split("\n")
|
}
|
||||||
.map((line) => line.trim())
|
|
||||||
.filter((line) => {
|
function agentHeaderTitle(messages: AgentMessage[], project: Project, fallback: string) {
|
||||||
const lower = line.toLowerCase();
|
const firstUserMessage = messages.find((message) => message.role === "user");
|
||||||
return line && !lower.startsWith("image generator target node:") && !lower.startsWith("selected model:") && !line.startsWith("模型选择");
|
const firstUserText = firstUserMessage ? visiblePromptText(firstUserMessage.content) : "";
|
||||||
});
|
if (firstUserText) return firstUserText;
|
||||||
return lines.join(" ").trim() || content.trim();
|
return visiblePromptText(project.brief) || project.title.trim() || fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isProjectGenerating(project: Project) {
|
function isProjectGenerating(project: Project) {
|
||||||
|
|||||||
@@ -44,6 +44,44 @@ func TestAgentThreadsOnlyListsAgentConversationThreads(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAgentThreadsSummaryHidesPromptReferenceDirectives(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
repo := memory.NewProjectRepository()
|
||||||
|
service := NewDesignService(repo, nil, nil, nil, nil)
|
||||||
|
now := time.Now()
|
||||||
|
project := design.Project{
|
||||||
|
ID: "project-agent-reference-history",
|
||||||
|
Title: "Agent history",
|
||||||
|
Status: design.StatusReady,
|
||||||
|
LastThreadID: "thread-agent",
|
||||||
|
UpdatedAt: now,
|
||||||
|
Messages: []design.Message{
|
||||||
|
{
|
||||||
|
ID: "agent-user",
|
||||||
|
Role: "user",
|
||||||
|
Type: "user",
|
||||||
|
Content: "Reference image 1 (image): http://localhost:19000/canvas-assets/reference.png 灰裤子拆成单件衣服,为了后面电商配图。\nSelected model: GPT Image 2",
|
||||||
|
ThreadID: "thread-agent",
|
||||||
|
CreatedAt: now,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if err := repo.Save(ctx, project); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
threads, err := service.AgentThreads(ctx, project.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(threads) != 1 {
|
||||||
|
t.Fatalf("expected one agent thread, got %#v", threads)
|
||||||
|
}
|
||||||
|
if threads[0].Text != "灰裤子拆成单件衣服,为了后面电商配图。" {
|
||||||
|
t.Fatalf("expected visible prompt text only, got %q", threads[0].Text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestProjectScopedToAgentThreadKeepsOnlyCurrentConversationContext(t *testing.T) {
|
func TestProjectScopedToAgentThreadKeepsOnlyCurrentConversationContext(t *testing.T) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
project := design.Project{
|
project := design.Project{
|
||||||
|
|||||||
@@ -824,6 +824,7 @@ func isNonAgentConversationMessage(message design.Message) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func agentThreadSummaryText(content string) string {
|
func agentThreadSummaryText(content string) string {
|
||||||
|
content = stripPromptReferenceDirectives(content)
|
||||||
lines := strings.Split(content, "\n")
|
lines := strings.Split(content, "\n")
|
||||||
kept := make([]string, 0, len(lines))
|
kept := make([]string, 0, len(lines))
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
@@ -837,7 +838,7 @@ func agentThreadSummaryText(content string) string {
|
|||||||
if text := strings.TrimSpace(strings.Join(kept, " ")); text != "" {
|
if text := strings.TrimSpace(strings.Join(kept, " ")); text != "" {
|
||||||
return text
|
return text
|
||||||
}
|
}
|
||||||
return strings.TrimSpace(content)
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func projectScopedToAgentThread(project design.Project, threadID string) design.Project {
|
func projectScopedToAgentThread(project design.Project, threadID string) design.Project {
|
||||||
@@ -4530,7 +4531,7 @@ func hasUserPromptTextInAgentMessages(messages []design.AgentChatMessage) bool {
|
|||||||
if strings.EqualFold(strings.TrimSpace(content.TextSource), "settings") {
|
if strings.EqualFold(strings.TrimSpace(content.TextSource), "settings") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
text := stripPromptReferenceDirectiveLines(stripImageGeneratorDirectiveLines(content.Text))
|
text := stripPromptReferenceDirectives(stripImageGeneratorDirectiveLines(content.Text))
|
||||||
if design.NormalizePrompt(text) != "" {
|
if design.NormalizePrompt(text) != "" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -4551,12 +4552,14 @@ func stripImageGeneratorDirectiveLines(text string) string {
|
|||||||
return strings.TrimSpace(strings.Join(filtered, "\n"))
|
return strings.TrimSpace(strings.Join(filtered, "\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func stripPromptReferenceDirectiveLines(text string) string {
|
func stripPromptReferenceDirectives(text string) string {
|
||||||
|
text = promptImageTokenPattern.ReplaceAllString(text, "")
|
||||||
|
text = promptImageDirectivePattern.ReplaceAllString(text, "")
|
||||||
lines := strings.Split(text, "\n")
|
lines := strings.Split(text, "\n")
|
||||||
filtered := make([]string, 0, len(lines))
|
filtered := make([]string, 0, len(lines))
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
trimmed := strings.TrimSpace(strings.ReplaceAll(line, "\u00a0", " "))
|
trimmed := strings.TrimSpace(strings.ReplaceAll(line, "\u00a0", " "))
|
||||||
if isPromptReferenceDirectiveLine(trimmed) {
|
if trimmed == "" || isPromptReferenceDirectiveLine(trimmed) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
filtered = append(filtered, line)
|
filtered = append(filtered, line)
|
||||||
|
|||||||
Reference in New Issue
Block a user