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.

11.3 KB
Raw Blame
// Proactive bots (contract: proactive-bots/contract.md). Bots decide reversible choices themselves
// and record them on the reply (meta.decisions) so the person can Change one; while a held reply
// is being decided, the chat shows "Team is deciding…" (SSE decide_updated); work done while the
// person was away arrives as a message with meta kind "work_ahead".
import { parseMetaObject } from './questions';
import { request } from './threads';

export interface Decision {
  index: number;
  question: string;
  options: string[];
  picked: string;
  why: string;
  status: 'decided' | 'changed';
  changed_to: string | null;
}

const str = (v: unknown): string => (typeof v === 'string' ? v : '');

/** A reply's decisions, in the order the bot made them; [] for anything else. */
export function parseDecisions(raw: unknown): Decision[] {
  const m = parseMetaObject(raw);
  if (!m || !Array.isArray(m.decisions)) return [];
  const out: Decision[] = [];
  m.decisions.forEach((d: unknown, i: number) => {
    if (!d || typeof d !== 'object') return;
    const r = d as Record<string, unknown>;
    const options = Array.isArray(r.options) ? r.options.filter((o): o is string => typeof o === 'string' && o.trim() !== '') : [];
    const picked = str(r.picked);
    if (!picked) return;
    const changedTo = typeof r.changed_to === 'string' && r.changed_to ? r.changed_to : null;
    out.push({
      index: typeof r.index === 'number' && Number.isInteger(r.index) ? r.index : i,
      question: str(r.question),
      options,
      picked,
      why: str(r.why),
      status: r.status === 'changed' && changedTo ? 'changed' : 'decided',
      changed_to: changedTo,
    });
  });
  return out;
}

// ---------------------------------------------------------------------------
// A raw [DECISIONS] block left in a reply's text

// Messages saved before the server took the block off every reply (channel rounds skipped it)
// still carry it in their text. The chat hides it and, when meta has no decisions, shows the
// block's lines as read-only chips. Same line format as src/server/decide.rs parse_line.

const OPEN_RE = /\[DECISIONS\]/gi;
const CLOSE_RE = /\[\/DECISIONS\]/i;
const MAX_DECISIONS = 6;

function field(part: string, name: string): string | null {
  const i = part.indexOf(':');
  if (i < 0) return null;
  return part.slice(0, i).trim().toLowerCase() === name ? part.slice(i + 1).trim() : null;
}

function parseBlockLine(line: string): Omit<Decision, 'index'> | null {
  const parts = line.trim().replace(/^[-*•\s]+/, '').split('|');
  const get = (name: string) => parts.map((p) => field(p, name)).find((v) => v !== null) ?? null;
  const question = get('question');
  const optionsRaw = get('options');
  const pickedRaw = get('picked');
  if (!question || optionsRaw === null || pickedRaw === null) return null;
  const options = optionsRaw.split(';').map((o) => o.trim()).filter(Boolean);
  if (options.length < 2 || options.length > 6) return null;
  const norm = (s: string) => s.trim().replace(/^["“”.]+|["“”.]+$/g, '').toLowerCase();
  const picked = options.find((o) => norm(o) === norm(pickedRaw));
  if (!picked) return null;
  return { question, options, picked, why: get('why') ?? '', status: 'decided', changed_to: null };
}

/**
 * Takes the last `[DECISIONS] … [/DECISIONS]` block (or an unclosed one at the end) out of a
 * reply: the text without it, and its valid lines. Lines that don't parse are dropped, never
 * shown; two decisions run together on one line are split at "question:".
 */
export function takeDecisionsBlock(text: string): { text: string; decisions: Decision[] } {
  let start = -1;
  let tagLen = 0;
  for (const m of text.matchAll(OPEN_RE)) {
    start = m.index ?? -1;
    tagLen = m[0].length;
  }
  if (start < 0) return { text, decisions: [] };
  const after = text.slice(start + tagLen);
  const close = CLOSE_RE.exec(after);
  const body = close ? after.slice(0, close.index) : after;
  const rest = close ? after.slice(close.index + close[0].length).trim() : '';
  const decisions: Decision[] = [];
  for (const line of body.split('\n').flatMap((l) => l.split(/\s+(?=question\s*:)/i))) {
    const d = parseBlockLine(line);
    if (d && decisions.length < MAX_DECISIONS) decisions.push({ index: decisions.length, ...d });
  }
  const head = text.slice(0, start).trimEnd();
  return { text: (rest ? `${head}\n\n${rest}` : head).trim(), decisions };
}

/** While a reply streams in: hides a block being written, including a half-typed opening tag. */
export function hideStreamingDecisions(text: string): string {
  const shown = takeDecisionsBlock(text).text;
  if (shown !== text) return shown;
  const tail = /\[[A-Za-z]{0,9}$/.exec(text);
  return tail && '[DECISIONS'.startsWith(tail[0].toUpperCase()) ? text.slice(0, tail.index).trimEnd() : text;
}

/**
 * What a bot message shows: its text without a raw block, and its decisions. Decisions saved on
 * the message win and can be changed; ones read from the text are read-only (the Change route
 * only knows saved ones).
 */
export function messageDecisions(text: string, meta: unknown): { text: string; decisions: Decision[]; readOnly: boolean } {
  const saved = parseDecisions(meta);
  const taken = takeDecisionsBlock(text);
  if (saved.length) return { text: taken.text, decisions: saved, readOnly: false };
  return { text: taken.text, decisions: taken.decisions, readOnly: true };
}

/** What the chip says: the current choice. */
export const decisionLabel = (d: Decision): string => (d.status === 'changed' ? `You chose: ${d.changed_to}` : `Decided: ${d.picked}`);

/** The options the person can switch to: every other one. Empty means no Change button. */
export const changeOptions = (d: Decision): string[] => (d.status === 'changed' ? [] : d.options.filter((o) => o !== d.picked));

/** Teammates the bot asked before deciding, when the reply came from the decide-first gate. */
export function consultedNames(raw: unknown): string[] {
  const df = parseMetaObject(raw)?.decide_first;
  if (!df || typeof df !== 'object') return [];
  const c = (df as Record<string, unknown>).consulted;
  return Array.isArray(c) ? c.filter((n): n is string => typeof n === 'string' && n.trim() !== '') : [];
}

/** "Asked Designer and Writer first." */
export function consultedLine(names: string[]): string {
  if (!names.length) return '';
  const list = names.length === 1 ? names[0] : `${names.slice(0, -1).join(', ')} and ${names[names.length - 1]}`;
  return `Asked ${list} first.`;
}

export interface ChangeResult<M> {
  message: M;
  prompt: string;
}

export const decisionsApi = {
  /** Records the person's new pick; the client then sends `prompt` to the same chat. */
  change: <M>(messageId: string, index: number, picked: string) =>
    request<ChangeResult<M>>('POST', `/api/messages/${encodeURIComponent(messageId)}/decisions/${index}/change`, { picked }),
};

// ---------------------------------------------------------------------------
// "Team is deciding…"

export interface Deciding {
  kwantId: string;
  heldTurnId: string | null;
  consulted: string[];
}

/** Chats where a held reply is being decided, by context id. */
export type DecidingMap = Readonly<Record<string, Deciding>>;

/** Applies a `decide_updated` event; returns the same map for anything else. */
export function applyDecideEvent(map: DecidingMap, ev: { type?: string; data?: any }): DecidingMap {
  if (ev.type !== 'decide_updated' && ev.type !== 'DecideUpdated') return map;
  const d = ev.data ?? {};
  const ctx: unknown = d.context_id ?? d.thread_id ?? d.kwant_id;
  if (typeof ctx !== 'string' || !ctx) return map;
  if (d.state === 'deciding') {
    return {
      ...map,
      [ctx]: {
        kwantId: str(d.kwant_id),
        heldTurnId: typeof d.held_turn_id === 'string' ? d.held_turn_id : null,
        consulted: Array.isArray(d.consulted) ? d.consulted.filter((n: unknown): n is string => typeof n === 'string') : (map[ctx]?.consulted ?? []),
      },
    };
  }
  if (!(ctx in map)) return map;
  const next = { ...map };
  delete next[ctx];
  return next;
}

/** After a (re)connect: chats with a consult or decide turn running (`purpose: "decide"`). */
export function hydrateDeciding(turns: Array<{ context_id?: string; kwant_id?: string; purpose?: string }> | undefined): DecidingMap {
  const out: Record<string, Deciding> = {};
  for (const t of turns ?? []) {
    if (t?.purpose !== 'decide' || !t.context_id || out[t.context_id]) continue;
    out[t.context_id] = { kwantId: t.kwant_id ?? '', heldTurnId: null, consulted: [] };
  }
  return out;
}

/** The line under "Team is deciding…". */
export function decidingDetail(botName: string, consulted: string[]): string {
  const who = botName || 'The bot';
  if (!consulted.length) return `${who} is running the checks it listed and making the call, instead of asking you.`;
  return `${who} is checking with ${consultedLine(consulted).replace(/^Asked /, '').replace(/ first\.$/, '')} and making the call, instead of asking you.`;
}

// ---------------------------------------------------------------------------
// "While you were away"

export interface WorkAheadMeta {
  kind: 'work_ahead';
  date: string;
  run_id: string;
}

export function parseWorkAhead(raw: unknown): WorkAheadMeta | null {
  const m = parseMetaObject(raw);
  if (!m || m.kind !== 'work_ahead') return null;
  return { kind: 'work_ahead', date: str(m.date), run_id: str(m.run_id) };
}

/** One plain line for the collapsed message: the first sentence-ish line without markdown. */
export function workAheadSummary(text: string, max = 140): string {
  const line =
    text
      .split('\n')
      .map((l) => l.replace(/^[#>*\-\s]+/, '').replace(/[*_`]/g, '').trim())
      .find(Boolean) ?? '';
  return line.length > max ? `${line.slice(0, max - 1).trimEnd()}…` : line;
}

// ---------------------------------------------------------------------------
// ask_user cards: why only the person can answer

export type WhyYou = 'irreversible' | 'outside' | 'money' | 'secret_or_signin' | 'taste_no_signal' | 'missing_after_research';

const WHY_YOU: Record<WhyYou, string> = {
  irreversible: 'It can’t be undone.',
  outside: 'It sends something or contacts someone.',
  money: 'It spends money.',
  secret_or_signin: 'It needs a password or a sign-in.',
  taste_no_signal: 'It’s a matter of taste and there’s nothing to go on yet.',
  missing_after_research: 'The bot looked and couldn’t find it.',
};

export const whyYouText = (v: unknown): string | null => (typeof v === 'string' && v in WHY_YOU ? WHY_YOU[v as WhyYou] : null);

// ---------------------------------------------------------------------------
// App settings

export const WORK_AHEAD_CAP_MIN = 1;
export const WORK_AHEAD_CAP_MAX = 10;

export interface ProactiveSettings {
  decide_first: boolean;
  work_ahead: boolean;
  work_ahead_daily_cap: number;
}

/** The three settings from GET /api/settings; null on a server without them. */
export function parseProactiveSettings(data: unknown): ProactiveSettings | null {
  if (!data || typeof data !== 'object') return null;
  const d = data as Record<string, unknown>;
  if (typeof d.decide_first !== 'boolean') return null;
  const cap = typeof d.work_ahead_daily_cap === 'number' && Number.isInteger(d.work_ahead_daily_cap) ? d.work_ahead_daily_cap : 3;
  return {
    decide_first: d.decide_first,
    work_ahead: typeof d.work_ahead === 'boolean' ? d.work_ahead : true,
    work_ahead_daily_cap: Math.min(WORK_AHEAD_CAP_MAX, Math.max(WORK_AHEAD_CAP_MIN, cap)),
  };
}