Add a per-user lead agent and make the right pane task-focused
This commit is contained in:
Vendored
+111
-41
@@ -1,4 +1,5 @@
|
||||
import { get, subscribe } from "./api.js";
|
||||
import { get, put, subscribe } from "./api.js";
|
||||
export const tabs = ["transcript", "changes", "files", "inbox", "todo"];
|
||||
export const state = {
|
||||
me: undefined,
|
||||
projects: [],
|
||||
@@ -7,9 +8,11 @@ export const state = {
|
||||
inbox: [],
|
||||
selectedTaskId: undefined,
|
||||
transcript: [],
|
||||
lastTranscriptSeq: 0,
|
||||
pinned: undefined,
|
||||
connected: false,
|
||||
focus: { tabs: ["transcript"], tab: "transcript" },
|
||||
leadSessions: [],
|
||||
leadTranscript: [],
|
||||
};
|
||||
const listeners = new Map();
|
||||
export function on(topic, fn) {
|
||||
@@ -49,6 +52,9 @@ export function findTask(id) {
|
||||
export function currentSession(taskId) {
|
||||
return state.sessions.get(taskId)?.findLast((s) => s.state !== "completed") ?? state.sessions.get(taskId)?.at(-1);
|
||||
}
|
||||
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);
|
||||
}
|
||||
@@ -79,9 +85,96 @@ export async function loadInbox() {
|
||||
state.inbox = await get("/api/inbox");
|
||||
notify("inbox");
|
||||
}
|
||||
export async function loadLeadSessions() {
|
||||
state.leadSessions = await get("/api/me/lead/sessions");
|
||||
notify("lead.sessions");
|
||||
}
|
||||
export async function loadFocus() {
|
||||
applyFocus(await get("/api/me/focus"));
|
||||
}
|
||||
export function applyFocus(f) {
|
||||
state.focus = f;
|
||||
const hash = f.task_id ? `#/task/${f.task_id}` : "#/";
|
||||
if (location.hash !== hash && !(hash === "#/" && location.hash === "")) {
|
||||
location.hash = hash;
|
||||
}
|
||||
notify("focus");
|
||||
}
|
||||
export async function setFocus(change) {
|
||||
const taskId = change.task_id === undefined ? state.focus.task_id : (change.task_id ?? undefined);
|
||||
const next = { tabs: state.focus.tabs, tab: change.tab ?? state.focus.tab };
|
||||
if (taskId) {
|
||||
next.task_id = taskId;
|
||||
}
|
||||
applyFocus(next);
|
||||
applyFocus(await put("/api/me/focus", { body: next }));
|
||||
}
|
||||
export function focusTask(taskId, tab = "transcript") {
|
||||
setFocus({ task_id: taskId, tab }).catch((err) => console.error("set focus", err));
|
||||
}
|
||||
export function focusTab(tab) {
|
||||
setFocus({ tab }).catch((err) => console.error("set focus", err));
|
||||
}
|
||||
class Feed {
|
||||
transcriptTopic;
|
||||
streamTopic;
|
||||
store;
|
||||
events = [];
|
||||
last = 0;
|
||||
buffer = [];
|
||||
timer = 0;
|
||||
constructor(transcriptTopic, streamTopic, store) {
|
||||
this.transcriptTopic = transcriptTopic;
|
||||
this.streamTopic = streamTopic;
|
||||
this.store = store;
|
||||
}
|
||||
reset() {
|
||||
clearTimeout(this.timer);
|
||||
this.timer = 0;
|
||||
this.buffer = [];
|
||||
this.events = [];
|
||||
this.last = 0;
|
||||
this.store(this.events);
|
||||
}
|
||||
push = (ev) => {
|
||||
this.buffer.push(ev);
|
||||
if (!this.timer) {
|
||||
this.timer = setTimeout(() => this.flush(), 0);
|
||||
}
|
||||
};
|
||||
flush() {
|
||||
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 = [];
|
||||
notify(this.streamTopic, ev);
|
||||
}
|
||||
this.append(stored);
|
||||
}
|
||||
append(events) {
|
||||
const fresh = events.filter((ev) => ev.seq > this.last);
|
||||
if (fresh.length === 0) {
|
||||
return;
|
||||
}
|
||||
this.events.push(...fresh);
|
||||
this.last = fresh.at(-1).seq;
|
||||
notify(this.transcriptTopic, fresh);
|
||||
}
|
||||
}
|
||||
const taskFeed = new Feed("transcript", "stream", (events) => {
|
||||
state.transcript = events;
|
||||
});
|
||||
const leadFeed = new Feed("lead.transcript", "lead.stream", (events) => {
|
||||
state.leadTranscript = events;
|
||||
});
|
||||
let closeTaskStream;
|
||||
let taskBuffer = [];
|
||||
let taskFlush = 0;
|
||||
const visitsKey = "byre.visits";
|
||||
export function lastVisits() {
|
||||
return JSON.parse(localStorage.getItem(visitsKey) ?? "{}");
|
||||
@@ -97,49 +190,18 @@ export function selectTask(taskId) {
|
||||
markVisited(taskId);
|
||||
closeTaskStream?.();
|
||||
closeTaskStream = undefined;
|
||||
clearTimeout(taskFlush);
|
||||
taskFlush = 0;
|
||||
taskBuffer = [];
|
||||
taskFeed.reset();
|
||||
state.selectedTaskId = taskId;
|
||||
state.transcript = [];
|
||||
state.lastTranscriptSeq = 0;
|
||||
state.pinned = undefined;
|
||||
notify("selection");
|
||||
if (!taskId) {
|
||||
return;
|
||||
}
|
||||
closeTaskStream = subscribe(0, taskId, queueTaskEvent, () => undefined);
|
||||
closeTaskStream = subscribe(0, { task_id: taskId }, taskFeed.push, () => undefined);
|
||||
}
|
||||
function queueTaskEvent(ev) {
|
||||
taskBuffer.push(ev);
|
||||
if (!taskFlush) {
|
||||
taskFlush = setTimeout(flushTaskEvents, 0);
|
||||
}
|
||||
}
|
||||
function flushTaskEvents() {
|
||||
const events = taskBuffer;
|
||||
taskBuffer = [];
|
||||
taskFlush = 0;
|
||||
let stored = [];
|
||||
for (const ev of events) {
|
||||
if (ev.seq !== 0) {
|
||||
stored.push(ev);
|
||||
continue;
|
||||
}
|
||||
appendTranscript(stored);
|
||||
stored = [];
|
||||
notify("stream", ev);
|
||||
}
|
||||
appendTranscript(stored);
|
||||
}
|
||||
function appendTranscript(events) {
|
||||
const fresh = events.filter((ev) => ev.seq > state.lastTranscriptSeq);
|
||||
if (fresh.length === 0) {
|
||||
return;
|
||||
}
|
||||
state.transcript.push(...fresh);
|
||||
state.lastTranscriptSeq = fresh.at(-1).seq;
|
||||
notify("transcript", fresh);
|
||||
export function startLeadStream() {
|
||||
leadFeed.reset();
|
||||
subscribe(0, { lead: true }, leadFeed.push, () => undefined);
|
||||
}
|
||||
export function handleEvent(ev) {
|
||||
if (ev.seq === 0) {
|
||||
@@ -166,7 +228,10 @@ export function handleEvent(ev) {
|
||||
case "session.created":
|
||||
case "session.state":
|
||||
case "session.model":
|
||||
if (ev.task_id) {
|
||||
if (ev.owner) {
|
||||
void loadLeadSessions();
|
||||
}
|
||||
else if (ev.task_id) {
|
||||
void loadSessions(ev.task_id);
|
||||
}
|
||||
break;
|
||||
@@ -174,6 +239,11 @@ export function handleEvent(ev) {
|
||||
case "inbox.updated":
|
||||
void loadInbox();
|
||||
break;
|
||||
case "focus.updated":
|
||||
if (ev.actor !== state.me?.login) {
|
||||
applyFocus(ev.payload);
|
||||
}
|
||||
break;
|
||||
}
|
||||
notify("event", ev);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user