Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

Reopen the bot chat on reload and direct /b/<bot> links

Pixel (kwant bot)7282a84a36cd
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx
index 2fdd9e89..bf903fbe 100644
--- a/frontend/src/App.tsx
+++ b/frontend/src/App.tsx
@@ -66,7 +66,7 @@ import {
turnsAwaitingAnswer,
} from './questions';
import { AwayItem, affectsAway, awayApi, awayTarget, badgeText } from './away';
-import { Space, eventsUrl, spaceColor } from './spaces';
+import { Space, eventsUrl, pickInitialChat, spaceColor } from './spaces';
// Kwant and Tools editors are their own chunks. They stay mounted (rendering nothing while closed),
// so both load right after start-up and opening them never waits.
@@ -490,35 +490,24 @@ export const SpaceApp: React.FC<SpaceAppProps> = ({
setChannels(cRes);
setTeams(normalizedTeams);
- // The chat asked for (a link, a notification, the last one open here) if it still exists.
- const wantBot = initialTarget?.botId ? (kRes as Kwant[]).find((k) => k.id === initialTarget.botId) : undefined;
- const wantChannel = initialTarget?.channelId
- ? (cRes as Channel[]).find((c) => c.id === initialTarget.channelId)
- : (cRes as Channel[]).find((c) => c.id === space.general_channel_id);
- if (wantBot && !wantChannel) {
- setSelectedId(wantBot.id);
- selectedIdRef.current = wantBot.id;
+ // The chat asked for (a link, a reload, a notification, the last one open here) if it still exists.
+ const first = pickInitialChat(initialTarget, Array.isArray(kRes) ? (kRes as Kwant[]) : [], Array.isArray(cRes) ? (cRes as Channel[]) : [], space.general_channel_id);
+ if (first?.kind === 'bot') {
+ setSelectedId(first.bot.id);
+ selectedIdRef.current = first.bot.id;
setIsChannel(false);
isChannelRef.current = false;
setSelectedTeamId(null);
- } else if (wantChannel || cRes.length > 0) {
- const init: Channel = wantChannel ?? cRes[0];
- const initId = init.id;
- setSelectedId(initId);
- selectedIdRef.current = initId;
+ } else if (first?.kind === 'channel') {
+ const init = first.channel;
+ setSelectedId(init.id);
+ selectedIdRef.current = init.id;
setIsChannel(true);
isChannelRef.current = true;
const matchedTeam = normalizedTeams.find(
(t) => `team-${t.name}` === init.name || t.id === init.id
);
setSelectedTeamId(matchedTeam ? matchedTeam.id : null);
- } else if (kRes.length > 0) {
- const initId = kRes[0].id;
- setSelectedId(initId);
- selectedIdRef.current = initId;
- setIsChannel(false);
- isChannelRef.current = false;
- setSelectedTeamId(null);
}
} catch (err) {
console.error('Failed to load initial data:', err);
diff --git a/frontend/src/spaces.ts b/frontend/src/spaces.ts
index b116d1c6..666480e2 100644
--- a/frontend/src/spaces.ts
+++ b/frontend/src/spaces.ts
@@ -220,6 +220,26 @@ export function pickSpace(spaces: Space[], fromUrl: string | null, remembered: s
);
}
+/**
+ * The chat a space opens on: the bot or channel asked for (a link, a reload, a notification) if it
+ * still exists, else #general, else the first channel, else the first bot. A bot target never
+ * falls through to #general just because #general exists.
+ */
+export function pickInitialChat<B extends { id: string }, C extends { id: string }>(
+ target: { channelId?: string | null; botId?: string | null } | null | undefined,
+ bots: B[],
+ channels: C[],
+ generalId: string | null | undefined,
+): { kind: 'bot'; bot: B } | { kind: 'channel'; channel: C } | null {
+ const channel = target?.channelId ? channels.find((c) => c.id === target.channelId) : undefined;
+ if (channel) return { kind: 'channel', channel };
+ const bot = target?.botId ? bots.find((b) => b.id === target.botId) : undefined;
+ if (bot) return { kind: 'bot', bot };
+ const fallback = channels.find((c) => c.id === generalId) ?? channels[0];
+ if (fallback) return { kind: 'channel', channel: fallback };
+ return bots[0] ? { kind: 'bot', bot: bots[0] } : null;
+}
+
// ---- #general: what Laya did ----
export interface RoutingMeta {
diff --git a/frontend/tests/spaces.test.mjs b/frontend/tests/spaces.test.mjs
index 80705533..1fceecdc 100644
--- a/frontend/tests/spaces.test.mjs
+++ b/frontend/tests/spaces.test.mjs
@@ -82,3 +82,22 @@ test('a new space preselects the first unused colour and offers Copy bots as com
assert.match(html, /This machine/);
assert.match(html, /Coming soon/);
});
+
+test('a reload or link reopens the bot or channel in the address, and a gone one falls back to #general', () => {
+ const bots = [{ id: 'k1' }, { id: 'k2' }];
+ const channels = [{ id: 'ch0' }, { id: 'ch1' }, { id: 'ch2' }];
+ const pick = (t) => {
+ const r = m.pickInitialChat(t, bots, channels, 'ch1');
+ return r && (r.kind === 'bot' ? `bot:${r.bot.id}` : `channel:${r.channel.id}`);
+ };
+ // The bug: a bot target lost to #general because #general exists.
+ assert.equal(pick({ botId: 'k2' }), 'bot:k2');
+ assert.equal(pick({ channelId: 'ch2' }), 'channel:ch2');
+ assert.equal(pick({ botId: 'gone' }), 'channel:ch1');
+ assert.equal(pick({ channelId: 'gone' }), 'channel:ch1');
+ assert.equal(pick(null), 'channel:ch1');
+ assert.equal(pick({}), 'channel:ch1');
+ assert.equal(m.pickInitialChat({ botId: 'gone' }, bots, channels, 'missing')?.channel.id, 'ch0');
+ assert.equal(m.pickInitialChat({ botId: 'gone' }, bots, [], 'ch1')?.bot.id, 'k1');
+ assert.equal(m.pickInitialChat({ botId: 'gone' }, [], [], 'ch1'), null);
+});