Skip to content

Commit 83fc1f9

Browse files
authored
fix(canvas): normalize forwarded agent prompts
Generated-By: PostHog Code Task-Id: 87a79e23-20c4-440b-818b-28154924ffbc
1 parent 8d4d42e commit 83fc1f9

4 files changed

Lines changed: 51 additions & 1 deletion

File tree

packages/core/src/canvas/threadTimeline.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
buildThreadTimeline,
44
deriveThreadAgentStatus,
55
hasAgentMention,
6+
normalizeAgentPromptText,
67
shouldSuspendThreadSession,
78
} from "./threadTimeline";
89

@@ -18,6 +19,24 @@ describe("hasAgentMention", () => {
1819
});
1920
});
2021

22+
describe("normalizeAgentPromptText", () => {
23+
it.each([
24+
[
25+
"forwarded thread comment",
26+
"[Thread comment from Peter Kirkham] @agent which model are you?",
27+
"which model are you?",
28+
],
29+
["direct prompt", "which model are you?", "which model are you?"],
30+
[
31+
"direct prompt with mention",
32+
"@agent which model are you?",
33+
"which model are you?",
34+
],
35+
])("normalizes a %s", (_name, content, expected) => {
36+
expect(normalizeAgentPromptText(content)).toBe(expected);
37+
});
38+
});
39+
2140
describe("buildThreadTimeline", () => {
2241
it("interleaves prompts, human replies, and agent turns chronologically", () => {
2342
const timeline = buildThreadTimeline({

packages/core/src/canvas/threadTimeline.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,22 @@ export interface ThreadAgentStatus {
6969
}
7070

7171
const AGENT_MENTION_PATTERN = /(^|\s)@agent\b/i;
72+
const THREAD_COMMENT_ATTRIBUTION_PATTERN =
73+
/^\[Thread comment from [^\]\r\n]+\]\s*/i;
74+
const LEADING_AGENT_MENTION_PATTERN = /^@agent\b[\s:]*/i;
7275

7376
export function hasAgentMention(content: string): boolean {
7477
return AGENT_MENTION_PATTERN.test(content);
7578
}
7679

80+
export function normalizeAgentPromptText(content: string): string {
81+
return content
82+
.trim()
83+
.replace(THREAD_COMMENT_ATTRIBUTION_PATTERN, "")
84+
.replace(LEADING_AGENT_MENTION_PATTERN, "")
85+
.trim();
86+
}
87+
7788
export function deriveThreadAgentStatus({
7889
hasActivity = false,
7990
hasError = false,

packages/ui/src/features/canvas/components/ThreadPanel.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,21 @@ describe("UserPromptRow", () => {
3030
expect(screen.getByText("@agent")).toBeInTheDocument();
3131
expect(screen.getByText("Investigate this")).toBeInTheDocument();
3232
});
33+
34+
it("hides forwarded thread attribution and duplicate agent mentions", () => {
35+
render(
36+
<UserPromptRow
37+
message={{
38+
id: "prompt",
39+
text: "[Thread comment from Peter Kirkham] @agent which model are you?",
40+
timestamp: 1,
41+
}}
42+
author={{ id: 1, uuid: "user", email: "user@example.com" }}
43+
/>,
44+
);
45+
46+
expect(screen.getAllByText("@agent")).toHaveLength(1);
47+
expect(screen.getByText("which model are you?")).toBeInTheDocument();
48+
expect(screen.queryByText(/Thread comment from/)).not.toBeInTheDocument();
49+
});
3350
});

packages/ui/src/features/canvas/components/ThreadPanel.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
buildThreadTimeline,
1212
deriveThreadAgentStatus,
1313
hasAgentMention,
14+
normalizeAgentPromptText,
1415
shouldSuspendThreadSession,
1516
type ThreadAgentMessage,
1617
type ThreadAgentStatus,
@@ -271,6 +272,8 @@ export function UserPromptRow({
271272
message: ThreadAgentMessage;
272273
author: TaskThreadMessage["author"];
273274
}) {
275+
const promptText = normalizeAgentPromptText(message.text);
276+
274277
return (
275278
<ThreadItem>
276279
<ThreadItemGutter>
@@ -288,7 +291,7 @@ export function UserPromptRow({
288291
)}
289292
</ThreadItemHeader>
290293
<ThreadItemBody className="wrap-break-word whitespace-pre-wrap">
291-
<span className={mentionChipClass}>@agent</span> {message.text}
294+
<span className={mentionChipClass}>@agent</span> {promptText}
292295
</ThreadItemBody>
293296
</ThreadItemContent>
294297
</ThreadItem>

0 commit comments

Comments
 (0)