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.

1.7 KB
Raw Blame
// What the avatar's model badge needs besides the live turn: each bot's pick and last reply's engine
// (from the app's bot list), and which engines can run (GET /api/engines). The app fills it, like botLooks.
import type { EngineInfo, RunsOn } from './engines';
import type { Profile } from './profiles';

export interface BadgeBot {
  id: string;
  preferred_model?: string | null;
  /** From GET /api/kwants; missing on servers before 0.9.6, null for a bot that never replied. */
  last_ran_on?: RunsOn | null;
}

let bots = new Map<string, BadgeBot>();
let engines: EngineInfo[] | null = null;
let profiles: Profile[] | null = null;
let rev = 0;
const listeners = new Set<() => void>();
const bump = () => {
  rev += 1;
  listeners.forEach((fn) => fn());
};

/** Replaces the known bots (the app calls it whenever its bot list changes). */
export function setBadgeBots(list: readonly BadgeBot[]): void {
  bots = new Map(list.map((b) => [b.id, b]));
  bump();
}

export function setBadgeEngines(list: EngineInfo[] | null): void {
  if (list === engines) return;
  engines = list;
  bump();
}

/** The space's accounts, for the hover's "5-hour 93% left · Weekly 44% left" (account-usage). */
export function setBadgeProfiles(list: Profile[] | null): void {
  if (list === profiles) return;
  profiles = list;
  bump();
}

export const badgeBotOf = (id: string | undefined): BadgeBot | undefined => (id ? bots.get(id) : undefined);
export const badgeEngines = (): EngineInfo[] | null => engines;
export const badgeProfiles = (): Profile[] | null => profiles;
export const badgeDataRev = (): number => rev;
export function subscribeBadgeData(fn: () => void): () => void {
  listeners.add(fn);
  return () => void listeners.delete(fn);
}