@giladbarnea/pi-simple-team 2.0.2 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/team-ui.ts CHANGED
@@ -2,12 +2,13 @@ import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
2
2
  import { getKeybindings, SelectList, sliceByColumn, truncateToWidth, visibleWidth, type Component, type SelectItem, type TUI } from "@earendil-works/pi-tui";
3
3
 
4
4
  import {
5
- actorHueToken,
5
+ memberRows,
6
6
  relativeTimeText,
7
- statusWordToken,
7
+ clipTeamLines,
8
8
  teamLineText,
9
9
  teamLogLines,
10
10
  teamMessageLines,
11
+ type TeammateView,
11
12
  type TeamMessageDetails,
12
13
  type TeamStatusView,
13
14
  type ThemeLike,
@@ -21,12 +22,19 @@ export interface TeamSnapshot {
21
22
  transports: readonly ("rpc" | "herdr")[];
22
23
  roster: string[];
23
24
  statuses: Record<string, TeamStatusView>;
25
+ teammates: Record<string, TeammateView>;
24
26
  log: TeamLogEntry[];
25
27
  }
26
28
 
27
29
  export type TeamSnapshotSource = () => readonly TeamSnapshot[];
28
30
 
29
31
  const RECENT_STATUS_LIMIT = 5;
32
+ /** A fact row, the status phrase row, and an empty row before the next member. */
33
+ const STATUS_ROWS_PER_MEMBER = 3;
34
+ /** Messages keeps its sender row, one body row, and the omission marker inside its borders. */
35
+ const MESSAGES_MINIMUM_HEIGHT = 5;
36
+ /** Team Log keeps one row inside its borders. */
37
+ const LOG_MINIMUM_HEIGHT = 3;
30
38
 
31
39
  type ZoomableWidget = "messages" | "log";
32
40
 
@@ -82,28 +90,6 @@ function recentStatuses(statuses: Record<string, TeamStatusView>, limit: number)
82
90
  .slice(0, limit);
83
91
  }
84
92
 
85
- function padVisible(text: string, width: number): string {
86
- return `${text}${" ".repeat(Math.max(0, width - visibleWidth(text)))}`;
87
- }
88
-
89
- function statusRows(theme: ThemeLike, statuses: Array<[string, TeamStatusView]>, roster: string[], width: number): string[] {
90
- const contentWidth = Math.max(0, width - 1);
91
- const nameWidth = Math.max(0, ...statuses.map(([name]) => visibleWidth(name)));
92
- const wordWidth = Math.max(0, ...statuses.map(([, status]) => visibleWidth(status.word)));
93
- const updatedTexts = statuses.map(([, status]) => relativeTimeText(status.updated));
94
- const timestampWidth = Math.max(0, ...updatedTexts.map((text) => visibleWidth(text)));
95
- const phraseWidth = Math.max(0, contentWidth - nameWidth - wordWidth - timestampWidth - 6);
96
-
97
- return statuses.map(([name, status], index) => {
98
- const styledName = theme.fg(actorHueToken(name, roster), name);
99
- const styledWord = theme.fg(statusWordToken(status.word), status.word);
100
- const updatedText = updatedTexts[index]!;
101
- const timestamp = theme.fg("dim", `${" ".repeat(Math.max(0, timestampWidth - visibleWidth(updatedText)))}${updatedText}`);
102
- const phrase = middleTruncateToWidth(status.phrase, phraseWidth, true);
103
- return `${padVisible(styledName, nameWidth)} ${padVisible(styledWord, wordWidth)} ${phrase} ${timestamp}`;
104
- });
105
- }
106
-
107
93
  function limitMessageRows(lines: string[], height: number): string[] {
108
94
  if (lines.length <= height) return lines;
109
95
  if (height <= 0) return [];
@@ -324,15 +310,15 @@ class TeamOverviewOverlay implements Component {
324
310
  const contentWidth = Math.max(1, width - 2);
325
311
  const contentHeight = Math.max(1, height - 2);
326
312
  const headerHeight = Math.min(3, Math.max(1, contentHeight - 3));
327
- const desiredStatusHeight = Math.min(Object.keys(team.statuses).length, RECENT_STATUS_LIMIT) + 2;
328
- const statusHeight = Math.max(1, Math.min(desiredStatusHeight, contentHeight - headerHeight - 2));
313
+ const statusBudget = contentHeight - headerHeight - MESSAGES_MINIMUM_HEIGHT - LOG_MINIMUM_HEIGHT;
314
+ const statusCount = Math.max(0, Math.min(Object.keys(team.statuses).length, RECENT_STATUS_LIMIT, Math.floor((statusBudget - 1) / STATUS_ROWS_PER_MEMBER)));
315
+ const statusHeight = Math.max(1, statusCount * STATUS_ROWS_PER_MEMBER + 1);
329
316
  const feedHeight = contentHeight - headerHeight - statusHeight;
330
- const messageHeight = Math.max(1, Math.min(feedHeight - 1, Math.round(feedHeight * 0.55)));
317
+ const messageHeight = Math.max(1, Math.min(feedHeight - LOG_MINIMUM_HEIGHT, Math.max(MESSAGES_MINIMUM_HEIGHT, Math.round(feedHeight * 0.55))));
331
318
  const logHeight = feedHeight - messageHeight;
332
319
 
333
- const statusContentHeight = Math.max(0, statusHeight - 2);
334
- const statuses = recentStatuses(team.statuses, Math.min(RECENT_STATUS_LIMIT, statusContentHeight));
335
- const statusLines = statusRows(this.theme, statuses, team.roster, Math.max(0, contentWidth - 2));
320
+ const statuses = recentStatuses(team.statuses, statusCount);
321
+ const statusLines = clipTeamLines(memberRows(this.theme, Object.fromEntries(statuses), team.teammates, team.roster), Math.max(1, contentWidth - 3));
336
322
 
337
323
  const messages = this.messageWidget(team, messageHeight - 2);
338
324
  const log = this.logWidget(team, logHeight - 2);
package/teamlog.ts CHANGED
@@ -6,6 +6,7 @@ export type TeamLogKind =
6
6
  | "status"
7
7
  | "agent_start"
8
8
  | "agent_end"
9
+ | "agent_settled"
9
10
  | "tool_start"
10
11
  | "tool_end"
11
12
  | "main_message"
@@ -260,10 +261,14 @@ export function normalizeChildEvent(team: string, teammate: string, event: Recor
260
261
  if (type === "agent_end") {
261
262
  const messages = event.messages;
262
263
  const messageCount = Array.isArray(messages) ? messages.length : undefined;
263
- const summary = messageCount === undefined ? `${teammate} finished` : `${teammate} finished (${messageCount} message${messageCount === 1 ? "" : "s"})`;
264
+ const summary = messageCount === undefined ? `${teammate} attempt ended` : `${teammate} attempt ended (${messageCount} message${messageCount === 1 ? "" : "s"})`;
264
265
  return { team, teammate, direction: "runtime", kind: "agent_end", summary, details: messageCount === undefined ? undefined : { messageCount } };
265
266
  }
266
267
 
268
+ if (type === "agent_settled") {
269
+ return { team, teammate, direction: "runtime", kind: "agent_settled", summary: `${teammate} run ${event.aborted ? "cancelled" : "ended"}`, details: { aborted: event.aborted } };
270
+ }
271
+
267
272
  if (type === "tool_execution_start") {
268
273
  const toolName = String(event.toolName ?? "tool");
269
274
  if (TEAM_CHILD_TOOLS.has(toolName)) return undefined;
@@ -0,0 +1,194 @@
1
+ import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
2
+
3
+ import { padVisible } from "./render-support/ansi.ts";
4
+ import { glyphs, glyphStyle } from "./render-support/glyphs.ts";
5
+ import { contextGauge, contextHeat, type HeatTheme } from "./render-support/heat.ts";
6
+
7
+ export interface FactTheme extends HeatTheme {
8
+ bold(text: string): string;
9
+ }
10
+
11
+ /** The facts a teammate row can show. A missing field means the view does not show that fact. A stopped teammate (`live: false`) has a dimmed name. */
12
+ export interface TeammateView {
13
+ name: string;
14
+ live?: boolean;
15
+ model?: string;
16
+ thinking?: string;
17
+ forkContext?: boolean;
18
+ canManageOwnTeams?: boolean;
19
+ showOnHerdrPane?: boolean;
20
+ contextPercent?: number;
21
+ }
22
+
23
+ /** A teammate row: its view facts plus the status word, when the view shows one. */
24
+ export interface FactRow extends TeammateView {
25
+ status?: string;
26
+ }
27
+
28
+ export type FactKey = "name" | "status" | "model" | "context" | "fork" | "manager" | "thinking" | "herdr";
29
+ type Form = "full" | "short" | "bare" | "icon";
30
+ type FormOrHidden = Form | "hidden";
31
+
32
+ interface Paint {
33
+ theme: FactTheme;
34
+ roster: string[];
35
+ nameWidth: number;
36
+ }
37
+
38
+ /** Each form renders one cell. A cell is empty when the row lacks the fact. */
39
+ type Ladder = { full: (row: FactRow, paint: Paint) => string } & Partial<Record<Exclude<Form, "full">, (row: FactRow, paint: Paint) => string>>;
40
+
41
+ /** Left to right on every teammate row, across every view. */
42
+ export const FACT_ORDER: readonly FactKey[] = ["name", "status", "model", "context", "fork", "manager", "thinking", "herdr"];
43
+
44
+ const TEAMMATE_HUE_TOKENS = ["mdCode", "customMessageLabel", "mdHeading"] as const;
45
+
46
+ /**
47
+ * actorHueToken("main", ["scout"]) === "accent"; actorHueToken("scout", ["scout"]) === "mdCode"
48
+ */
49
+ export function actorHueToken(name: string, roster: string[]): string {
50
+ if (name === "main") return "accent";
51
+ const index = roster.indexOf(name);
52
+ return index === -1 ? "text" : TEAMMATE_HUE_TOKENS[index % TEAMMATE_HUE_TOKENS.length]!;
53
+ }
54
+
55
+ const STATUS_WORD_TOKENS: Record<string, string> = {
56
+ active: "success",
57
+ busy: "success",
58
+ running: "success",
59
+ working: "success",
60
+ blocked: "warning",
61
+ restarted: "warning",
62
+ resumed: "success",
63
+ waiting: "warning",
64
+ available: "muted",
65
+ dormant: "muted",
66
+ idle: "muted",
67
+ spawned: "muted",
68
+ done: "dim",
69
+ exited: "dim",
70
+ stopped: "dim",
71
+ error: "error",
72
+ failed: "error",
73
+ };
74
+
75
+ /**
76
+ * statusWordToken("working") === "success"; statusWordToken("reviewing") === "accent"
77
+ */
78
+ export function statusWordToken(word: string): string {
79
+ return STATUS_WORD_TOKENS[word.trim().toLowerCase()] ?? "accent";
80
+ }
81
+
82
+ export const DIM_SGR_OPEN = "\x1b[2m";
83
+ export const DIM_SGR_CLOSE = "\x1b[22m";
84
+
85
+ /** @example modelTail("openai-codex/gpt-6-astra") // "gpt-6-astra" */
86
+ function modelTail(model: string): string {
87
+ return model.slice(model.lastIndexOf("/") + 1);
88
+ }
89
+
90
+ /** A boolean fact's ladder: every form is empty unless the row sets the flag. */
91
+ function flag(field: "forkContext" | "canManageOwnTeams" | "showOnHerdrPane", forms: Record<Form, string>): Ladder {
92
+ const form = (text: string) => (row: FactRow, paint: Paint) => (row[field] ? paint.theme.fg("muted", text) : "");
93
+ return { full: form(forms.full), short: form(forms.short), bare: form(forms.bare), icon: form(forms.icon) };
94
+ }
95
+
96
+ /** The context gauge and percent share one heat color; the label stays quiet. */
97
+ function context(label: string, withGauge = true): (row: FactRow, paint: Paint) => string {
98
+ return (row, paint) => {
99
+ if (row.contextPercent === undefined) return "";
100
+ const reading = `${withGauge ? contextGauge(row.contextPercent) : ""}${Math.round(row.contextPercent)}%`;
101
+ return `${contextHeat(paint.theme, row.contextPercent, reading)}${label ? paint.theme.fg("muted", ` ${label}`) : ""}`;
102
+ };
103
+ }
104
+
105
+ const LADDERS: Record<FactKey, Ladder> = {
106
+ name: {
107
+ full: (row, paint) => {
108
+ const name = paint.theme.fg(actorHueToken(row.name, paint.roster), padVisible(row.name, paint.nameWidth));
109
+ return row.live === false ? `${DIM_SGR_OPEN}${name}${DIM_SGR_CLOSE}` : name;
110
+ },
111
+ },
112
+ status: { full: (row, paint) => (row.status ? paint.theme.fg(statusWordToken(row.status), row.status) : "") },
113
+ model: {
114
+ full: (row, paint) => (row.model ? paint.theme.fg("muted", row.model) : ""),
115
+ short: (row, paint) => (row.model ? paint.theme.fg("muted", modelTail(row.model)) : ""),
116
+ },
117
+ context: { full: context("context"), short: context("ctx"), bare: context("ctx", false), icon: context("") },
118
+ fork: flag("forkContext", { full: "⑂ fork", short: "⑂ fork", bare: "fork", icon: "⑂" }),
119
+ manager: flag("canManageOwnTeams", { full: "〒 team manager", short: "〒 manager", bare: "manager", icon: "〒" }),
120
+ thinking: { full: (row, paint) => (row.thinking ? paint.theme.fg("muted", row.thinking) : "") },
121
+ herdr: flag("showOnHerdrPane", { full: "⧉ herdr pane", short: "⧉ herdr", bare: "herdr", icon: "⧉" }),
122
+ };
123
+
124
+ /**
125
+ * The shrink state machine. State N applies the first N steps, so each later state is narrower.
126
+ * First shorten labels, then drop icons from the labels, then keep icons alone, then hide whole facts.
127
+ * Within each phase, the lowest-priority fact gives up width first.
128
+ */
129
+ const SHRINK_STEPS: ReadonlyArray<readonly [FactKey, FormOrHidden]> = [
130
+ ["herdr", "short"], ["manager", "short"], ["context", "short"], ["model", "short"],
131
+ ["herdr", "bare"], ["manager", "bare"], ["fork", "bare"],
132
+ ["herdr", "icon"], ["manager", "icon"], ["fork", "icon"], ["context", "icon"],
133
+ ["herdr", "hidden"], ["thinking", "hidden"], ["manager", "hidden"], ["fork", "hidden"], ["context", "hidden"], ["model", "hidden"], ["status", "hidden"],
134
+ ];
135
+
136
+ const FULL_FORMS = Object.fromEntries(FACT_ORDER.map((key) => [key, "full"])) as Record<FactKey, FormOrHidden>;
137
+
138
+ /** formsAt(1).herdr === "short" */
139
+ function formsAt(state: number): Record<FactKey, FormOrHidden> {
140
+ return Object.fromEntries([...Object.entries(FULL_FORMS), ...SHRINK_STEPS.slice(0, state)]) as Record<FactKey, FormOrHidden>;
141
+ }
142
+
143
+ const COLUMN_GAP = " ";
144
+
145
+ /** ASCII terminals have no fact icons, so any fact with a text-only form always uses it. */
146
+ function cell(key: FactKey, form: FormOrHidden, row: FactRow, paint: Paint): string {
147
+ if (form === "hidden") return "";
148
+ const ladder = LADDERS[key];
149
+ const shown = glyphStyle() === "ascii" && ladder.bare ? "bare" : form;
150
+ return ladder[shown]!(row, paint);
151
+ }
152
+
153
+ /** One fact in its icon form, for views that list teammates inline. Empty when the row lacks the fact. */
154
+ export function inlineFact(theme: FactTheme, key: FactKey, row: FactRow): string {
155
+ return cell(key, "icon", row, { theme, roster: [], nameWidth: 0 });
156
+ }
157
+
158
+ /** The cells of every row at one state, padded into aligned columns. Empty columns drop out. */
159
+ function layoutRows(rows: FactRow[], paint: Paint, forms: Record<FactKey, FormOrHidden>): string[] {
160
+ const columns = FACT_ORDER.map((key) => rows.map((row) => cell(key, forms[key], row, paint))).filter((cells) => cells.some((text) => text.length > 0));
161
+ const widths = columns.map((cells) => Math.max(...cells.map((text) => visibleWidth(text))));
162
+ return rows.map((_row, rowIndex) => columns.map((cells, columnIndex) => padVisible(cells[rowIndex]!, widths[columnIndex]!)).join(COLUMN_GAP).trimEnd());
163
+ }
164
+
165
+ /** One aligned set of teammate rows that shrinks as a whole to fit a width. */
166
+ export class FactTable {
167
+ private readonly paint: Paint;
168
+ private readonly fittedByWidth = new Map<number, string[]>();
169
+
170
+ constructor(theme: FactTheme, private readonly rows: FactRow[], roster: string[]) {
171
+ this.paint = { theme, roster, nameWidth: Math.max(...rows.map((row) => visibleWidth(row.name))) };
172
+ }
173
+
174
+ /** The row with every fact in its full form. */
175
+ full(row: FactRow): string {
176
+ return this.fitted(Number.POSITIVE_INFINITY)[this.rows.indexOf(row)]!;
177
+ }
178
+
179
+ /** The row at the widest state that fits `width`, clipped only when even the narrowest state does not. */
180
+ fit(row: FactRow, width: number): string {
181
+ return truncateToWidth(this.fitted(width)[this.rows.indexOf(row)]!, Math.max(1, width), glyphs().ellipsis);
182
+ }
183
+
184
+ private fitted(width: number): string[] {
185
+ const cached = this.fittedByWidth.get(width);
186
+ if (cached) return cached;
187
+ let lines = layoutRows(this.rows, this.paint, formsAt(0));
188
+ for (let state = 1; state <= SHRINK_STEPS.length && Math.max(...lines.map((line) => visibleWidth(line))) > width; state++) {
189
+ lines = layoutRows(this.rows, this.paint, formsAt(state));
190
+ }
191
+ this.fittedByWidth.set(width, lines);
192
+ return lines;
193
+ }
194
+ }
package/teammate.ts CHANGED
@@ -1,11 +1,12 @@
1
- export type ThinkingLevel = "low" | "medium" | "high" | "xhigh" | "max";
1
+ /** Main requests one of `low` to `max`. Pi can run a teammate at `off` or `minimal` when the model supports less. */
2
+ export type ThinkingLevel = "off" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max";
2
3
 
3
4
  export interface Teammate {
4
5
  name: string;
5
6
  systemPrompt: string;
6
7
  model: string;
7
8
  thinking?: ThinkingLevel;
8
- inheritMainContext?: boolean;
9
+ forkContext?: boolean;
9
10
  canManageOwnTeams?: boolean;
10
11
  showOnHerdrPane?: boolean;
11
12
  extensionPaths?: string[];
@@ -17,4 +18,5 @@ export type TeammateRecord = Required<Omit<Teammate, "extensionPaths">> & {
17
18
  sessionFile: string;
18
19
  live: boolean;
19
20
  active: boolean;
21
+ contextPercent?: number;
20
22
  };