Skip to content
Merged
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
123 changes: 108 additions & 15 deletions desktop/src/features/agents/agentConversationLink.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,124 @@ import test from "node:test";

import {
buildAgentConversationLink,
isAgentConversationLink,
parseAgentConversationLink,
resolveAgentConversationLinkRenderTarget,
trimAgentConversationLinkMatch,
} from "./agentConversationLink.ts";

test("buildAgentConversationLink -> parseAgentConversationLink round-trips", () => {
const href = buildAgentConversationLink({
agentReplyId: "reply-1",
channelId: "channel-1",
const CHANNEL = "f570339f-8f8a-4e08-a779-8d954aa44109";
const REPLY =
"b04819ffc1f7c8ffb49c6d30b5899f470198264680d02e78894a658e30a9059f";

test("buildAgentConversationLink → parseAgentConversationLink round-trips", () => {
const url = buildAgentConversationLink({
agentReplyId: REPLY,
channelId: CHANNEL,
});
assert.equal(url, `buzz://task?channel=${CHANNEL}&reply=${REPLY}`);

assert.deepEqual(parseAgentConversationLink(href), {
ok: true,
value: {
agentReplyId: "reply-1",
channelId: "channel-1",
},
const parsed = parseAgentConversationLink(url);
assert.equal(parsed.ok, true);
assert.deepEqual(parsed.ok && parsed.value, {
agentReplyId: REPLY,
channelId: CHANNEL,
});
});

test("buildAgentConversationLink rejects missing required params", () => {
assert.throws(() =>
buildAgentConversationLink({ agentReplyId: REPLY, channelId: "" }),
);
assert.throws(() =>
buildAgentConversationLink({ agentReplyId: "", channelId: CHANNEL }),
);
});

test("parseAgentConversationLink rejects unsupported schemes and hosts", () => {
assert.deepEqual(
parseAgentConversationLink(
`https://example.com/?channel=${CHANNEL}&reply=${REPLY}`,
),
{ ok: false, reason: "wrong-scheme" },
);
assert.deepEqual(
parseAgentConversationLink("buzz://message?channel=c&id=m"),
{
ok: false,
reason: "wrong-host",
},
);
});

test("parseAgentConversationLink rejects missing required params", () => {
assert.deepEqual(parseAgentConversationLink("buzz://task?channel=c1"), {
ok: false,
reason: "missing-reply",
});
assert.deepEqual(parseAgentConversationLink("buzz://task?reply=m1"), {
assert.deepEqual(parseAgentConversationLink(`buzz://task?reply=${REPLY}`), {
ok: false,
reason: "missing-channel",
});
assert.deepEqual(
parseAgentConversationLink(`buzz://task?channel=${CHANNEL}`),
{
ok: false,
reason: "missing-reply",
},
);
});

test("isAgentConversationLink matches task links only", () => {
assert.equal(
isAgentConversationLink(`buzz://task?channel=${CHANNEL}&reply=${REPLY}`),
true,
);
assert.equal(isAgentConversationLink("buzz://message?channel=c&id=m"), false);
assert.equal(isAgentConversationLink("https://example.com"), false);
assert.equal(isAgentConversationLink(undefined), false);
assert.equal(isAgentConversationLink(""), false);
});

test("resolveAgentConversationLinkRenderTarget distinguishes cards from labels", () => {
const href = `buzz://task?channel=${CHANNEL}&reply=${REPLY}`;
const link = {
agentReplyId: REPLY,
channelId: CHANNEL,
};

assert.deepEqual(
resolveAgentConversationLinkRenderTarget({ href, label: href }),
{
kind: "card",
link,
},
);
assert.deepEqual(
resolveAgentConversationLinkRenderTarget({ href, label: "task" }),
{
kind: "label",
link,
},
);
assert.deepEqual(
resolveAgentConversationLinkRenderTarget({
href: "https://example.com",
label: href,
}),
{ kind: "none" },
);
});

test("trimAgentConversationLinkMatch keeps sentence punctuation outside links", () => {
const href = `buzz://task?channel=${CHANNEL}&reply=${REPLY}`;

assert.deepEqual(trimAgentConversationLinkMatch(`${href}.`), {
value: href,
trailing: ".",
});
assert.deepEqual(trimAgentConversationLinkMatch(`${href})`), {
value: href,
trailing: ")",
});
assert.deepEqual(trimAgentConversationLinkMatch(`${href}]`), {
value: href,
trailing: "]",
});
});
25 changes: 25 additions & 0 deletions desktop/src/features/agents/agentConversationLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,28 @@ export function trimAgentConversationLinkMatch(matchText: string) {
}
return { value, trailing: matchText.slice(value.length) };
}

type AgentConversationLinkRenderInput = {
href: string;
label: string;
};

export type AgentConversationLinkRenderTarget =
| { kind: "card"; link: ParsedAgentConversationLink }
| { kind: "label"; link: ParsedAgentConversationLink }
| { kind: "none" };

export function resolveAgentConversationLinkRenderTarget({
href,
label,
}: AgentConversationLinkRenderInput): AgentConversationLinkRenderTarget {
if (!isAgentConversationLink(href)) return { kind: "none" };

const parsed = parseAgentConversationLink(href);
if (!parsed.ok) return { kind: "none" };

return {
kind: label === href ? "card" : "label",
link: parsed.value,
};
}
44 changes: 0 additions & 44 deletions desktop/src/features/agents/agentConversations.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import assert from "node:assert/strict";
import test from "node:test";

import {
buildAgentConversationMentionPubkeys,
buildAgentConversation,
buildAgentConversationRecap,
buildAgentConversationMarkers,
deriveAgentConversationTitle,
getAutoRoutedAgentConversationPubkeys,
getHiddenAgentConversationMessageIds,
parseAgentConversationMarker,
readPersistedAgentConversations,
Expand Down Expand Up @@ -62,48 +60,6 @@ test("continued conversation title condenses a refined Buzz data thread", () =>
});
});

test("continued conversation auto-routes only a single messageable agent", () => {
assert.deepEqual(
getAutoRoutedAgentConversationPubkeys([
{ canMessage: true, pubkey: "agent-one" },
]),
["agent-one"],
);

assert.deepEqual(
getAutoRoutedAgentConversationPubkeys([
{ canMessage: true, pubkey: "agent-one" },
{ canMessage: true, pubkey: "agent-two" },
]),
[],
);

assert.deepEqual(
getAutoRoutedAgentConversationPubkeys([
{ canMessage: false, pubkey: "agent-one" },
]),
[],
);
});

test("continued conversation mention routing preserves explicit multi-agent mentions", () => {
assert.deepEqual(
buildAgentConversationMentionPubkeys({
autoRouteAgentPubkeys: [],
mentionPubkeys: ["agent-one"],
}),
["agent-one"],
);

assert.deepEqual(
buildAgentConversationMentionPubkeys({
autoRouteAgentPubkeys: ["AGENT-ONE"],
mentionPubkeys: ["agent-one", "agent-two"],
}),
["AGENT-ONE", "agent-two"],
);
});

function markerEvent({ content = {}, createdAt = 1, id = "marker" } = {}) {
return {
id,
Expand Down
56 changes: 12 additions & 44 deletions desktop/src/features/agents/agentConversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
KIND_AGENT_CONVERSATION,
KIND_AGENT_CONVERSATION_COMPAT,
} from "@/shared/constants/kinds";
import { normalizePubkey } from "@/shared/lib/pubkey";
import { parseAgentConversationLink } from "./agentConversationLink";
import {
collectConversationContextMessages,
deriveTitleFromContext,
Expand Down Expand Up @@ -80,11 +80,6 @@ export type AgentConversationRecapInput = {
messages: readonly TimelineMessage[];
};

export type AgentConversationRouteableParticipant = {
canMessage: boolean;
pubkey: string;
};

function normalizeAgentConversationStorageScope(
workspaceScope: string | null | undefined,
): string {
Expand All @@ -106,44 +101,17 @@ export function agentConversationsStorageKey(
return `${AGENT_CONVERSATIONS_STORAGE_PREFIX}:${normalizeAgentConversationStorageScope(workspaceScope)}:${pubkey}`;
}

export function getAutoRoutedAgentConversationPubkeys(
participants: readonly AgentConversationRouteableParticipant[],
): string[] {
if (participants.length !== 1) {
return [];
}

const [participant] = participants;
return participant.canMessage ? [participant.pubkey] : [];
}

export function buildAgentConversationMentionPubkeys({
autoRouteAgentPubkeys,
mentionPubkeys,
}: {
autoRouteAgentPubkeys: readonly string[];
mentionPubkeys: readonly string[];
}): string[] {
const seenPubkeys = new Set<string>();
const merged: string[] = [];
const add = (pubkey: string) => {
const normalized = normalizePubkey(pubkey);
if (!normalized || seenPubkeys.has(normalized)) {
return;
}

seenPubkeys.add(normalized);
merged.push(pubkey);
};

for (const pubkey of autoRouteAgentPubkeys) {
add(pubkey);
}
for (const pubkey of mentionPubkeys) {
add(pubkey);
}

return merged;
export function getAgentConversationMarkerTitleForHref(
markers: readonly AgentConversationMarker[] | undefined,
href: string,
) {
const parsed = parseAgentConversationLink(href);
if (!parsed.ok) return undefined;
return markers?.find(
(marker) =>
marker.channelId === parsed.value.channelId &&
marker.agentReplyId === parsed.value.agentReplyId,
)?.title;
}

export function readHiddenAgentConversationIds(
Expand Down
4 changes: 2 additions & 2 deletions desktop/src/features/agents/remarkAgentConversationLinks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Remark plugin that detects bare `buzz://task?…` URLs and replaces each with
* a custom task-link element. The renderer turns that element into an in-app
* task link instead of leaving a raw custom-scheme URL as inert text.
* a custom task-link element. The renderer turns that element into the same
* native task card shown when a dedicated conversation is started.
*/

import { createRemarkPrefixPlugin } from "../../shared/lib/createRemarkPrefixPlugin.ts";
Expand Down
Loading
Loading