Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

4.6 KB
Raw Blame
/**
 * Pure state of the surfaces that list the Project's scheduled tasks: how the filter chips
 * bucket the server's display statuses, what the search box matches, which state glyph a row
 * wears, and the chips' wording. Kept apart from the components so the rules run in the
 * node-only unit tests (test/schedule-panel-state.test.ts), and shared by both listers — the
 * chat dock's panel and the scheduled-tasks page — so the two filter one list the same way.
 */
import type { ScheduleItem, ScheduleStatus } from "@adelie/server/api";
import { S } from "../../lib/strings";

export type ScheduleFilter = "all" | "active" | "paused";

export const SCHEDULE_FILTERS: readonly ScheduleFilter[] = ["all", "active", "paused"];

/** The chips' wording, from the active dictionary (read at render time, so a locale switch relabels them). */
export function scheduleFilterLabels(): Record<ScheduleFilter, string> {
  return {
    all: S.schedule.filterAll,
    active: S.schedule.filterActive,
    paused: S.schedule.filterPaused,
  };
}

/**
 * The chip a status sits under. `disabled` is "paused" — the switch on the row resumes it. The
 * scheduler archives settled tasks (done / expired / missed) out of the list, so they have no
 * chip: one still listed (a queued fire not yet sent, or an archive move that failed) shows
 * under "all" only, like an invalid file, which also carries its reason on the row.
 */
export function filterBucket(status: ScheduleStatus): Exclude<ScheduleFilter, "all"> | null {
  switch (status) {
    case "active":
      return "active";
    case "disabled":
      return "paused";
    case "done":
    case "expired":
    case "missed":
    case "invalid":
      return null;
  }
}

/**
 * The tasks bound to one Session. New-Session tasks belong to the agent as a whole and stay on
 * its settings tab. Generic in the item so the caller keeps what it passed in: the list being
 * narrowed carries the agent that owns each task, and the panel needs it to write the file back.
 */
export function sessionSchedules<T extends Pick<ScheduleItem, "sessionId">>(
  items: readonly T[],
  sessionId: string,
): T[] {
  return items.filter((item) => item.sessionId === sessionId);
}

/**
 * The Sessions that have a task still to fire — the rows the sidebar gives an alarm clock. It runs
 * over the Project's whole list, so it marks the Sessions of every Agent, which is the point: the
 * list draws them all, and a row's mark must not depend on which Agent is current. Session ids are
 * globally unique, so the set needs no agent beside them. Same binding rule as `sessionSchedules`,
 * so the mark and the panel can never disagree about which tasks belong to a conversation. "Still
 * to fire" is the server's `nextFireAt`: it is set only while a next run is on the calendar, and
 * absent for a task that is switched off, past its end time, invalid, or a one-off that has
 * already run — every case the row should say nothing about, and one rule rather than four
 * re-derived here. A set rather than a per-Session predicate: the sidebar asks the same question
 * of every row it draws.
 */
export function pendingScheduleSessions(items: readonly ScheduleItem[]): Set<string> {
  const ids = new Set<string>();
  for (const item of items) {
    if (item.sessionId === undefined || item.nextFireAt === undefined) continue;
    ids.add(item.sessionId);
  }
  return ids;
}

/** Case-insensitive match on the task's name and its prompt; a blank query matches everything. */
export function matchesQuery(item: Pick<ScheduleItem, "name" | "prompt">, query: string): boolean {
  const q = query.trim().toLowerCase();
  if (q === "") return true;
  return item.name.toLowerCase().includes(q) || item.prompt.toLowerCase().includes(q);
}

/** Generic in the item for the same reason as `sessionSchedules`: filtering must not strip fields. */
export function filterSchedules<T extends Pick<ScheduleItem, "status" | "name" | "prompt">>(
  items: readonly T[],
  filter: ScheduleFilter,
  query: string,
): T[] {
  return items.filter(
    (item) =>
      (filter === "all" || filterBucket(item.status) === filter) && matchesQuery(item, query),
  );
}

export type ScheduleGlyph = "play" | "pause" | "check" | "alert";

/** The row's state mark: play for an armed task, pause for a disabled one, a check for every settled state, an alert for an invalid file. */
export function scheduleGlyph(status: ScheduleStatus): ScheduleGlyph {
  switch (status) {
    case "active":
      return "play";
    case "disabled":
      return "pause";
    case "done":
    case "expired":
    case "missed":
      return "check";
    case "invalid":
      return "alert";
  }
}