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
94 changes: 94 additions & 0 deletions server/comms-visibility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Bot⇄bot comms visibility: channel creation, message mirroring, and
// per-thread chips. Extracted from /api/internal/ask-bot so delegations
// (delegate_bot) and any future peer flow reuse the same UX without a copy.

import type { BotRecord, GroupRecord, Message, Store } from "./store.ts";

/** What a peer-exchange helper needs from the outside world:
* the store (for persisted messages + groups) and the SSE broadcasters
* so chat clients see the change without waiting for a refresh. */
export interface CommsBus {
store: Store;
/** SSE broadcast (kind: "message" envelope). */
broadcast: (payload: Record<string, unknown>) => void;
/** SSE broadcast (kind: "group" envelope) for a single group. */
broadcastGroup: (groupId: string) => void;
}

/** Find or create the bot⇄bot channel for the pair. The channel keeps
* the pair's full exchange, lives in the sidebar like any room, and the
* user can open it to chip in. */
export function getOrCreateChannel(store: Store, from: BotRecord, target: BotRecord): GroupRecord {
return (
store.dmGroup(from.id, target.id) ??
store.createGroup(`${from.name} ⇄ ${target.name}`, [from.id, target.id], true)
);
}

/** Mirror `from`'s outgoing message into the channel, drop chips into
* both 1:1 threads linking to the channel, and bump the channel's unread
* count. The chips are what make bot-to-bot turns observable — those
* turns cost the user tokens, and a hidden exchange is exactly the kind
* of mistake peer coordination is supposed to avoid. */
export function mirrorExchange(
bus: CommsBus,
from: BotRecord,
target: BotRecord,
message: string,
channel: GroupRecord | undefined,
): void {
const note = (threadId: string, m: Omit<Message, "id" | "at">) => {
const message = bus.store.appendMessage(threadId, m);
bus.broadcast({ kind: "message", threadId, message });
return message;
};
if (channel) {
note(channel.threadId, {
role: "bot",
kind: "text",
text: message,
from: { botId: from.id, name: from.name, color: from.color },
});
}
note(from.threadId, {
role: "bot",
kind: "activity",
tool: { name: `Messaged @${target.name}` },
comm: channel
? { groupId: channel.id, withBotId: target.id, withName: target.name, withColor: target.color }
: undefined,
});
note(target.threadId, {
role: "bot",
kind: "activity",
tool: { name: `Message from @${from.name}` },
comm: channel
? { groupId: channel.id, withBotId: from.id, withName: from.name, withColor: from.color }
: undefined,
});
if (channel) {
bus.store.patchGroup(channel.id, { unread: true });
bus.broadcastGroup(channel.id);
}
}

/** Mirror `target`'s reply into the channel so the channel stays the
* single authoritative record of the exchange. The 1:1 threads already
* carry their own chips from `mirrorExchange`. */
export function mirrorReply(
bus: CommsBus,
target: BotRecord,
reply: string,
channel: GroupRecord | undefined,
): void {
if (!channel || !reply.trim()) return;
const message = bus.store.appendMessage(channel.threadId, {
role: "bot",
kind: "text",
text: reply,
from: { botId: target.id, name: target.name, color: target.color },
});
bus.broadcast({ kind: "message", threadId: channel.threadId, message });
bus.store.patchGroup(channel.id, { unread: true });
bus.broadcastGroup(channel.id);
}
Loading
Loading