@narumitw/pi-herdr 0.2.0 → 0.3.0
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/README.md +8 -0
- package/package.json +51 -52
- package/skills/herdr/SKILL.md +12 -0
- package/src/herdr-agent-state.ts +404 -411
- package/src/herdr-client.ts +236 -238
- package/src/herdr-menu.ts +49 -50
- package/src/herdr-metadata.ts +46 -52
- package/src/herdr-observer.ts +334 -372
- package/src/herdr-protocol.ts +185 -187
- package/src/herdr-settings-controller.ts +118 -124
- package/src/herdr-widget.ts +204 -217
- package/src/settings.ts +131 -135
package/src/herdr-protocol.ts
CHANGED
|
@@ -3,235 +3,233 @@ export const HERDR_AGENT_STATUSES = ["idle", "working", "blocked", "done", "unkn
|
|
|
3
3
|
export type HerdrAgentStatus = (typeof HERDR_AGENT_STATUSES)[number];
|
|
4
4
|
|
|
5
5
|
export interface HerdrPane {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
20
|
}
|
|
21
21
|
|
|
22
22
|
export interface HerdrWorkspace {
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
workspaceId: string;
|
|
24
|
+
label?: string;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
export type HerdrPaneEvent =
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
54
|
|
|
55
55
|
const HERDR_WIDGET_TOPOLOGY_SUBSCRIPTIONS = [
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
56
|
+
{ type: "pane.created" },
|
|
57
|
+
{ type: "pane.closed" },
|
|
58
|
+
{ type: "pane.moved" },
|
|
59
|
+
{ type: "pane.exited" },
|
|
60
|
+
{ type: "pane.agent_detected" },
|
|
61
61
|
] as const;
|
|
62
62
|
|
|
63
63
|
export function herdrWidgetSubscriptions(panes: readonly HerdrPane[]) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
70
|
}
|
|
71
71
|
|
|
72
72
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
73
|
-
|
|
73
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
function requiredString(value: unknown): string | undefined {
|
|
77
|
-
|
|
77
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
function optionalString(value: unknown): string | undefined {
|
|
81
|
-
|
|
81
|
+
return typeof value === "string" ? value : undefined;
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
function agentStatus(value: unknown): HerdrAgentStatus | undefined {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
85
|
+
return typeof value === "string" && HERDR_AGENT_STATUSES.includes(value as HerdrAgentStatus)
|
|
86
|
+
? (value as HerdrAgentStatus)
|
|
87
|
+
: undefined;
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
function stringRecord(value: unknown): Record<string, string> | undefined {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
95
|
}
|
|
96
96
|
|
|
97
97
|
export function parseHerdrPane(value: unknown): HerdrPane | undefined {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
};
|
|
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: optionalString(value.terminal_title_stripped) ?? optionalString(value.terminal_title),
|
|
132
|
+
title: optionalString(value.title),
|
|
133
|
+
};
|
|
135
134
|
}
|
|
136
135
|
|
|
137
136
|
export function parsePaneCurrentResult(value: unknown): HerdrPane {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
137
|
+
if (!isRecord(value) || value.type !== "pane_current") {
|
|
138
|
+
throw new Error("Herdr returned an invalid current-pane response");
|
|
139
|
+
}
|
|
140
|
+
const pane = parseHerdrPane(value.pane);
|
|
141
|
+
if (!pane) throw new Error("Herdr returned an invalid current pane");
|
|
142
|
+
return pane;
|
|
144
143
|
}
|
|
145
144
|
|
|
146
145
|
export function parsePaneListResult(value: unknown): HerdrPane[] {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
146
|
+
if (!isRecord(value) || value.type !== "pane_list" || !Array.isArray(value.panes)) {
|
|
147
|
+
throw new Error("Herdr returned an invalid pane-list response");
|
|
148
|
+
}
|
|
149
|
+
const panes = value.panes.map(parseHerdrPane);
|
|
150
|
+
if (panes.some((pane) => pane === undefined)) {
|
|
151
|
+
throw new Error("Herdr returned an invalid pane in the pane list");
|
|
152
|
+
}
|
|
153
|
+
return panes as HerdrPane[];
|
|
155
154
|
}
|
|
156
155
|
|
|
157
156
|
export function parseAgentListResult(value: unknown): HerdrPane[] {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
157
|
+
if (!isRecord(value) || value.type !== "agent_list" || !Array.isArray(value.agents)) {
|
|
158
|
+
throw new Error("Herdr returned an invalid agent-list response");
|
|
159
|
+
}
|
|
160
|
+
return value.agents.flatMap((entry) => {
|
|
161
|
+
const pane = parseHerdrPane(entry);
|
|
162
|
+
return pane ? [pane] : [];
|
|
163
|
+
});
|
|
165
164
|
}
|
|
166
165
|
|
|
167
166
|
export function parseWorkspaceGetResult(value: unknown): HerdrWorkspace {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
167
|
+
if (!isRecord(value) || value.type !== "workspace_info" || !isRecord(value.workspace)) {
|
|
168
|
+
throw new Error("Herdr returned an invalid workspace response");
|
|
169
|
+
}
|
|
170
|
+
const workspaceId = requiredString(value.workspace.workspace_id);
|
|
171
|
+
if (!workspaceId) throw new Error("Herdr returned an invalid workspace identity");
|
|
172
|
+
return { workspaceId, label: optionalString(value.workspace.label) };
|
|
174
173
|
}
|
|
175
174
|
|
|
176
175
|
export function parseHerdrPaneEvent(value: unknown): HerdrPaneEvent | undefined {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
return undefined;
|
|
176
|
+
if (!isRecord(value) || typeof value.event !== "string" || !isRecord(value.data)) {
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
const data = value.data;
|
|
180
|
+
|
|
181
|
+
if (value.event === "pane.agent_status_changed") {
|
|
182
|
+
const paneId = requiredString(data.pane_id);
|
|
183
|
+
const workspaceId = requiredString(data.workspace_id);
|
|
184
|
+
const status = agentStatus(data.agent_status);
|
|
185
|
+
if (!paneId || !workspaceId || !status) return undefined;
|
|
186
|
+
return {
|
|
187
|
+
type: "agent_status_changed",
|
|
188
|
+
paneId,
|
|
189
|
+
workspaceId,
|
|
190
|
+
agent: optionalString(data.agent),
|
|
191
|
+
agentStatus: status,
|
|
192
|
+
displayAgent: optionalString(data.display_agent),
|
|
193
|
+
stateLabels: stringRecord(data.state_labels),
|
|
194
|
+
title: optionalString(data.title),
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (value.event === "pane_created") {
|
|
199
|
+
const pane = parseHerdrPane(data.pane);
|
|
200
|
+
return pane ? { type: "created", pane } : undefined;
|
|
201
|
+
}
|
|
202
|
+
if (value.event === "pane_closed" || value.event === "pane_exited") {
|
|
203
|
+
const paneId = requiredString(data.pane_id);
|
|
204
|
+
const workspaceId = requiredString(data.workspace_id);
|
|
205
|
+
if (!paneId || !workspaceId) return undefined;
|
|
206
|
+
return {
|
|
207
|
+
type: value.event === "pane_closed" ? "closed" : "exited",
|
|
208
|
+
paneId,
|
|
209
|
+
workspaceId,
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
if (value.event === "pane_moved") {
|
|
213
|
+
const pane = parseHerdrPane(data.pane);
|
|
214
|
+
const previousPaneId = requiredString(data.previous_pane_id);
|
|
215
|
+
const previousWorkspaceId = requiredString(data.previous_workspace_id);
|
|
216
|
+
if (!pane || !previousPaneId || !previousWorkspaceId) return undefined;
|
|
217
|
+
return { type: "moved", pane, previousPaneId, previousWorkspaceId };
|
|
218
|
+
}
|
|
219
|
+
if (value.event === "pane_agent_detected") {
|
|
220
|
+
const paneId = requiredString(data.pane_id);
|
|
221
|
+
const workspaceId = requiredString(data.workspace_id);
|
|
222
|
+
if (!paneId || !workspaceId || typeof data.released !== "boolean") return undefined;
|
|
223
|
+
const finalStatus = data.final_status === null ? undefined : agentStatus(data.final_status);
|
|
224
|
+
if (data.final_status !== undefined && data.final_status !== null && !finalStatus) return undefined;
|
|
225
|
+
return {
|
|
226
|
+
type: "agent_detected",
|
|
227
|
+
paneId,
|
|
228
|
+
workspaceId,
|
|
229
|
+
agent: optionalString(data.agent),
|
|
230
|
+
finalStatus,
|
|
231
|
+
released: data.released,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
return undefined;
|
|
237
235
|
}
|