Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

4.7 KB
Raw Blame
// SPDX-License-Identifier: FSL-1.1-MIT
// Copyright (c) 2026 Cua AI, Inc.

/**
 * `@name` in the Space thread's composer: the mention being typed at the
 * caret (the picker's query), putting the picked name in its place, and
 * splitting text so mentions draw as chips. The daemon parses mentions
 * from the posted text itself (DESIGN §4.1); these only drive the page.
 */

/** A name a mention can complete to. */
export interface MentionCandidate {
  /** What goes after `@` (`claude`, `grace`). */
  handle: string;
  /** How the picker shows it (`Claude Code`, `Grace`). */
  label: string;
  kind: "agent" | "person";
  /** An agent's kind (`claude-code`), for its mark when it isn't in the Space yet. */
  harness?: string;
  /** Under the name for an agent not in the Space yet ("Starts in this Space", "Not connected"). */
  detail?: string;
  /** Listed but not pickable (`@everyone` for someone who can't give tasks); `detail` says why. */
  disabled?: boolean;
}

/** `@` starts a mention at the start or after a space or an opening
 * bracket, never inside a word (`ada@example.com` is not one). */
const BEFORE = /[\s([{"']/;
const HANDLE = /^[\w.-]*$/;

/** The mention the caret is in: where its `@` is and what follows it. */
export function activeMention(text: string, caret: number): { start: number; query: string } | null {
  const at = text.lastIndexOf("@", Math.max(0, caret - 1));
  if (at < 0 || caret <= at) return null;
  if (at > 0 && !BEFORE.test(text[at - 1]!)) return null;
  const query = text.slice(at + 1, caret);
  return HANDLE.test(query) ? { start: at, query } : null;
}

/** The candidates for `query`: those whose handle or a word of their
 * label starts with it, agents first, in the order given. */
export function matchMentions(query: string, candidates: readonly MentionCandidate[]): MentionCandidate[] {
  const q = query.toLowerCase();
  const hit = (c: MentionCandidate) => !q || c.handle.toLowerCase().startsWith(q) || c.label.toLowerCase().split(/\s+/).some((w) => w.startsWith(q));
  const found = candidates.filter(hit);
  return [...found.filter((c) => c.kind === "agent"), ...found.filter((c) => c.kind !== "agent")];
}

/** `text` with the mention at `start` (as far as `caret`) replaced by
 * `@handle ` and where the caret goes after it. */
export function insertMention(text: string, start: number, caret: number, handle: string): { text: string; caret: number } {
  const after = text.slice(caret).replace(/^[\w.-]*/, "");
  const inserted = `@${handle}${after.startsWith(" ") ? "" : " "}`;
  return { text: text.slice(0, start) + inserted + after, caret: start + inserted.length };
}

export type MentionSegment = { text: string; mention: false } | { text: string; mention: true; handle: string };

const MENTION = /@([\w][\w.-]*)/g;

/** `text` cut into plain runs and `@handle` mentions. With `handles`, only
 * those names count (a stray `@` stays plain). A trailing `.` ends a mention. */
export function mentionSegments(text: string, handles?: ReadonlySet<string>): MentionSegment[] {
  const out: MentionSegment[] = [];
  let last = 0;
  for (const m of text.matchAll(MENTION)) {
    const at = m.index!;
    if (at > 0 && !BEFORE.test(text[at - 1]!)) continue;
    const handle = m[1]!.replace(/[.-]+$/, "");
    if (handles && !handles.has(handle.toLowerCase())) continue;
    if (at > last) out.push({ text: text.slice(last, at), mention: false });
    out.push({ text: `@${handle}`, mention: true, handle: handle.toLowerCase() });
    last = at + 1 + handle.length;
  }
  if (last < text.length) out.push({ text: text.slice(last), mention: false });
  return out;
}

/** The handle that addresses every agent you may task. */
export const EVERYONE = "everyone";

/** The lower-case handles `text` mentions, in order and once each. With
 * `handles`, only those names count. */
export function mentionedHandles(text: string, handles?: ReadonlySet<string>): string[] {
  const out: string[] = [];
  for (const s of mentionSegments(text, handles)) if (s.mention && !out.includes(s.handle)) out.push(s.handle);
  return out;
}

/** Whether `text` already starts with a mention of `handle` (so a Reply does not add it twice). */
export function startsWithMention(text: string, handle: string): boolean {
  const m = /^\s*@([\w][\w.-]*)/.exec(text);
  return !!m && m[1]!.replace(/[.-]+$/, "").toLowerCase() === handle.toLowerCase();
}

/** `text` addressed to `handles`: `@a @b ` in front, unless `text` already starts with `@handle` (one handle). */
export function addLeadingMention(text: string, ...handles: string[]): string {
  const missing = handles.filter((h) => !mentionedHandles(text).includes(h.toLowerCase()));
  if (!missing.length) return text;
  return `${missing.map((h) => `@${h}`).join(" ")} ${text.replace(/^\s+/, "")}`;
}