From 2011069b724a727ba4756f98ea6e4019bd1df3d4 Mon Sep 17 00:00:00 2001 From: TheAnimatrix Date: Tue, 1 Dec 2026 02:43:33 +0530 Subject: [PATCH 1/5] feat(bots): Allow for this chat in the chat of another computer's bot, with who it reaches, time left and End access; offer cards say what became of each --- src/renderer/components/bots/BotCards.tsx | 8 +- src/renderer/components/bots/BotChatGrant.tsx | 219 ++++++++++++++++++ src/renderer/components/bots/BotChatView.tsx | 22 +- src/renderer/components/bots/BotOfferCard.tsx | 42 +++- src/renderer/services/personalBotsClient.ts | 16 +- src/renderer/stores/botsStore.ts | 2 + src/renderer/styles/bots.css | 15 ++ tests/renderer/botChatGrant.test.ts | 143 ++++++++++++ tests/renderer/botOfferCard.test.ts | 31 ++- tests/renderer/fakeBotsClient.ts | 28 ++- 10 files changed, 505 insertions(+), 21 deletions(-) create mode 100644 src/renderer/components/bots/BotChatGrant.tsx create mode 100644 tests/renderer/botChatGrant.test.ts diff --git a/src/renderer/components/bots/BotCards.tsx b/src/renderer/components/bots/BotCards.tsx index 2473182ae..af407e8ac 100644 --- a/src/renderer/components/bots/BotCards.tsx +++ b/src/renderer/components/bots/BotCards.tsx @@ -79,10 +79,10 @@ function GalleryCard({ card }: { card: BotChatGalleryCard }) { /** * A message's cards, in the order the bot made them; an automation it created as its own row (`BotAutomationRow`), and an - * offered message with its Send where it is for this computer (`BotOfferRow`; `messageId` and `seq` name its row, - * `remote` a bot of another computer). + * offered message with its Send where it is for this computer (`BotOfferRow`; `messageId` and `seq` name its row, `at` + * when it was made, `remote` a bot of another computer). */ -export function BotCards({ cards, botId, messageId, seq, remote = false }: { cards: readonly BotChatCard[]; botId?: string; messageId?: string; seq?: number; remote?: boolean }) { +export function BotCards({ cards, botId, messageId, seq, at, remote = false }: { cards: readonly BotChatCard[]; botId?: string; messageId?: string; seq?: number; at?: string; remote?: boolean }) { if (!cards.length) return null return
{cards.map((card, index) => { @@ -92,7 +92,7 @@ export function BotCards({ cards, botId, messageId, seq, remote = false }: { car if (card.type === 'checklist') return if (card.type === 'progress') return if (card.type === 'gallery') return - if (card.type === 'offer') return botId ? : null + if (card.type === 'offer') return botId ? : null return null })}
diff --git a/src/renderer/components/bots/BotChatGrant.tsx b/src/renderer/components/bots/BotChatGrant.tsx new file mode 100644 index 000000000..9c709eb82 --- /dev/null +++ b/src/renderer/components/bots/BotChatGrant.tsx @@ -0,0 +1,219 @@ +import { useCallback, useEffect, useRef, useState } from 'react' +import { + BOT_GRANT_REFUSALS, + MAX_CHAT_GRANT_RECIPIENTS, + isBotGrantRefusal, + type BotChatGrantEnd, + type BotChatGrantView, + type BotOfferRecipient +} from '../../../shared/bots/personal/offers' +import { botsClient } from '../../stores/botsStore' +import { errorText, personalBotsEvents } from '../../services/personalBotsClient' +import { useComposerNotice, type ComposerNoticeProps } from '../ComposerNotice' +import { Check, Link2 } from '../../lib/icons' +import { useDismiss } from './BotEmoji' + +/** + * "Allow for this chat" (issue #2490 stage 2), in the chat of a bot of another computer: the composer's link button + * picks bots of this computer that bot may message, and allows the chat for them; while it is allowed, the line above + * the composer says who it can reach and for how long, with End access. Another window shows that line only. This + * computer keeps the grant and decides everything (`bots.remote.grantOpen`); the window only shows what it says. + */ + +/** The chat's grant as this window sees it, read again when it opens or ends anywhere here, or the connection came back. */ +export function useChatGrant(botId: string, live: boolean): { grant: BotChatGrantView | undefined; set(next: BotChatGrantView): void } { + const [grant, setGrant] = useState(undefined) + // A read asked for later wins over one that answers late. + const asked = useRef(0) + const read = useCallback(() => { + const ask = ++asked.current + void botsClient().grantState(botId).then((next) => { if (asked.current === ask) setGrant(next) }, () => undefined) + }, [botId]) + useEffect(() => { + setGrant(undefined) + if (!live) return + read() + return personalBotsEvents().onChanged?.((event) => { + if (event?.grant === botId || (event?.reread && (!event.botId || event.botId === botId))) read() + }) + }, [botId, live, read]) + const set = useCallback((next: BotChatGrantView) => { + asked.current += 1 + setGrant(next) + }, []) + return { grant, set } +} + +/** "Alpha", "Alpha and Beta", "Alpha, Beta and Gamma", "Alpha, Beta and 3 more". */ +export function grantNames(recipients: readonly BotOfferRecipient[]): string { + const names = recipients.map((bot) => bot.name) + if (names.length <= 1) return names[0] ?? '' + if (names.length <= 3) return `${names.slice(0, -1).join(', ')} and ${names.at(-1)}` + return `${names.slice(0, 2).join(', ')} and ${names.length - 2} more` +} + +/** Why access ended, in a few words, for the line a window shows once it does. */ +const ENDED_LINES: Record = { + ended: 'Access was ended', + left: 'The window that allowed it left the chat', + closed: 'The window that allowed it closed', + idle: 'Nothing was sent for 10 minutes', + expired: 'The hour is up', + offline: 'The connection dropped', + private: 'A bot it could message was made private', + blocked: 'That computer was blocked', + removed: 'That bot is no longer shared here' +} + +/** Whole minutes left until `endsAt`, at least 1 while it is open. */ +function minutesLeft(endsAt: string | undefined, now: number): number | undefined { + const at = endsAt ? Date.parse(endsAt) : Number.NaN + return Number.isFinite(at) ? Math.max(1, Math.ceil((at - now) / 60_000)) : undefined +} + +/** The time now, again every 30 seconds while `on`: a minute count that moves calmly. */ +function useMinuteClock(on: boolean): number { + const [now, setNow] = useState(() => Date.now()) + useEffect(() => { + if (!on) return + setNow(Date.now()) + const timer = setInterval(() => setNow(Date.now()), 30_000) + return () => clearInterval(timer) + }, [on]) + return now +} + +/** + * The line above the composer while the chat is allowed (who, time left, End access in the window that allowed it, + * "Allowed in another window" in any other), and once after it ends for a reason this window did not give. + */ +export function BotChatGrantNotice({ botName, grant, onEnd }: { botName: string; grant: BotChatGrantView | undefined; onEnd(): Promise }) { + const active = grant?.active === true + const now = useMinuteClock(active) + const [ending, setEnding] = useState(false) + const [ended, setEnded] = useState(undefined) + // The last open grant this window saw, to tell an end it did not ask for. + const seen = useRef(undefined) + useEffect(() => { + if (grant?.active) { + seen.current = grant + setEnded(undefined) + return + } + const was = seen.current + seen.current = undefined + if (was && grant?.ended && !(was.mine && grant.ended === 'ended')) setEnded(grant.ended) + }, [grant]) + const end = async () => { + setEnding(true) + try { + await onEnd() + } finally { + setEnding(false) + } + } + const icon =