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.

Sidebar follows hires, undos and bot changes without a reload

Lin (kwant bot)2af9d2adb7f1
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx
index 3e7e21c0..e794eb2f 100644
--- a/frontend/src/App.tsx
+++ b/frontend/src/App.tsx
@@ -8,7 +8,7 @@ import { DelegationCard } from './components/DelegationCard';
import { QuestionCard } from './components/QuestionCard';
import { McpSuggestionCard } from './components/McpSuggestionCard';
import { Suggestion, SuggestionMeta, applySuggestion, suggestionsApi } from './mcpSuggestions';
-import { ApiError, BotActionMeta, HireMeta, applyBotAction, applyHire, botActionsApi, decisionError, hiresApi } from './botActions';
+import { ApiError, BotActionMeta, HireMeta, applyBotAction, applyHire, botActionsApi, changesBots, decisionError, hiresApi } from './botActions';
import { BotActionCard, type BotActionDecision } from './components/BotActionCard';
import { HireCard, type HireDecision } from './components/HireCard';
import type { RoutineFocus } from './components/RoutinesPanel';
@@ -291,6 +291,45 @@ export const SpaceApp: React.FC<SpaceAppProps> = ({
const refreshProfiles = useCallback(() => {
profilesApi.list().then(setProfiles, () => undefined);
}, []);
+ // Bots, teams and the space's counts again after a bot was hired, made, undone or deleted elsewhere, so the sidebar
+ // matches the server without a reload. Bursts of events share one load; a late reply never
+ // overwrites a newer one.
+ const onSpacesChangedRef = useRef(onSpacesChanged);
+ onSpacesChangedRef.current = onSpacesChanged;
+ const rosterTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
+ const rosterSeqRef = useRef(0);
+ const refreshRoster = useCallback(() => {
+ if (rosterTimerRef.current) clearTimeout(rosterTimerRef.current);
+ rosterTimerRef.current = setTimeout(async () => {
+ rosterTimerRef.current = null;
+ const seq = ++rosterSeqRef.current;
+ try {
+ const [kRes, tRes] = await Promise.all([
+ fetch('/api/kwants').then((r) => (r.ok ? r.json() : Promise.reject(new Error(`HTTP ${r.status}`)))),
+ fetch('/api/teams').then((r) => (r.ok ? r.json() : null)).catch(() => null),
+ ]);
+ if (seq !== rosterSeqRef.current || !Array.isArray(kRes)) return;
+ setKwants(kRes);
+ // The space header counts bots too.
+ onSpacesChangedRef.current();
+ if (Array.isArray(tRes)) {
+ setTeams(tRes.map((t: any) => ({
+ ...t,
+ member_kwants: Array.isArray(t.member_kwants)
+ ? t.member_kwants
+ : typeof t.member_kwants === 'string'
+ ? JSON.parse(t.member_kwants || '[]')
+ : [],
+ })));
+ }
+ } catch {
+ // The next change or a reload tries again; the list on screen stays as it was.
+ }
+ }, 150);
+ }, []);
+ useEffect(() => () => {
+ if (rosterTimerRef.current) clearTimeout(rosterTimerRef.current);
+ }, []);
const markLive = (ctx: string | null | undefined, speaker?: string) => {
if (!ctx) return;
const name = speaker ?? liveRef.current[ctx] ?? '';
@@ -573,6 +612,7 @@ export const SpaceApp: React.FC<SpaceAppProps> = ({
try {
const event = JSON.parse(e.data);
if (affectsAway(event.type)) refreshAwayCount(800);
+ if (changesBots(event.type, event.data)) refreshRoster();
switch (event.type) {
case 'turn_queued':
case 'TurnQueued':
diff --git a/frontend/src/botActions.ts b/frontend/src/botActions.ts
index 2fc5d132..7d90dfd7 100644
--- a/frontend/src/botActions.ts
+++ b/frontend/src/botActions.ts
@@ -416,3 +416,24 @@ export const hiresApi = {
setPolicy: (spaceId: string, body: { policy?: 'ask' | 'auto'; clear_denials?: boolean }) =>
call<unknown>('PUT', `/api/spaces/${enc(spaceId)}/hire-policy`, body),
};
+
+// ---------------------------------------------------------------------------
+// Sidebar
+
+/**
+ * Whether a live event added, changed or removed a bot, so the bot list should reload: a bot
+ * action on a bot that was applied, undone or confirmed, or a hire that was accepted or undone.
+ */
+export function changesBots(type: string, data: unknown): boolean {
+ const d = data && typeof data === 'object' ? (data as Record<string, unknown>) : null;
+ if (!d) return false;
+ if (type === 'bot_action_updated' || type === 'BotActionUpdated') {
+ const card = parseBotActionMeta((d.action as BotAction | undefined)?.card);
+ return !!card && card.target_kind === 'bot' && ['applied', 'undone', 'confirmed'].includes(card.status);
+ }
+ if (type === 'hire_updated' || type === 'HireUpdated') {
+ const card = parseHireMeta((d.hire as Hire | undefined)?.card);
+ return !!card && ['accepted', 'auto_accepted', 'undone'].includes(card.status);
+ }
+ return false;
+}
diff --git a/frontend/tests/botActions.test.mjs b/frontend/tests/botActions.test.mjs
index 4409db40..743646dc 100644
--- a/frontend/tests/botActions.test.mjs
+++ b/frontend/tests/botActions.test.mjs
@@ -276,3 +276,23 @@ test('hire policy parses with safe defaults', () => {
assert.deepEqual(d.denied, []);
assert.equal(b.parseHirePolicy(null), null);
});
+
+test('bot and hire events that add or remove a bot ask the sidebar to reload', () => {
+ const action = (over) => ({ id: 'a9', status: over.status ?? 'applied', card: routineMeta({ action_id: 'a9', ...over }) });
+ const hireEv = (status) => ({ id: 'h9', status, card: { kind: 'hire_proposal', hire_id: 'h9', kwant_id: 'k1', name: 'Chef', role: 'Cook', status } });
+ // A bot made, renamed, hired, undone or deleted: reload.
+ assert.equal(b.changesBots('bot_action_updated', { action: action({ action: 'bot_create', target_kind: 'bot', target_id: 'k2' }) }), true);
+ assert.equal(b.changesBots('bot_action_updated', { action: action({ action: 'bot_hire', target_kind: 'bot', status: 'undone' }) }), true);
+ assert.equal(b.changesBots('BotActionUpdated', { action: action({ action: 'bot_delete', target_kind: 'bot', status: 'confirmed' }) }), true);
+ assert.equal(b.changesBots('hire_updated', { hire: hireEv('accepted') }), true);
+ assert.equal(b.changesBots('HireUpdated', { hire: hireEv('auto_accepted') }), true);
+ assert.equal(b.changesBots('hire_updated', { hire: hireEv('undone') }), true);
+ // Nothing about the bot list changed yet, or not a bot at all: leave it.
+ assert.equal(b.changesBots('bot_action_updated', { action: action({ action: 'bot_delete', target_kind: 'bot', status: 'pending' }) }), false);
+ assert.equal(b.changesBots('bot_action_updated', { action: action({ action: 'bot_delete', target_kind: 'bot', status: 'declined' }) }), false);
+ assert.equal(b.changesBots('bot_action_updated', { action: action({}) }), false);
+ assert.equal(b.changesBots('hire_updated', { hire: hireEv('pending') }), false);
+ assert.equal(b.changesBots('hire_updated', { hire: hireEv('denied') }), false);
+ assert.equal(b.changesBots('message_added', { message: {} }), false);
+ assert.equal(b.changesBots('hire_updated', null), false);
+});