Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

5.7 KB
Raw Blame
import cronstrue from "cronstrue";
import { DEFAULT_CRON_TIMEZONE } from "../../cron/schedule.ts";
import type { CandidateDestination, Cron, Monitor, Webhook } from "../../types.ts";
import type { DirectoryChannel, DirectoryMember } from "../../directory/directory-store.ts";

export function deliveryMenu(candidates: CandidateDestination[], defaultKey: string | undefined): string {
  const lines = candidates
    .toSorted((a, b) => a.key.localeCompare(b.key))
    .map(
      (c) =>
        `- ${c.label}${c.key === defaultKey ? " (default — where we're talking now)" : ""}: destinationKey \`${c.key}\``,
    );
  return [
    "## Where scheduled tasks post",
    "When you schedule a cron here, it delivers to the default below unless you pass a different",
    "`destinationKey` to the `cron` tool on create, or move an existing one with `cron`",
    "action=retarget. A `destinationKey` may only come from this list — these are the destinations",
    "authenticated for this conversation. To schedule something privately for whoever asked instead,",
    'pass `scope:"personal"` (it runs at their personal scope and DMs them).',
    ...lines,
  ].join("\n");
}

export function renderProjectHomeChannel(channelName: string): string {
  const name = channelName.replace(/^#/, "");
  return [
    "## Project home channel",
    `This project is linked to the Slack channel #${name} — that channel is where the project's people follow the work.`,
    `Treat it as the default external audience: schedule deliveries there (the \`cron\` tool's \`channel: "${name}"\`), and when asked to report out or notify the team, post there with \`reach\` (\`channel: "${name}"\`).`,
    "Project membership follows the channel: everyone in the channel is a member of this project, and people joining or leaving the channel join or leave the project with it.",
  ].join("\n");
}

function localIsoTime(timezone: string, nowMs: number): string {
  const parts = new Intl.DateTimeFormat("en-CA", {
    timeZone: timezone,
    year: "numeric",
    month: "2-digit",
    day: "2-digit",
    hour: "2-digit",
    minute: "2-digit",
    second: "2-digit",
    hourCycle: "h23",
    timeZoneName: "longOffset",
  }).formatToParts(nowMs);
  const fields = Object.fromEntries(parts.map(({ type, value }) => [type, value]));
  const offset = fields.timeZoneName === "GMT" ? "+00:00" : fields.timeZoneName!.slice(3);
  return `${fields.year}-${fields.month}-${fields.day}T${fields.hour}:${fields.minute}:${fields.second}${offset}`;
}

function cronSchedulePromptLabel(c: Cron, timezone: string): string {
  const schedule = c.schedule;
  if (schedule.cron)
    return `${cronstrue.toString(schedule.cron.replaceAll("#L", "L"))} (${schedule.timezone ?? DEFAULT_CRON_TIMEZONE})`;
  if (schedule.everyMs != null) {
    for (const [size, unit] of [
      [3_600_000, "hours"],
      [60_000, "minutes"],
      [1000, "seconds"],
    ] as const) {
      if (schedule.everyMs % size === 0) return `every ${schedule.everyMs / size} ${unit}`;
    }
    return `every ${schedule.everyMs} milliseconds`;
  }
  return `once at ${localIsoTime(timezone, schedule.firstFireAt ?? c.createdAt)}`;
}

export function renderStandingObligations(
  crons: Cron[],
  webhooks: Webhook[],
  monitors: Monitor[],
  timezone = DEFAULT_CRON_TIMEZONE,
): string {
  const summary = (text: string) => (text.length > 100 ? `${text.slice(0, 97)}…` : text);
  const lines = [
    ...crons
      .toSorted((a, b) => a.id.localeCompare(b.id))
      .map((c) => `- ${c.title || "Untitled scheduled task"} — ${cronSchedulePromptLabel(c, timezone)}`),
    ...webhooks.toSorted((a, b) => a.id.localeCompare(b.id)).map((w) => `- Webhook: ${summary(w.action)}`),
    ...monitors
      .toSorted((a, b) => a.id.localeCompare(b.id))
      .map((m) => `- Job watch: ${summary(m.purpose ?? m.command)}`),
  ];
  return [
    "## Already scheduled here",
    ...(lines.length ? lines : ["No active scheduled work was found for this conversation."]),
  ].join("\n");
}

const REACH_ROSTER_CAP = 30;
export function renderReachRoster(channels: DirectoryChannel[], displayName: string): string | null {
  if (channels.length === 0) return null;
  const shown = channels
    .toSorted((a, b) => a.channelId.localeCompare(b.channelId))
    .slice(0, REACH_ROSTER_CAP)
    .map((c) => `#${c.name}`)
    .join(", ");
  const more =
    channels.length > REACH_ROSTER_CAP ? ` …and ${channels.length - REACH_ROSTER_CAP} more — name one to check` : "";
  return [
    "## Other computers you can reach",
    `Rooms you and ${displayName} share: ${shown}${more}`,
    "This conversation's computer is where you work. Other rooms' computers are places you visit:",
    'read, search, fetch — don\'t rearrange. Run a command there with execute(scope: "#room").',
    "Say where anything you bring back came from.",
  ].join("\n");
}

const ROSTER_CAP = 20;
export function renderConversationRoster(members: DirectoryMember[]): string | null {
  if (members.length === 0) return null;
  const shown = members.toSorted((a, b) => a.principalId.localeCompare(b.principalId)).slice(0, ROSTER_CAP);
  const lines = shown.map((m) => `- ${m.displayName} (${m.principalId})`);
  const more = members.length > ROSTER_CAP ? `\n…and ${members.length - ROSTER_CAP} more in this conversation.` : "";
  return (
    [
      "## Who's in this conversation",
      "The people party to this conversation, from the company directory. Each is a distinct, real person — when a request names someone, it means this specific person, not anyone who merely posted here:",
      ...lines,
    ].join("\n") + more
  );
}

export function currentTimeBlock(timezone: string, nowMs: number): string {
  return `## The user's local time\n${localIsoTime(timezone, nowMs)} (${timezone})`;
}