443 lines
14 KiB
JavaScript
443 lines
14 KiB
JavaScript
import { get, put, subscribe } from "./api.js";
|
|
import { notifyInbox, notifyTurn } from "./notifications.js";
|
|
export const tabs = ["transcript", "changes", "files", "terminal", "inbox", "reviews", "workflow", "project"];
|
|
export const state = {
|
|
me: undefined,
|
|
projects: [],
|
|
tasks: new Map(),
|
|
sessions: new Map(),
|
|
inbox: [],
|
|
selectedTaskId: undefined,
|
|
transcript: [],
|
|
pinned: undefined,
|
|
reviewed: undefined,
|
|
connected: false,
|
|
focus: { tabs: ["transcript"], tab: "transcript" },
|
|
leadSessions: [],
|
|
leadTranscript: [],
|
|
jump: undefined,
|
|
summaryReads: {},
|
|
};
|
|
const listeners = new Map();
|
|
export function on(topic, fn) {
|
|
let set = listeners.get(topic);
|
|
if (!set) {
|
|
set = new Set();
|
|
listeners.set(topic, set);
|
|
}
|
|
set.add(fn);
|
|
return () => set.delete(fn);
|
|
}
|
|
export function onEach(topics, fn) {
|
|
const offs = topics.map((t) => on(t, fn));
|
|
return () => {
|
|
for (const off of offs) {
|
|
off();
|
|
}
|
|
};
|
|
}
|
|
export function activeTasks() {
|
|
return state.projects.flatMap((project) => (state.tasks.get(project.id) ?? []).filter((t) => t.status === "active").map((task) => ({ project, task })));
|
|
}
|
|
export function notify(topic, detail) {
|
|
for (const fn of listeners.get(topic) ?? []) {
|
|
fn(detail);
|
|
}
|
|
}
|
|
export function findTask(id) {
|
|
for (const tasks of state.tasks.values()) {
|
|
const task = tasks.find((t) => t.id === id);
|
|
if (task) {
|
|
return task;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
export function currentSession(taskId) {
|
|
return state.sessions.get(taskId)?.findLast((s) => s.state !== "completed") ?? state.sessions.get(taskId)?.at(-1);
|
|
}
|
|
const workingTitles = {
|
|
running: "The agent is mid-turn",
|
|
review_pending: "A review of the turn's work is about to start",
|
|
reviewing: "A review is running",
|
|
discarding: "Discarding",
|
|
};
|
|
export function taskState(task) {
|
|
const open = state.inbox.filter((item) => item.task_id === task.id);
|
|
if (open.some((item) => item.kind === "question")) {
|
|
return { kind: "answer", label: "Needs your answer", title: "The agent asked a question in the inbox" };
|
|
}
|
|
if (task.merging) {
|
|
return { kind: "merging", label: "Merging", title: "The merge turn is running" };
|
|
}
|
|
if (task.merging_into_parent) {
|
|
return { kind: "merging", label: "Merging", title: "Waiting for the parent task's agent to merge it" };
|
|
}
|
|
if (task.merge_queued_at !== undefined) {
|
|
return { kind: "held", label: "Merge held", title: "Waiting for the merge in progress in this project to finish" };
|
|
}
|
|
if (open.length > 0) {
|
|
return { kind: "error", label: "Error", title: "The agent's session failed or hit a rate limit; see the inbox" };
|
|
}
|
|
const working = workingTitles[task.state];
|
|
if (working !== undefined) {
|
|
return { kind: "working", label: "Working", title: working };
|
|
}
|
|
return { kind: "idle", label: "Idle", title: "Waiting for a message" };
|
|
}
|
|
export function currentLead() {
|
|
return state.leadSessions.findLast((s) => s.state !== "completed") ?? state.leadSessions.at(-1);
|
|
}
|
|
export function snapshots() {
|
|
return state.transcript.filter((e) => e.kind === "snapshot.created").map((e) => e.payload);
|
|
}
|
|
export function pinSnapshot(n) {
|
|
state.pinned = n;
|
|
notify("pin");
|
|
}
|
|
export function pickReview(n) {
|
|
state.reviewed = n;
|
|
notify("review");
|
|
}
|
|
export function showReview(n) {
|
|
pickReview(n);
|
|
focusTab("reviews");
|
|
}
|
|
export async function loadMe() {
|
|
state.me = await get("/api/me");
|
|
notify("me");
|
|
}
|
|
export async function loadProjects() {
|
|
state.projects = await get("/api/projects");
|
|
await Promise.all(state.projects.map((p) => loadTasks(p.id)));
|
|
notify("projects");
|
|
}
|
|
const requestSeqs = new Map();
|
|
function claimRequest(key) {
|
|
const seq = (requestSeqs.get(key) ?? 0) + 1;
|
|
requestSeqs.set(key, seq);
|
|
return () => requestSeqs.get(key) === seq;
|
|
}
|
|
export async function loadTasks(projectId) {
|
|
const latest = claimRequest(`tasks:${projectId}`);
|
|
const tasks = await get("/api/projects/{projectId}/tasks", { path: { projectId } });
|
|
if (!latest()) {
|
|
return;
|
|
}
|
|
state.tasks.set(projectId, tasks);
|
|
for (const t of tasks) {
|
|
if (t.status === "active") {
|
|
taskFeed(t.id);
|
|
}
|
|
}
|
|
dropClosedFeeds();
|
|
await Promise.all(tasks.filter((t) => t.status === "active").map((t) => loadSessions(t.id)));
|
|
notify("tasks");
|
|
}
|
|
export async function loadSessions(taskId) {
|
|
const latest = claimRequest(`sessions:${taskId}`);
|
|
const sessions = await get("/api/tasks/{taskId}/sessions", { path: { taskId } });
|
|
if (!latest()) {
|
|
return;
|
|
}
|
|
state.sessions.set(taskId, sessions);
|
|
notify("sessions", taskId);
|
|
}
|
|
export async function loadInbox() {
|
|
const latest = claimRequest("inbox");
|
|
const inbox = await get("/api/inbox");
|
|
if (!latest()) {
|
|
return;
|
|
}
|
|
state.inbox = inbox;
|
|
notify("inbox");
|
|
}
|
|
export async function loadLeadSessions() {
|
|
state.leadSessions = await get("/api/me/lead/sessions");
|
|
notify("lead.sessions");
|
|
}
|
|
export async function loadFocus() {
|
|
const latest = claimRequest("focus");
|
|
const focus = await get("/api/me/focus");
|
|
if (!latest()) {
|
|
return;
|
|
}
|
|
applyFocus(focus);
|
|
}
|
|
export function applyFocus(f) {
|
|
state.focus = f;
|
|
const hash = f.task_id ? `#/task/${f.task_id}` : f.project_id ? `#/project/${f.project_id}` : "#/";
|
|
if (location.hash !== hash && !(hash === "#/" && location.hash === "")) {
|
|
location.hash = hash;
|
|
}
|
|
notify("focus");
|
|
}
|
|
export function focusedProject() {
|
|
return state.projects.find((p) => p.id === state.focus.project_id);
|
|
}
|
|
export async function setFocus(change) {
|
|
const taskId = change.task_id === undefined ? state.focus.task_id : (change.task_id ?? undefined);
|
|
const projectId = taskId ? findTask(taskId)?.project_id : change.project_id === undefined ? state.focus.project_id : (change.project_id ?? undefined);
|
|
const next = { tabs: state.focus.tabs, tab: change.tab ?? state.focus.tab };
|
|
if (taskId) {
|
|
next.task_id = taskId;
|
|
}
|
|
if (projectId) {
|
|
next.project_id = projectId;
|
|
}
|
|
const latest = claimRequest("focus");
|
|
applyFocus(next);
|
|
const focus = await put("/api/me/focus", { body: next });
|
|
if (!latest()) {
|
|
return;
|
|
}
|
|
applyFocus(focus);
|
|
}
|
|
export function focusTask(taskId, tab = "transcript") {
|
|
setFocus({ task_id: taskId, tab }).catch((err) => console.error("set focus", err));
|
|
}
|
|
export function focusProject(projectId) {
|
|
setFocus({ task_id: null, project_id: projectId, tab: "project" }).catch((err) => console.error("set focus", err));
|
|
}
|
|
export function focusTab(tab) {
|
|
setFocus({ tab }).catch((err) => console.error("set focus", err));
|
|
}
|
|
class Feed {
|
|
transcriptTopic;
|
|
streamTopic;
|
|
readyTopic;
|
|
shown;
|
|
events = [];
|
|
ready = false;
|
|
last = 0;
|
|
buffer = [];
|
|
timer = 0;
|
|
constructor(transcriptTopic, streamTopic, readyTopic, shown) {
|
|
this.transcriptTopic = transcriptTopic;
|
|
this.streamTopic = streamTopic;
|
|
this.readyTopic = readyTopic;
|
|
this.shown = shown;
|
|
}
|
|
push = (ev) => {
|
|
if (ev.seq === 0 && (!this.ready || !this.shown())) {
|
|
return;
|
|
}
|
|
this.buffer.push(ev);
|
|
if (this.ready && !this.timer) {
|
|
this.timer = setTimeout(() => this.flush(), 0);
|
|
}
|
|
};
|
|
load = (history) => {
|
|
if (this.ready) {
|
|
return;
|
|
}
|
|
this.ready = true;
|
|
this.buffer = [...history, ...this.buffer];
|
|
this.flush();
|
|
if (this.shown()) {
|
|
notify(this.readyTopic);
|
|
}
|
|
};
|
|
flush() {
|
|
clearTimeout(this.timer);
|
|
const events = this.buffer;
|
|
this.buffer = [];
|
|
this.timer = 0;
|
|
let stored = [];
|
|
for (const ev of events) {
|
|
if (ev.seq !== 0) {
|
|
stored.push(ev);
|
|
continue;
|
|
}
|
|
this.append(stored);
|
|
stored = [];
|
|
if (this.shown()) {
|
|
notify(this.streamTopic, ev);
|
|
}
|
|
}
|
|
this.append(stored);
|
|
}
|
|
append(events) {
|
|
const fresh = [];
|
|
for (const ev of events) {
|
|
if (ev.seq > this.last) {
|
|
fresh.push(ev);
|
|
this.last = ev.seq;
|
|
}
|
|
}
|
|
if (fresh.length === 0) {
|
|
return;
|
|
}
|
|
this.events.push(...fresh);
|
|
if (this.shown()) {
|
|
notify(this.transcriptTopic, fresh);
|
|
}
|
|
}
|
|
}
|
|
const historyPage = 1000;
|
|
const taskFeeds = new Map();
|
|
const leadFeed = new Feed("lead.transcript", "lead.stream", "lead.ready", () => true);
|
|
state.leadTranscript = leadFeed.events;
|
|
async function loadHistory(taskId, feed) {
|
|
const events = [];
|
|
for (;;) {
|
|
const page = await get("/api/tasks/{taskId}/events", { path: { taskId }, query: { after: events.at(-1)?.seq ?? 0, limit: historyPage } });
|
|
events.push(...page);
|
|
if (page.length < historyPage) {
|
|
break;
|
|
}
|
|
}
|
|
feed.load(events);
|
|
}
|
|
const historyLoads = new Set();
|
|
function taskFeed(taskId) {
|
|
let feed = taskFeeds.get(taskId);
|
|
if (!feed) {
|
|
feed = new Feed("transcript", "stream", "ready", () => state.selectedTaskId === taskId);
|
|
taskFeeds.set(taskId, feed);
|
|
}
|
|
if (feed.ready || historyLoads.has(feed)) {
|
|
return feed;
|
|
}
|
|
const loading = feed;
|
|
historyLoads.add(loading);
|
|
loadHistory(taskId, loading)
|
|
.catch((err) => console.error("load task history", taskId, err))
|
|
.finally(() => historyLoads.delete(loading));
|
|
return feed;
|
|
}
|
|
function dropClosedFeeds() {
|
|
for (const taskId of taskFeeds.keys()) {
|
|
const task = findTask(taskId);
|
|
if (taskId !== state.selectedTaskId && task && task.status !== "active") {
|
|
taskFeeds.delete(taskId);
|
|
}
|
|
}
|
|
}
|
|
export function transcriptLoading() {
|
|
return state.selectedTaskId !== undefined && taskFeeds.get(state.selectedTaskId)?.ready !== true;
|
|
}
|
|
export function leadLoading() {
|
|
return !leadFeed.ready;
|
|
}
|
|
const visitsKey = "byre.visits";
|
|
export function lastVisits() {
|
|
return JSON.parse(localStorage.getItem(visitsKey) ?? "{}");
|
|
}
|
|
function markVisited(taskId) {
|
|
if (!taskId) {
|
|
return;
|
|
}
|
|
localStorage.setItem(visitsKey, JSON.stringify({ ...lastVisits(), [taskId]: Date.now() }));
|
|
}
|
|
export async function loadSummaryReads() {
|
|
state.summaryReads = await get("/api/me/summary-reads");
|
|
notify("summaries");
|
|
}
|
|
export function unreadSummary(task) {
|
|
return task.workflow === undefined && task.summary_seq > (state.summaryReads[task.id] ?? 0);
|
|
}
|
|
function noteSummaryRead(taskId, seq) {
|
|
if ((state.summaryReads[taskId] ?? 0) >= seq) {
|
|
return false;
|
|
}
|
|
state.summaryReads[taskId] = seq;
|
|
notify("summaries");
|
|
return true;
|
|
}
|
|
export function markSummaryRead(taskId, seq) {
|
|
if (!noteSummaryRead(taskId, seq)) {
|
|
return;
|
|
}
|
|
put("/api/tasks/{taskId}/summary-read", { path: { taskId }, body: { seq } }).catch((err) => console.error("mark summary read", err));
|
|
}
|
|
export function openSummary(task) {
|
|
state.jump = { taskId: task.id, seq: task.summary_seq };
|
|
focusTask(task.id, "transcript");
|
|
notify("jump");
|
|
}
|
|
export function selectTask(taskId) {
|
|
markVisited(state.selectedTaskId);
|
|
markVisited(taskId);
|
|
state.selectedTaskId = taskId;
|
|
state.transcript = taskId ? taskFeed(taskId).events : [];
|
|
state.pinned = undefined;
|
|
state.reviewed = undefined;
|
|
dropClosedFeeds();
|
|
notify("selection");
|
|
}
|
|
export function startLeadStream() {
|
|
subscribe(0, { lead: true }, leadFeed.push, () => undefined, () => leadFeed.load([]));
|
|
}
|
|
export function handleEvent(ev) {
|
|
if (ev.task_id) {
|
|
taskFeeds.get(ev.task_id)?.push(ev);
|
|
}
|
|
if (ev.seq === 0) {
|
|
notify("event", ev);
|
|
return;
|
|
}
|
|
switch (ev.kind) {
|
|
case "user.updated":
|
|
if (ev.payload["login"] === state.me?.login) {
|
|
void loadMe();
|
|
}
|
|
break;
|
|
case "project.created":
|
|
void loadProjects();
|
|
break;
|
|
case "task.created":
|
|
case "task.merge_title":
|
|
case "task.merge_queued":
|
|
case "task.merge_unqueued":
|
|
case "task.parent_merge_started":
|
|
case "task.parent_merge_stopped":
|
|
case "task.merge_started":
|
|
case "task.merge_ended":
|
|
case "task.discard_started":
|
|
case "task.merged":
|
|
case "task.discarded":
|
|
case "task.state":
|
|
if (ev.project_id) {
|
|
void loadTasks(ev.project_id);
|
|
}
|
|
break;
|
|
case "session.created":
|
|
case "session.state":
|
|
case "session.model":
|
|
if (ev.owner) {
|
|
void loadLeadSessions();
|
|
}
|
|
else if (ev.task_id) {
|
|
void loadSessions(ev.task_id);
|
|
}
|
|
break;
|
|
case "inbox.created":
|
|
notifyInbox(ev.payload);
|
|
void loadInbox();
|
|
break;
|
|
case "inbox.updated":
|
|
void loadInbox();
|
|
break;
|
|
case "turn.ended": {
|
|
notifyTurn(ev);
|
|
const task = ev.payload["summary"] === true && ev.task_id ? findTask(ev.task_id) : undefined;
|
|
if (task && ev.seq > task.summary_seq) {
|
|
task.summary_seq = ev.seq;
|
|
notify("summaries");
|
|
}
|
|
break;
|
|
}
|
|
case "summary.read":
|
|
noteSummaryRead(String(ev.payload["task_id"]), Number(ev.payload["seq"]));
|
|
break;
|
|
case "focus.updated":
|
|
if (ev.actor !== state.me?.login) {
|
|
claimRequest("focus");
|
|
applyFocus(ev.payload);
|
|
}
|
|
break;
|
|
}
|
|
notify("event", ev);
|
|
}
|
|
//# sourceMappingURL=state.js.map
|