@narumitw/pi-herdr 0.0.1
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/LICENSE +21 -0
- package/README.md +140 -0
- package/package.json +54 -0
- package/skills/herdr/SKILL.md +195 -0
- package/src/herdr-agent-state.ts +266 -0
- package/src/herdr-client.ts +287 -0
- package/src/herdr-observer.ts +415 -0
- package/src/herdr-protocol.ts +237 -0
- package/src/herdr-widget.ts +281 -0
- package/src/index.ts +1 -0
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
export const HERDR_AGENT_STATUSES = ["idle", "working", "blocked", "done", "unknown"] as const;
|
|
2
|
+
|
|
3
|
+
export type HerdrAgentStatus = (typeof HERDR_AGENT_STATUSES)[number];
|
|
4
|
+
|
|
5
|
+
export interface HerdrPane {
|
|
6
|
+
paneId: string;
|
|
7
|
+
workspaceId: string;
|
|
8
|
+
tabId: string;
|
|
9
|
+
terminalId: string;
|
|
10
|
+
focused: boolean;
|
|
11
|
+
revision: number;
|
|
12
|
+
agent?: string;
|
|
13
|
+
agentName?: string;
|
|
14
|
+
agentStatus: HerdrAgentStatus;
|
|
15
|
+
displayAgent?: string;
|
|
16
|
+
label?: string;
|
|
17
|
+
stateLabels: Record<string, string>;
|
|
18
|
+
terminalTitle?: string;
|
|
19
|
+
title?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface HerdrWorkspace {
|
|
23
|
+
workspaceId: string;
|
|
24
|
+
label?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type HerdrPaneEvent =
|
|
28
|
+
| { type: "created"; pane: HerdrPane }
|
|
29
|
+
| { type: "closed" | "exited"; paneId: string; workspaceId: string }
|
|
30
|
+
| {
|
|
31
|
+
type: "moved";
|
|
32
|
+
pane: HerdrPane;
|
|
33
|
+
previousPaneId: string;
|
|
34
|
+
previousWorkspaceId: string;
|
|
35
|
+
}
|
|
36
|
+
| {
|
|
37
|
+
type: "agent_detected";
|
|
38
|
+
paneId: string;
|
|
39
|
+
workspaceId: string;
|
|
40
|
+
agent?: string;
|
|
41
|
+
finalStatus?: HerdrAgentStatus;
|
|
42
|
+
released: boolean;
|
|
43
|
+
}
|
|
44
|
+
| {
|
|
45
|
+
type: "agent_status_changed";
|
|
46
|
+
paneId: string;
|
|
47
|
+
workspaceId: string;
|
|
48
|
+
agent?: string;
|
|
49
|
+
agentStatus: HerdrAgentStatus;
|
|
50
|
+
displayAgent?: string;
|
|
51
|
+
stateLabels?: Record<string, string>;
|
|
52
|
+
title?: string;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const HERDR_WIDGET_TOPOLOGY_SUBSCRIPTIONS = [
|
|
56
|
+
{ type: "pane.created" },
|
|
57
|
+
{ type: "pane.closed" },
|
|
58
|
+
{ type: "pane.moved" },
|
|
59
|
+
{ type: "pane.exited" },
|
|
60
|
+
{ type: "pane.agent_detected" },
|
|
61
|
+
] as const;
|
|
62
|
+
|
|
63
|
+
export function herdrWidgetSubscriptions(panes: readonly HerdrPane[]) {
|
|
64
|
+
return [
|
|
65
|
+
...HERDR_WIDGET_TOPOLOGY_SUBSCRIPTIONS.map((entry) => ({ ...entry })),
|
|
66
|
+
...panes
|
|
67
|
+
.filter((pane) => pane.agent !== undefined)
|
|
68
|
+
.map((pane) => ({ type: "pane.agent_status_changed", pane_id: pane.paneId })),
|
|
69
|
+
];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
73
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function requiredString(value: unknown): string | undefined {
|
|
77
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function optionalString(value: unknown): string | undefined {
|
|
81
|
+
return typeof value === "string" ? value : undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function agentStatus(value: unknown): HerdrAgentStatus | undefined {
|
|
85
|
+
return typeof value === "string" && HERDR_AGENT_STATUSES.includes(value as HerdrAgentStatus)
|
|
86
|
+
? (value as HerdrAgentStatus)
|
|
87
|
+
: undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function stringRecord(value: unknown): Record<string, string> | undefined {
|
|
91
|
+
if (!isRecord(value)) return undefined;
|
|
92
|
+
const entries = Object.entries(value);
|
|
93
|
+
if (!entries.every(([, entry]) => typeof entry === "string")) return undefined;
|
|
94
|
+
return Object.fromEntries(entries) as Record<string, string>;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function parseHerdrPane(value: unknown): HerdrPane | undefined {
|
|
98
|
+
if (!isRecord(value)) return undefined;
|
|
99
|
+
const paneId = requiredString(value.pane_id);
|
|
100
|
+
const workspaceId = requiredString(value.workspace_id);
|
|
101
|
+
const tabId = requiredString(value.tab_id);
|
|
102
|
+
const terminalId = requiredString(value.terminal_id);
|
|
103
|
+
const status = agentStatus(value.agent_status);
|
|
104
|
+
const revision = value.revision;
|
|
105
|
+
if (
|
|
106
|
+
!paneId ||
|
|
107
|
+
!workspaceId ||
|
|
108
|
+
!tabId ||
|
|
109
|
+
!terminalId ||
|
|
110
|
+
!status ||
|
|
111
|
+
typeof value.focused !== "boolean" ||
|
|
112
|
+
typeof revision !== "number" ||
|
|
113
|
+
!Number.isSafeInteger(revision) ||
|
|
114
|
+
revision < 0
|
|
115
|
+
) {
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
paneId,
|
|
120
|
+
workspaceId,
|
|
121
|
+
tabId,
|
|
122
|
+
terminalId,
|
|
123
|
+
focused: value.focused,
|
|
124
|
+
revision,
|
|
125
|
+
agent: optionalString(value.agent),
|
|
126
|
+
agentName: optionalString(value.name),
|
|
127
|
+
agentStatus: status,
|
|
128
|
+
displayAgent: optionalString(value.display_agent),
|
|
129
|
+
label: optionalString(value.label),
|
|
130
|
+
stateLabels: stringRecord(value.state_labels) ?? {},
|
|
131
|
+
terminalTitle:
|
|
132
|
+
optionalString(value.terminal_title_stripped) ?? optionalString(value.terminal_title),
|
|
133
|
+
title: optionalString(value.title),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function parsePaneCurrentResult(value: unknown): HerdrPane {
|
|
138
|
+
if (!isRecord(value) || value.type !== "pane_current") {
|
|
139
|
+
throw new Error("Herdr returned an invalid current-pane response");
|
|
140
|
+
}
|
|
141
|
+
const pane = parseHerdrPane(value.pane);
|
|
142
|
+
if (!pane) throw new Error("Herdr returned an invalid current pane");
|
|
143
|
+
return pane;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function parsePaneListResult(value: unknown): HerdrPane[] {
|
|
147
|
+
if (!isRecord(value) || value.type !== "pane_list" || !Array.isArray(value.panes)) {
|
|
148
|
+
throw new Error("Herdr returned an invalid pane-list response");
|
|
149
|
+
}
|
|
150
|
+
const panes = value.panes.map(parseHerdrPane);
|
|
151
|
+
if (panes.some((pane) => pane === undefined)) {
|
|
152
|
+
throw new Error("Herdr returned an invalid pane in the pane list");
|
|
153
|
+
}
|
|
154
|
+
return panes as HerdrPane[];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function parseAgentListResult(value: unknown): HerdrPane[] {
|
|
158
|
+
if (!isRecord(value) || value.type !== "agent_list" || !Array.isArray(value.agents)) {
|
|
159
|
+
throw new Error("Herdr returned an invalid agent-list response");
|
|
160
|
+
}
|
|
161
|
+
return value.agents.flatMap((entry) => {
|
|
162
|
+
const pane = parseHerdrPane(entry);
|
|
163
|
+
return pane ? [pane] : [];
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function parseWorkspaceGetResult(value: unknown): HerdrWorkspace {
|
|
168
|
+
if (!isRecord(value) || value.type !== "workspace_info" || !isRecord(value.workspace)) {
|
|
169
|
+
throw new Error("Herdr returned an invalid workspace response");
|
|
170
|
+
}
|
|
171
|
+
const workspaceId = requiredString(value.workspace.workspace_id);
|
|
172
|
+
if (!workspaceId) throw new Error("Herdr returned an invalid workspace identity");
|
|
173
|
+
return { workspaceId, label: optionalString(value.workspace.label) };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function parseHerdrPaneEvent(value: unknown): HerdrPaneEvent | undefined {
|
|
177
|
+
if (!isRecord(value) || typeof value.event !== "string" || !isRecord(value.data)) {
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
const data = value.data;
|
|
181
|
+
|
|
182
|
+
if (value.event === "pane.agent_status_changed") {
|
|
183
|
+
const paneId = requiredString(data.pane_id);
|
|
184
|
+
const workspaceId = requiredString(data.workspace_id);
|
|
185
|
+
const status = agentStatus(data.agent_status);
|
|
186
|
+
if (!paneId || !workspaceId || !status) return undefined;
|
|
187
|
+
return {
|
|
188
|
+
type: "agent_status_changed",
|
|
189
|
+
paneId,
|
|
190
|
+
workspaceId,
|
|
191
|
+
agent: optionalString(data.agent),
|
|
192
|
+
agentStatus: status,
|
|
193
|
+
displayAgent: optionalString(data.display_agent),
|
|
194
|
+
stateLabels: stringRecord(data.state_labels),
|
|
195
|
+
title: optionalString(data.title),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (value.event === "pane_created") {
|
|
200
|
+
const pane = parseHerdrPane(data.pane);
|
|
201
|
+
return pane ? { type: "created", pane } : undefined;
|
|
202
|
+
}
|
|
203
|
+
if (value.event === "pane_closed" || value.event === "pane_exited") {
|
|
204
|
+
const paneId = requiredString(data.pane_id);
|
|
205
|
+
const workspaceId = requiredString(data.workspace_id);
|
|
206
|
+
if (!paneId || !workspaceId) return undefined;
|
|
207
|
+
return {
|
|
208
|
+
type: value.event === "pane_closed" ? "closed" : "exited",
|
|
209
|
+
paneId,
|
|
210
|
+
workspaceId,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
if (value.event === "pane_moved") {
|
|
214
|
+
const pane = parseHerdrPane(data.pane);
|
|
215
|
+
const previousPaneId = requiredString(data.previous_pane_id);
|
|
216
|
+
const previousWorkspaceId = requiredString(data.previous_workspace_id);
|
|
217
|
+
if (!pane || !previousPaneId || !previousWorkspaceId) return undefined;
|
|
218
|
+
return { type: "moved", pane, previousPaneId, previousWorkspaceId };
|
|
219
|
+
}
|
|
220
|
+
if (value.event === "pane_agent_detected") {
|
|
221
|
+
const paneId = requiredString(data.pane_id);
|
|
222
|
+
const workspaceId = requiredString(data.workspace_id);
|
|
223
|
+
if (!paneId || !workspaceId || typeof data.released !== "boolean") return undefined;
|
|
224
|
+
const finalStatus = data.final_status === null ? undefined : agentStatus(data.final_status);
|
|
225
|
+
if (data.final_status !== undefined && data.final_status !== null && !finalStatus)
|
|
226
|
+
return undefined;
|
|
227
|
+
return {
|
|
228
|
+
type: "agent_detected",
|
|
229
|
+
paneId,
|
|
230
|
+
workspaceId,
|
|
231
|
+
agent: optionalString(data.agent),
|
|
232
|
+
finalStatus,
|
|
233
|
+
released: data.released,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
return undefined;
|
|
237
|
+
}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { EditorStatusWidget } from "@narumitw/pi-tui-kit/editor-status-widget";
|
|
3
|
+
import { sanitizeTerminalText } from "@narumitw/pi-tui-kit/terminal-text";
|
|
4
|
+
import type {
|
|
5
|
+
HerdrAgentStatus,
|
|
6
|
+
HerdrPane,
|
|
7
|
+
HerdrPaneEvent,
|
|
8
|
+
HerdrWorkspace,
|
|
9
|
+
} from "./herdr-protocol.js";
|
|
10
|
+
|
|
11
|
+
export const HERDR_WIDGET_KEY = "herdr:agents";
|
|
12
|
+
export const MAX_HERDR_WIDGET_AGENTS = 5;
|
|
13
|
+
|
|
14
|
+
interface ObservedPane {
|
|
15
|
+
paneId: string;
|
|
16
|
+
workspaceId: string;
|
|
17
|
+
agent?: string;
|
|
18
|
+
agentName?: string;
|
|
19
|
+
agentStatus: HerdrAgentStatus;
|
|
20
|
+
displayAgent?: string;
|
|
21
|
+
label?: string;
|
|
22
|
+
stateLabels: Record<string, string>;
|
|
23
|
+
title?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface HerdrWidgetRow {
|
|
27
|
+
agent: string;
|
|
28
|
+
pane: string;
|
|
29
|
+
paneId: string;
|
|
30
|
+
space: string;
|
|
31
|
+
state: HerdrAgentStatus;
|
|
32
|
+
stateLabel: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface HerdrWidgetSnapshot {
|
|
36
|
+
totalAgents: number;
|
|
37
|
+
hiddenAgents: number;
|
|
38
|
+
rows: readonly HerdrWidgetRow[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const STATE_ORDER: Record<HerdrAgentStatus, number> = {
|
|
42
|
+
blocked: 0,
|
|
43
|
+
done: 1,
|
|
44
|
+
working: 2,
|
|
45
|
+
idle: 3,
|
|
46
|
+
unknown: 4,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
function observedPane(pane: HerdrPane): ObservedPane {
|
|
50
|
+
return {
|
|
51
|
+
paneId: pane.paneId,
|
|
52
|
+
workspaceId: pane.workspaceId,
|
|
53
|
+
agent: pane.agent,
|
|
54
|
+
agentName: pane.agentName,
|
|
55
|
+
agentStatus: pane.agentStatus,
|
|
56
|
+
displayAgent: pane.displayAgent,
|
|
57
|
+
label: pane.label,
|
|
58
|
+
stateLabels: { ...pane.stateLabels },
|
|
59
|
+
title: pane.title,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function safeDisplay(value: string | undefined): string | undefined {
|
|
64
|
+
if (value === undefined) return undefined;
|
|
65
|
+
const safe = sanitizeTerminalText(value).trim();
|
|
66
|
+
return safe.length > 0 ? safe : undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function shortIdentity(value: string, fallback: string): string {
|
|
70
|
+
const safe = safeDisplay(value);
|
|
71
|
+
if (!safe) return fallback;
|
|
72
|
+
return safe.split(":").at(-1) || fallback;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function agentDisplay(pane: ObservedPane): string {
|
|
76
|
+
return (
|
|
77
|
+
safeDisplay(pane.agentName) ??
|
|
78
|
+
safeDisplay(pane.displayAgent) ??
|
|
79
|
+
safeDisplay(pane.agent) ??
|
|
80
|
+
"agent"
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function paneDisplay(pane: ObservedPane): string {
|
|
85
|
+
const id = shortIdentity(pane.paneId, "pane");
|
|
86
|
+
const label = safeDisplay(pane.label) ?? safeDisplay(pane.title);
|
|
87
|
+
return label ? `${label}/${id}` : id;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function spaceDisplay(workspace: HerdrWorkspace): string {
|
|
91
|
+
const id = shortIdentity(workspace.workspaceId, "space");
|
|
92
|
+
const label = safeDisplay(workspace.label);
|
|
93
|
+
return label ? `${label}/${id}` : id;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function rowForPane(pane: ObservedPane, workspace: HerdrWorkspace): HerdrWidgetRow {
|
|
97
|
+
return {
|
|
98
|
+
agent: agentDisplay(pane),
|
|
99
|
+
pane: paneDisplay(pane),
|
|
100
|
+
paneId: safeDisplay(pane.paneId) ?? "pane",
|
|
101
|
+
space: spaceDisplay(workspace),
|
|
102
|
+
state: pane.agentStatus,
|
|
103
|
+
stateLabel:
|
|
104
|
+
safeDisplay(pane.stateLabels[pane.agentStatus]) ?? safeDisplay(pane.agentStatus) ?? "unknown",
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function immutableSnapshot(rows: HerdrWidgetRow[]): HerdrWidgetSnapshot | undefined {
|
|
109
|
+
if (rows.length === 0) return undefined;
|
|
110
|
+
const visible = rows.slice(0, MAX_HERDR_WIDGET_AGENTS).map((row) => Object.freeze({ ...row }));
|
|
111
|
+
return Object.freeze({
|
|
112
|
+
totalAgents: rows.length,
|
|
113
|
+
hiddenAgents: Math.max(0, rows.length - visible.length),
|
|
114
|
+
rows: Object.freeze(visible),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export class HerdrWidgetModel {
|
|
119
|
+
private currentPaneId: string | undefined;
|
|
120
|
+
private currentWorkspaceId: string | undefined;
|
|
121
|
+
private workspace: HerdrWorkspace | undefined;
|
|
122
|
+
private panes = new Map<string, ObservedPane>();
|
|
123
|
+
|
|
124
|
+
reset(
|
|
125
|
+
currentPane: HerdrPane,
|
|
126
|
+
panes: readonly HerdrPane[],
|
|
127
|
+
workspace: HerdrWorkspace = { workspaceId: currentPane.workspaceId },
|
|
128
|
+
): void {
|
|
129
|
+
this.currentPaneId = currentPane.paneId;
|
|
130
|
+
this.currentWorkspaceId = currentPane.workspaceId;
|
|
131
|
+
this.workspace = { ...workspace };
|
|
132
|
+
this.panes = new Map(panes.map((pane) => [pane.paneId, observedPane(pane)]));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
clear(): void {
|
|
136
|
+
this.currentPaneId = undefined;
|
|
137
|
+
this.currentWorkspaceId = undefined;
|
|
138
|
+
this.workspace = undefined;
|
|
139
|
+
this.panes.clear();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
isCurrentPane(paneId: string): boolean {
|
|
143
|
+
return this.currentPaneId === paneId;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
isCurrentWorkspace(workspaceId: string): boolean {
|
|
147
|
+
return this.currentWorkspaceId === workspaceId;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
apply(event: HerdrPaneEvent): void {
|
|
151
|
+
if (event.type === "created") {
|
|
152
|
+
this.panes.set(event.pane.paneId, observedPane(event.pane));
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (event.type === "closed" || event.type === "exited") {
|
|
156
|
+
this.panes.delete(event.paneId);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (event.type === "moved") {
|
|
160
|
+
this.panes.delete(event.previousPaneId);
|
|
161
|
+
this.panes.set(event.pane.paneId, observedPane(event.pane));
|
|
162
|
+
if (this.currentPaneId === event.previousPaneId) {
|
|
163
|
+
this.currentPaneId = event.pane.paneId;
|
|
164
|
+
this.currentWorkspaceId = event.pane.workspaceId;
|
|
165
|
+
this.workspace = { workspaceId: event.pane.workspaceId };
|
|
166
|
+
}
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const existing = this.panes.get(event.paneId);
|
|
171
|
+
if (event.type === "agent_detected") {
|
|
172
|
+
if (event.released || !event.agent) {
|
|
173
|
+
if (existing) this.panes.set(event.paneId, { ...existing, agent: undefined });
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
this.panes.set(event.paneId, {
|
|
177
|
+
...(existing ?? {
|
|
178
|
+
paneId: event.paneId,
|
|
179
|
+
workspaceId: event.workspaceId,
|
|
180
|
+
agentStatus: event.finalStatus ?? "unknown",
|
|
181
|
+
stateLabels: {},
|
|
182
|
+
}),
|
|
183
|
+
agent: event.agent,
|
|
184
|
+
agentStatus: event.finalStatus ?? existing?.agentStatus ?? "unknown",
|
|
185
|
+
});
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
if (event.type !== "agent_status_changed") return;
|
|
189
|
+
|
|
190
|
+
this.panes.set(event.paneId, {
|
|
191
|
+
...(existing ?? {
|
|
192
|
+
paneId: event.paneId,
|
|
193
|
+
workspaceId: event.workspaceId,
|
|
194
|
+
stateLabels: {},
|
|
195
|
+
}),
|
|
196
|
+
agent: event.agent ?? existing?.agent,
|
|
197
|
+
agentStatus: event.agentStatus,
|
|
198
|
+
displayAgent: event.displayAgent ?? existing?.displayAgent,
|
|
199
|
+
stateLabels: event.stateLabels ?? existing?.stateLabels ?? {},
|
|
200
|
+
title: event.title ?? existing?.title,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
snapshot(): HerdrWidgetSnapshot | undefined {
|
|
205
|
+
if (!this.currentPaneId || !this.currentWorkspaceId || !this.workspace) return undefined;
|
|
206
|
+
const rows = [...this.panes.values()]
|
|
207
|
+
.filter(
|
|
208
|
+
(pane) =>
|
|
209
|
+
pane.workspaceId === this.currentWorkspaceId &&
|
|
210
|
+
pane.paneId !== this.currentPaneId &&
|
|
211
|
+
pane.agent !== undefined,
|
|
212
|
+
)
|
|
213
|
+
.map((pane) => rowForPane(pane, this.workspace as HerdrWorkspace));
|
|
214
|
+
rows.sort(
|
|
215
|
+
(left, right) =>
|
|
216
|
+
STATE_ORDER[left.state] - STATE_ORDER[right.state] ||
|
|
217
|
+
left.agent.localeCompare(right.agent) ||
|
|
218
|
+
left.pane.localeCompare(right.pane) ||
|
|
219
|
+
left.paneId.localeCompare(right.paneId),
|
|
220
|
+
);
|
|
221
|
+
return immutableSnapshot(rows);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function stateStyle(theme: Theme, state: HerdrAgentStatus, text: string): string {
|
|
226
|
+
switch (state) {
|
|
227
|
+
case "blocked":
|
|
228
|
+
return theme.fg("warning", text);
|
|
229
|
+
case "done":
|
|
230
|
+
return theme.fg("success", text);
|
|
231
|
+
case "working":
|
|
232
|
+
return theme.fg("accent", text);
|
|
233
|
+
case "idle":
|
|
234
|
+
return theme.fg("muted", text);
|
|
235
|
+
case "unknown":
|
|
236
|
+
return theme.fg("dim", text);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function stateSymbol(state: HerdrAgentStatus): string {
|
|
241
|
+
switch (state) {
|
|
242
|
+
case "blocked":
|
|
243
|
+
return "!";
|
|
244
|
+
case "done":
|
|
245
|
+
return "✓";
|
|
246
|
+
case "working":
|
|
247
|
+
return "●";
|
|
248
|
+
case "idle":
|
|
249
|
+
return "○";
|
|
250
|
+
case "unknown":
|
|
251
|
+
return "?";
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export function createHerdrWidget(snapshot: HerdrWidgetSnapshot, theme: Theme): EditorStatusWidget {
|
|
256
|
+
return new EditorStatusWidget({
|
|
257
|
+
theme,
|
|
258
|
+
renderBody() {
|
|
259
|
+
const noun = snapshot.totalAgents === 1 ? "agent" : "agents";
|
|
260
|
+
const lines = [theme.fg("muted", `Herdr · ${snapshot.totalAgents} sibling ${noun}`)];
|
|
261
|
+
const separator = theme.fg("dim", " · ");
|
|
262
|
+
for (const row of snapshot.rows) {
|
|
263
|
+
const state = stateStyle(theme, row.state, `${stateSymbol(row.state)} ${row.stateLabel}`);
|
|
264
|
+
const agent = theme.bold(theme.fg("text", row.agent));
|
|
265
|
+
const pane = theme.fg("muted", row.pane);
|
|
266
|
+
const space = theme.fg("dim", row.space);
|
|
267
|
+
lines.push(`${state} ${agent}${separator}${pane}${separator}${space}`);
|
|
268
|
+
}
|
|
269
|
+
if (snapshot.hiddenAgents > 0) {
|
|
270
|
+
lines.push(theme.fg("dim", `+${snapshot.hiddenAgents} more`));
|
|
271
|
+
}
|
|
272
|
+
return lines;
|
|
273
|
+
},
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export function herdrWidgetFingerprint(
|
|
278
|
+
snapshot: HerdrWidgetSnapshot | undefined,
|
|
279
|
+
): string | undefined {
|
|
280
|
+
return snapshot ? JSON.stringify(snapshot) : undefined;
|
|
281
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "./herdr-agent-state.js";
|