Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Keep a private chat's card chip out of someone else's conversation; reopen the same chip when its card waits again

AdityaUmale48d0248510d8
diff --git a/server/host/ask-cards.test.ts b/server/host/ask-cards.test.ts
index 2f592000d5..9d84c2c4b0 100644
--- a/server/host/ask-cards.test.ts
+++ b/server/host/ask-cards.test.ts
@@ -9,7 +9,7 @@ import { askToolName, createAskCards, type AskCardsDeps } from "./ask-cards.ts";
const AUTH = { kind: "session", scopes: ["client"], session: { id: "s", email: "[email protected]" } } as unknown as RequestAuth;
const QUESTION = "What wording should the new footer text use?";
-function world(options: { inboxThread?: string; live?: string[] } = {}) {
+function world(options: { inboxThread?: string; live?: string[]; privateThreads?: string[] } = {}) {
const threads = new Map<string, Message[]>();
let ids = 0;
const bots = [
@@ -50,6 +50,7 @@ function world(options: { inboxThread?: string; live?: string[] } = {}) {
delegatorOf: (threadId) => (threadId === "work" ? { threadId: "inbox", botId: "scout" } : undefined),
notify: (notification) => { if (notification) notified.push(notification); },
copiedInto: (inboxThreadId) => inboxThreadId === options.inboxThread,
+ keptPrivate: (threadId) => (options.privateThreads ?? []).includes(threadId),
live: (_threadId, requestId) => (options.live ?? []).includes(requestId),
answerCard: vi.fn(async (input) => {
if (refuse) return { ok: false as const, ...refuse };
@@ -414,6 +415,27 @@ describe("the chip in the conversation that handed the work on", () => {
expect(open?.tool?.name).toBe("Waiting on you to connect Gmail in @Ledger");
});
+ it("says the same line waits again when the card waits again, without a second line", () => {
+ const desk = world();
+ desk.cards.reopenWaitingOnCardChip("work", "c1");
+ expect(desk.rows("inbox")).toEqual([]);
+ desk.cards.showWaitingOnCardChip("work", { kind: "sign-in", messageId: "c1", label: "example.com" }, asker);
+ desk.cards.settleWaitingOnCardChip("work", "c1");
+ desk.cards.reopenWaitingOnCardChip("work", "c1");
+ const rows = desk.rows("inbox");
+ expect(rows).toHaveLength(1);
+ expect(rows[0]!.tool).toMatchObject({ name: "Waiting on you to sign in to example.com in @Ledger", nameCode: "chat.notice.waitingSignInElsewhere" });
+ expect(rows[0]!.tool?.ok).toBeUndefined();
+ desk.cards.settleWaitingOnCardChip("work", "c1");
+ expect(desk.rows("inbox")[0]!.tool).toMatchObject({ name: "@Ledger no longer waits on you", ok: true });
+ });
+
+ it("keeps a private conversation's card, site and title out of a conversation that belongs to someone else", () => {
+ const team = world({ privateThreads: ["work"] });
+ team.cards.showWaitingOnCardChip("work", { kind: "sign-in", messageId: "c1", label: "example.com" }, asker);
+ expect(team.rows("inbox")).toEqual([]);
+ });
+
it("leaves a card's chip out of the person's inbox, which has the card, and out of work nobody handed on", () => {
const team = world({ inboxThread: "inbox" });
team.cards.showWaitingOnCardChip("work", { kind: "password", messageId: "c1", label: "API key" }, asker);
diff --git a/server/host/ask-cards.ts b/server/host/ask-cards.ts
index 677170c7d0..a9a23b1e9d 100644
--- a/server/host/ask-cards.ts
+++ b/server/host/ask-cards.ts
@@ -34,6 +34,8 @@ export interface AskCardsDeps {
notify(notification: Notification | null): void;
/** On a Team Cloud: what `botId` asks in `threadId` reaches the inbox conversation `inboxThreadId` as a copy. */
copiedInto(inboxThreadId: string, botId: string, threadId: string): boolean;
+ /** `threadId` is private to someone other than the person `parentThreadId` belongs to: its details stay out of it. */
+ keptPrivate(threadId: string, parentThreadId: string): boolean;
/** The bot's turn in `threadId` waits on this card's live request. */
live(threadId: string, requestId: string): boolean;
/** The parked node's next turn carries the person's answer. */
@@ -110,7 +112,7 @@ export function createAskCards(deps: AskCardsDeps) {
}
}
- const waitingCardChips = new Map<string, { threadId: string; messageId: string; name: string }>();
+ const waitingCardChips = new Map<string, { threadId: string; messageId: string; name: string; tool: NonNullable<Message["tool"]>; settled: boolean }>();
/** A sign-in, password, connect or review card left open in handed-off work: the conversation that handed it on links to
* it, as it does for a question or approval. Keyed by the card, so it shows once. */
@@ -119,21 +121,21 @@ export function createAskCards(deps: AskCardsDeps) {
const id = `${threadId}:${wait.messageId}`;
if (!key || waitingCardChips.has(id)) return;
const delegator = delegatorOf(threadId);
- if (!delegator || delegator.threadId === threadId || deps.copiedInto(delegator.threadId, asker.id, threadId)) return;
+ if (!delegator || delegator.threadId === threadId || deps.copiedInto(delegator.threadId, asker.id, threadId) || deps.keptPrivate(threadId, delegator.threadId)) return;
+ const tool = noticeTool(key, { name: asker.name, label: wait.label }, {});
const chip = store.appendMessage(delegator.threadId, { role: "bot", kind: "activity",
from: { botId: asker.id, name: asker.name, color: asker.color },
- tool: noticeTool(key, { name: asker.name, label: wait.label }, {}),
+ tool,
threadRef: { botId: asker.id, threadId, title: store.taskByThread(asker.id, threadId)?.title ?? asker.name },
});
- waitingCardChips.set(id, { threadId: delegator.threadId, messageId: chip.id, name: asker.name });
+ waitingCardChips.set(id, { threadId: delegator.threadId, messageId: chip.id, name: asker.name, tool, settled: false });
}
/** The card was saved, declined, replaced or stopped: its chip says the work no longer waits. */
function settleWaitingOnCardChip(threadId: string, cardMessageId: string) {
- const id = `${threadId}:${cardMessageId}`;
- const chip = waitingCardChips.get(id);
- if (!chip) return;
- waitingCardChips.delete(id);
+ const chip = waitingCardChips.get(`${threadId}:${cardMessageId}`);
+ if (!chip || chip.settled) return;
+ chip.settled = true;
const existing = store.messagesFor(chip.threadId).find((message) => message.id === chip.messageId);
if (!existing?.tool) return;
const { nameCode: _code, nameParams: _params, ...tool } = existing.tool;
@@ -142,6 +144,17 @@ export function createAskCards(deps: AskCardsDeps) {
});
}
+ /** The same card waits again (the person went back to its branch): its chip says so again, on the same line. */
+ function reopenWaitingOnCardChip(threadId: string, cardMessageId: string) {
+ const chip = waitingCardChips.get(`${threadId}:${cardMessageId}`);
+ if (!chip?.settled) return;
+ chip.settled = false;
+ const existing = store.messagesFor(chip.threadId).find((message) => message.id === chip.messageId);
+ if (!existing?.tool) return;
+ const { ok: _ok, ...tool } = existing.tool;
+ store.patchMessage(chip.threadId, chip.messageId, { tool: { ...tool, ...chip.tool } });
+ }
+
/** In a room only the asking member's own lines. */
const askersLines = (node: Pick<HandoffView, "threadId" | "botId" | "groupId">) => store.messagesFor(node.threadId)
.filter((message) => !node.groupId || message.from?.botId === node.botId);
@@ -318,5 +331,5 @@ export function createAskCards(deps: AskCardsDeps) {
questionCard({ id: `${botId}-${turnId}`, threadId, botId, executions: 1, createdAt: reply.at, startedAt: reply.at } as HandoffView, question);
}
- return { park, remind, decided, closed, askedAgain, answerParked, answeredInWords, typed, ownQuestion, showWaitingOnPersonChip, settleWaitingOnPersonChips, showWaitingOnCardChip, settleWaitingOnCardChip };
+ return { park, remind, decided, closed, askedAgain, answerParked, answeredInWords, typed, ownQuestion, showWaitingOnPersonChip, settleWaitingOnPersonChips, showWaitingOnCardChip, settleWaitingOnCardChip, reopenWaitingOnCardChip };
}
diff --git a/server/host/person-waits.test.ts b/server/host/person-waits.test.ts
index ca927ad4b7..9f1e5adf81 100644
--- a/server/host/person-waits.test.ts
+++ b/server/host/person-waits.test.ts
@@ -17,14 +17,16 @@ function fixture() {
const sent: Notification[] = [];
const known = new Set<string>();
const elsewhere: string[] = [];
+ const resumed: string[] = [];
const waits = createPersonWaits({ store, notify: (notification) => { if (notification) sent.push(notification); }, threadsWithCards: () => [...known],
waitingElsewhere: (threadId, wait, asker) => elsewhere.push(`open ${threadId} ${wait.messageId} ${wait.kind} ${wait.label} ${asker.name}`),
- waitSettled: (threadId, messageId) => elsewhere.push(`settled ${threadId} ${messageId}`) });
+ waitSettled: (threadId, messageId) => elsewhere.push(`settled ${threadId} ${messageId}`),
+ waitResumed: (threadId, messageId) => resumed.push(`${threadId} ${messageId}`) });
const bot = store.createBot({ name: "Postie" });
const thread = store.createTask(bot.id, "Work", false)!.threadId;
const task = () => store.taskByThread(bot.id, thread)!;
const card = (patch: Record<string, unknown> = {}) => store.appendMessage(thread, { role: "bot", kind: "secret", text: "enter it", secret: { ...SECRET, ...patch } });
- return { store, waits, sent, known, bot, thread, task, card, elsewhere };
+ return { store, waits, sent, known, bot, thread, task, card, elsewhere, resumed };
}
describe("a conversation resting on the person", () => {
@@ -213,6 +215,16 @@ describe("telling the person", () => {
expect(elsewhere).toEqual([`open ${thread} ${asked.id} password Mailbox password Postie`, `settled ${thread} ${asked.id}`]);
});
+ it("tells it when the same card waits on the person again", async () => {
+ const { store, thread, card, elsewhere, resumed } = fixture();
+ const asked = card();
+ await flush();
+ store.patchMessage(thread, asked.id, { secret: { ...SECRET, dismissed: true } });
+ store.patchMessage(thread, asked.id, { secret: { ...SECRET, dismissed: false } });
+ expect(elsewhere.at(-1)).toBe(`settled ${thread} ${asked.id}`);
+ expect(resumed).toEqual([`${thread} ${asked.id}`, `${thread} ${asked.id}`]);
+ });
+
it("does not tell it of a card settled before anyone could hear of it, nor of a provider's question", async () => {
const { store, thread, card, elsewhere } = fixture();
const asked = card();
diff --git a/server/host/person-waits.ts b/server/host/person-waits.ts
index 526aa7f5a9..806e6c9358 100644
--- a/server/host/person-waits.ts
+++ b/server/host/person-waits.ts
@@ -19,6 +19,8 @@ export interface PersonWaitsDeps {
waitingElsewhere?(threadId: string, wait: PersonWait, asker: BotRecord): void;
/** That card no longer waits on the person. */
waitSettled?(threadId: string, cardMessageId: string): void;
+ /** That card waits on the person again. */
+ waitResumed?(threadId: string, cardMessageId: string): void;
}
const NOTIFIED_ELSEWHERE: ReadonlySet<PersonWait["kind"]> = new Set(["question", "approval"]);
@@ -58,6 +60,7 @@ export function createPersonWaits(deps: PersonWaitsDeps) {
if (wait) resting.set(threadId, wait);
else resting.delete(threadId);
if (before && before.messageId !== wait?.messageId) deps.waitSettled?.(threadId, before.messageId);
+ if (wait && wait.messageId !== before?.messageId) deps.waitResumed?.(threadId, wait.messageId);
show(threadId, wait);
}
diff --git a/server/index.ts b/server/index.ts
index 01e2733596..c009ec9912 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -3843,7 +3843,7 @@ const followupEndedRoots = new Map<string, string | undefined>();
const askCards = createAskCards({ store, node: (id) => coordinator.node(id), delegatorOf, notify, answerCard: respondToCard, answerNode: (id, text) => coordinator.answerNode(id, text),
answeredIn: (threadId, requestId) => automationReports.answeredIn(teamMessages.inbox.answeredIn(threadId, requestId), requestId), live: (threadId, requestId) => askMessageByRequest.has(`${threadId}:${requestId}`),
- copiedInto: (inboxThreadId, botId, threadId) => Boolean(CLOUD_TEAM) && teamMessages.inbox.isHomeOf(inboxThreadId, teamOwners.routing.askedPerson(botId, threadId)) });
+ copiedInto: (inboxThreadId, botId, threadId) => Boolean(CLOUD_TEAM) && teamMessages.inbox.isHomeOf(inboxThreadId, teamOwners.routing.askedPerson(botId, threadId)), keptPrivate: (threadId, parentThreadId) => keptPrivate({ child: teamHost.privateTo(threadId), parent: teamHost.privateTo(parentThreadId) }) });
const questionRouting = createQuestionRouting({ store, handedInUnproven, active: () => coordinator.active(), node: (id) => coordinator.node(id), parkedFor: (botId) => coordinator.parkedFor(botId), answerNode: (id, text, by) => coordinator.answerNode(id, text, by), live: (threadId, requestId) => askMessageByRequest.has(`${threadId}:${requestId}`), hasLive: (threadId) => { for (const key of askMessageByRequest.keys()) if (key.startsWith(`${threadId}:`)) return true; return false; },
unattended: (botId, threadId) => teammatePrompts.unattendedWork(botId, threadId),
answerLive: async (threadId, requestId, text) => await answerRequest(threadId, botForThread(store.botByThread(threadId)?.id ?? "", threadId)?.modelSelection.instanceId ?? "", requestId, "answer", text) === "answered", settleSignIns: (node) => signInSettle.answeredElsewhere(node),
@@ -4912,7 +4912,7 @@ const providerLabel = (provider: string): string => {
};
// Rewritten by every dispatch, so a quiet turn never silences the next one.
-const quietTurnThreads = new Set<string>(); const doneNotice = createDoneNotice({ awaitingTeammates: (threadId) => coordinator.awaitingTeammateThreads().has(threadId), queuedFollowUp: hasQueuedPersonMessage }); const personWaits = createPersonWaits({ store, notify, threadsWithCards: threadsWithCardsSince, waitingElsewhere: askCards.showWaitingOnCardChip, waitSettled: askCards.settleWaitingOnCardChip });
+const quietTurnThreads = new Set<string>(); const doneNotice = createDoneNotice({ awaitingTeammates: (threadId) => coordinator.awaitingTeammateThreads().has(threadId), queuedFollowUp: hasQueuedPersonMessage }); const personWaits = createPersonWaits({ store, notify, threadsWithCards: threadsWithCardsSince, waitingElsewhere: askCards.showWaitingOnCardChip, waitSettled: askCards.settleWaitingOnCardChip, waitResumed: askCards.reopenWaitingOnCardChip });
const QUIET_TURN_NEWS: ReadonlySet<Notification["kind"]> = new Set(["done", "turn-failed", "incident"]);
function notify(notification: Notification | null, how?: { routine?: boolean; summary?: boolean }) {