OXYGENHUB
# 0bfe88762caa spectra-vscode / src / views.ts
4.8 KiB · last changed 0.3.0: choose which channels an agent may post in, from the Agents and Channels views 1 day ago source blame history raw
1
// The side bar: Channels, Agents, Inbox and Briefs.
2
import * as vscode from "vscode";
3
import { Model } from "./model";
4
import { when } from "./pure";
5
6
abstract class View implements vscode.TreeDataProvider<vscode.TreeItem> {
7
  private readonly emitter = new vscode.EventEmitter<void>();
8
  readonly onDidChangeTreeData = this.emitter.event;
9
  constructor(protected readonly model: Model) {
10
    model.onDidChange(() => this.emitter.fire());
11
  }
12
  getTreeItem(i: vscode.TreeItem): vscode.TreeItem {
13
    return i;
14
  }
15
  getChildren(): vscode.TreeItem[] {
16
    if (this.model.error) {
17
      const i = new vscode.TreeItem("Spectra is not answering");
18
      i.description = this.model.error;
19
      i.tooltip = this.model.error;
20
      i.iconPath = new vscode.ThemeIcon("warning");
21
      i.command = { command: "spectra.refresh", title: "Refresh" };
22
      return [i];
23
    }
24
    return this.items();
25
  }
26
  protected abstract items(): vscode.TreeItem[];
27
}
28
29
const PRESENCE: Record<string, string> = { online: "charts.green", busy: "charts.purple", idle: "charts.yellow", blocked: "charts.orange", paused: "charts.yellow", offline: "disabledForeground" };
30
31
export class Agents extends View {
32
  protected items(): vscode.TreeItem[] {
33
    return this.model.agents
34
      .filter((a) => !a.disabled)
35
      .map((a) => {
36
        const i = new vscode.TreeItem(a.name);
37
        const also = (a.channels ?? []).map((c) => `#${c}`);
38
        i.description = `${a.presence} · #${a.project}${also.length ? ` +${also.join(" ")}` : ""}${a.status ? ` · ${a.status}` : ""}`;
39
        const expires = a.delegation_expires ? `\nMay act for you until ${new Date(a.delegation_expires).toLocaleString()}` : "";
40
        const state = a.state && a.state !== "ok" ? `\n⚠ ${a.state}` : "";
41
        const may = also.length ? `\nMay also post in ${also.join(", ")}` : "";
42
        i.tooltip = `${a.display}\n${a.kind}${expires}${may}${state}`;
43
        i.iconPath = new vscode.ThemeIcon(a.state && a.state !== "ok" ? "warning" : "circle-filled", new vscode.ThemeColor(PRESENCE[a.presence] ?? "disabledForeground"));
44
        i.contextValue = "agent";
45
        i.id = `agent:${a.name}`;
46
        return i;
47
      });
48
  }
49
}
50
51
export class Channels extends View {
52
  protected items(): vscode.TreeItem[] {
53
    const mine = this.model.folderProject();
54
    const rows = [...this.model.channels].filter((c) => c.kind !== "system").sort((a, b) => Number(b.name === mine) - Number(a.name === mine));
55
    return rows.map((c) => {
56
      const i = new vscode.TreeItem(c.display);
57
      const own = this.model.agents.filter((a) => !a.disabled && a.project === c.name).map((a) => a.name);
58
      const guests = this.model.agents.filter((a) => !a.disabled && (a.channels ?? []).includes(c.name)).map((a) => a.name);
59
      i.description = c.purpose ?? undefined;
60
      const who = c.kind === "project" ? `\nAgents who may post: ${[...own, ...guests].join(", ") || "none yet"}` : "";
61
      i.tooltip = `${c.display}${c.purpose ? `\n${c.purpose}` : ""}\n${c.messages} message(s)${who}`;
62
      i.contextValue = c.kind === "project" ? "projectChannel" : "channel";
63
      i.iconPath = new vscode.ThemeIcon(c.kind === "direct" ? "mention" : c.name === mine ? "star-full" : "comment-discussion");
64
      i.command = { command: "spectra.openChannel", title: "Open", arguments: [c.name] };
65
      i.id = `channel:${c.name}`;
66
      return i;
67
    });
68
  }
69
}
70
71
export class Inbox extends View {
72
  protected items(): vscode.TreeItem[] {
73
    if (!this.model.inbox.length) {
74
      const i = new vscode.TreeItem("Nothing unread");
75
      i.iconPath = new vscode.ThemeIcon("check");
76
      return [i];
77
    }
78
    return this.model.inbox.map((m) => {
79
      const i = new vscode.TreeItem(m.summary.replace(/\s+/g, " ").slice(0, 120));
80
      i.description = `${m.from_display || m.from} · ${m.channel} · ${when(m.at_ms)}`;
81
      i.iconPath = new vscode.ThemeIcon(m.kind === "dm" ? "mail" : "mention");
82
      i.command = { command: "spectra.openChannel", title: "Open", arguments: [m.channel.replace(/^#/, "")] };
83
      return i;
84
    });
85
  }
86
}
87
88
export class Briefs extends View {
89
  protected items(): vscode.TreeItem[] {
90
    const overall = new vscode.TreeItem("Overall brief");
91
    overall.description = this.model.briefs.overall ? firstLine(this.model.briefs.overall) : "not set";
92
    overall.iconPath = new vscode.ThemeIcon("globe");
93
    overall.command = { command: "spectra.editOverallBrief", title: "Edit" };
94
    const items = [overall];
95
    for (const p of this.model.projects) {
96
      const b = this.model.briefs.projects.find((x) => x.project === p.name);
97
      const i = new vscode.TreeItem(p.name);
98
      i.description = b ? firstLine(b.text) : "not set";
99
      i.iconPath = new vscode.ThemeIcon("note");
100
      i.command = { command: "spectra.editProjectBrief", title: "Edit", arguments: [p.name] };
101
      items.push(i);
102
    }
103
    return items;
104
  }
105
}
106
107
function firstLine(s: string): string {
108
  return s.trim().split("\n")[0].slice(0, 80);
109
}