@narumitw/pi-starship 0.43.0 → 0.45.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 +274 -89
- package/package.json +2 -2
- package/src/command-inspector.ts +470 -0
- package/src/command-preview.ts +206 -0
- package/src/commands.ts +187 -152
- package/src/config.ts +177 -67
- package/src/format/style.ts +64 -45
- package/src/modules/activity.ts +1 -1
- package/src/modules/brand.ts +1 -1
- package/src/modules/cache.ts +1 -1
- package/src/modules/catalog.ts +65 -4
- package/src/modules/context.ts +13 -1
- package/src/modules/cost.ts +12 -1
- package/src/modules/development.ts +1 -1
- package/src/modules/directory.ts +1 -1
- package/src/modules/display.ts +14 -0
- package/src/modules/execution.ts +12 -1
- package/src/modules/extension-status.ts +7 -98
- package/src/modules/fill.ts +1 -1
- package/src/modules/git/branch.ts +3 -37
- package/src/modules/git/commit.ts +1 -1
- package/src/modules/git/metrics.ts +8 -2
- package/src/modules/git/runtime.ts +5 -2
- package/src/modules/git/state.ts +1 -1
- package/src/modules/git/status.ts +1 -1
- package/src/modules/git/worktree.ts +1 -1
- package/src/modules/github-pr.ts +24 -0
- package/src/modules/index.ts +15 -6
- package/src/modules/inspection.ts +138 -0
- package/src/modules/model.ts +1 -1
- package/src/modules/provider.ts +1 -1
- package/src/modules/render.ts +23 -19
- package/src/modules/thinking.ts +1 -1
- package/src/modules/time.ts +1 -1
- package/src/modules/tokens.ts +1 -1
- package/src/modules/turn.ts +1 -1
- package/src/modules/types.ts +32 -5
- package/src/pi-starship.ts +149 -27
- package/src/runtime/command.ts +46 -8
- package/src/runtime/execution.ts +15 -3
- package/src/runtime/github-pr.ts +332 -0
- package/src/runtime/helpers.ts +1 -0
- package/src/runtime/refresh-controller.ts +30 -8
- package/src/runtime/types.ts +4 -0
- package/src/runtime/workspace.ts +27 -10
- package/src/installed-packages.ts +0 -84
package/src/runtime/execution.ts
CHANGED
|
@@ -6,14 +6,23 @@ import {
|
|
|
6
6
|
optionStrings,
|
|
7
7
|
safeMetadata,
|
|
8
8
|
} from "./helpers.js";
|
|
9
|
-
import
|
|
9
|
+
import {
|
|
10
|
+
type CollectorContext,
|
|
11
|
+
type MutableModuleSnapshot,
|
|
12
|
+
PRIVATE_STYLE_SELECTOR,
|
|
13
|
+
} from "./types.js";
|
|
10
14
|
|
|
11
15
|
export async function collectExecution(context: CollectorContext): Promise<MutableModuleSnapshot> {
|
|
12
16
|
if (context.input.reason === "periodic" && context.input.previous) {
|
|
13
17
|
const retained: MutableModuleSnapshot = {};
|
|
14
18
|
for (const name of ["os", "container", "hostname", "username"] as const) {
|
|
15
19
|
const values = context.input.previous.modules[name];
|
|
16
|
-
if (context.needs(name)
|
|
20
|
+
if (!context.needs(name) || !values) continue;
|
|
21
|
+
const selector = context.input.previous.styleSelectors?.[name];
|
|
22
|
+
retained[name] = {
|
|
23
|
+
...values,
|
|
24
|
+
...(selector === undefined ? {} : { [PRIVATE_STYLE_SELECTOR]: selector }),
|
|
25
|
+
};
|
|
17
26
|
}
|
|
18
27
|
return retained;
|
|
19
28
|
}
|
|
@@ -116,7 +125,10 @@ function usernameValues(context: CollectorContext): Record<string, string> | und
|
|
|
116
125
|
) {
|
|
117
126
|
return undefined;
|
|
118
127
|
}
|
|
119
|
-
return {
|
|
128
|
+
return {
|
|
129
|
+
user: exactAlias(user, optionMap(context, "username", "aliases")) ?? user,
|
|
130
|
+
[PRIVATE_STYLE_SELECTOR]: privileged ? "root" : "user",
|
|
131
|
+
};
|
|
120
132
|
}
|
|
121
133
|
|
|
122
134
|
function isSsh(context: CollectorContext): boolean {
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import type { GithubPrSnapshot, GithubPrState } from "../modules/types.js";
|
|
2
|
+
import type { WorkspaceExec } from "./types.js";
|
|
3
|
+
|
|
4
|
+
const GH_TIMEOUT_MS = 10_000;
|
|
5
|
+
const MAX_GH_OUTPUT_BYTES = 128 * 1024;
|
|
6
|
+
const MAX_URL_LENGTH = 4_096;
|
|
7
|
+
const MAX_CHECKS = 1_000;
|
|
8
|
+
const MAX_FIELD_LENGTH = 128;
|
|
9
|
+
const RFC3339_TIMESTAMP =
|
|
10
|
+
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d{1,9})?(?:Z|[+-](\d{2}):(\d{2}))$/u;
|
|
11
|
+
export const TERMINAL_PR_LIFETIME_MS = 24 * 60 * 60 * 1_000;
|
|
12
|
+
|
|
13
|
+
const GH_PR_FIELDS = [
|
|
14
|
+
"number",
|
|
15
|
+
"isDraft",
|
|
16
|
+
"url",
|
|
17
|
+
"state",
|
|
18
|
+
"closedAt",
|
|
19
|
+
"mergedAt",
|
|
20
|
+
"reviewDecision",
|
|
21
|
+
"statusCheckRollup",
|
|
22
|
+
] as const;
|
|
23
|
+
|
|
24
|
+
interface GithubPrQueryOptions {
|
|
25
|
+
platform?: NodeJS.Platform;
|
|
26
|
+
environment?: Readonly<Record<string, string | undefined>>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface CheckSummary {
|
|
30
|
+
failed: number;
|
|
31
|
+
pending: number;
|
|
32
|
+
total: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type PullRequestState = "OPEN" | "CLOSED" | "MERGED";
|
|
36
|
+
type ReviewDecision = "APPROVED" | "CHANGES_REQUESTED" | "REVIEW_REQUIRED" | "";
|
|
37
|
+
|
|
38
|
+
export function ghPrViewInvocation(platform: NodeJS.Platform = process.platform): {
|
|
39
|
+
command: string;
|
|
40
|
+
args: string[];
|
|
41
|
+
} {
|
|
42
|
+
return {
|
|
43
|
+
command: platform === "win32" ? "gh.exe" : "gh",
|
|
44
|
+
args: ["pr", "view", "--json", GH_PR_FIELDS.join(",")],
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function githubPrEnvironment(
|
|
49
|
+
source: Readonly<Record<string, string | undefined>> = process.env,
|
|
50
|
+
): Record<string, string | undefined> {
|
|
51
|
+
const environment: Record<string, string | undefined> = Object.create(null);
|
|
52
|
+
for (const [name, value] of Object.entries(source)) {
|
|
53
|
+
const normalized = name.toUpperCase();
|
|
54
|
+
if (normalized === "GH_HOST" || normalized === "GH_REPO") continue;
|
|
55
|
+
Object.defineProperty(environment, name, {
|
|
56
|
+
value,
|
|
57
|
+
writable: true,
|
|
58
|
+
enumerable: true,
|
|
59
|
+
configurable: true,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return environment;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function queryGithubPr(
|
|
66
|
+
exec: WorkspaceExec,
|
|
67
|
+
cwd: string,
|
|
68
|
+
signal?: AbortSignal,
|
|
69
|
+
now = Date.now(),
|
|
70
|
+
options: GithubPrQueryOptions = {},
|
|
71
|
+
): Promise<GithubPrSnapshot | undefined> {
|
|
72
|
+
const invocation = ghPrViewInvocation(options.platform);
|
|
73
|
+
try {
|
|
74
|
+
const result = await exec(invocation.command, invocation.args, {
|
|
75
|
+
cwd,
|
|
76
|
+
signal,
|
|
77
|
+
timeout: GH_TIMEOUT_MS,
|
|
78
|
+
maxOutputBytes: MAX_GH_OUTPUT_BYTES,
|
|
79
|
+
environment: githubPrEnvironment(options.environment),
|
|
80
|
+
});
|
|
81
|
+
if (result.killed || result.code !== 0) return undefined;
|
|
82
|
+
if (Buffer.byteLength(result.stdout) > MAX_GH_OUTPUT_BYTES) return undefined;
|
|
83
|
+
return buildGithubPrSnapshot(JSON.parse(result.stdout), now);
|
|
84
|
+
} catch {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function buildGithubPrSnapshot(
|
|
90
|
+
value: unknown,
|
|
91
|
+
now = Date.now(),
|
|
92
|
+
): GithubPrSnapshot | undefined {
|
|
93
|
+
try {
|
|
94
|
+
const pr = record(value);
|
|
95
|
+
const number = positiveInteger(pr.number);
|
|
96
|
+
const isDraft = requiredBoolean(pr.isDraft);
|
|
97
|
+
const url = requiredBoundedString(pr.url, MAX_URL_LENGTH, true);
|
|
98
|
+
const state = pullRequestState(pr.state);
|
|
99
|
+
const closedAt = optionalTimestamp(pr.closedAt);
|
|
100
|
+
const mergedAt = optionalTimestamp(pr.mergedAt);
|
|
101
|
+
const reviewDecision = reviewDecisionValue(pr.reviewDecision);
|
|
102
|
+
const checks = summarizeChecks(pr.statusCheckRollup);
|
|
103
|
+
const expiresAt = terminalExpiry(state, closedAt, mergedAt);
|
|
104
|
+
if (state !== "OPEN" && (expiresAt === undefined || now >= expiresAt)) return undefined;
|
|
105
|
+
|
|
106
|
+
const presentationState: GithubPrState =
|
|
107
|
+
state === "MERGED" ? "merged" : state === "CLOSED" ? "closed" : isDraft ? "draft" : "open";
|
|
108
|
+
const checksText = formatChecks(checks);
|
|
109
|
+
const review = formatReview(reviewDecision);
|
|
110
|
+
const status = compactStatus(presentationState, checks, checksText, review);
|
|
111
|
+
const snapshot: GithubPrSnapshot = {
|
|
112
|
+
number: String(number),
|
|
113
|
+
link: osc8Link(url, `#${number}`),
|
|
114
|
+
state: presentationState,
|
|
115
|
+
checks: checksText,
|
|
116
|
+
review,
|
|
117
|
+
status,
|
|
118
|
+
...(expiresAt === undefined ? {} : { expiresAt }),
|
|
119
|
+
};
|
|
120
|
+
return Object.freeze(snapshot);
|
|
121
|
+
} catch {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function githubPrSnapshotEqual(
|
|
127
|
+
left: GithubPrSnapshot | undefined,
|
|
128
|
+
right: GithubPrSnapshot | undefined,
|
|
129
|
+
): boolean {
|
|
130
|
+
return JSON.stringify(left) === JSON.stringify(right);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function terminalExpiry(
|
|
134
|
+
state: PullRequestState,
|
|
135
|
+
closedAt: number | undefined,
|
|
136
|
+
mergedAt: number | undefined,
|
|
137
|
+
): number | undefined {
|
|
138
|
+
if (state === "OPEN") return undefined;
|
|
139
|
+
const terminalAt = state === "MERGED" ? mergedAt : closedAt;
|
|
140
|
+
return terminalAt === undefined ? undefined : terminalAt + TERMINAL_PR_LIFETIME_MS;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function summarizeChecks(value: unknown): CheckSummary {
|
|
144
|
+
if (!Array.isArray(value) || value.length > MAX_CHECKS) throw new Error("Invalid PR checks");
|
|
145
|
+
const summary: CheckSummary = { failed: 0, pending: 0, total: value.length };
|
|
146
|
+
for (const item of value) {
|
|
147
|
+
const check = record(item);
|
|
148
|
+
const state = optionalUppercase(check.state);
|
|
149
|
+
const status = optionalUppercase(check.status);
|
|
150
|
+
const conclusion = optionalUppercase(check.conclusion);
|
|
151
|
+
if (state === "SUCCESS") continue;
|
|
152
|
+
if (state === "FAILURE" || state === "ERROR") {
|
|
153
|
+
summary.failed += 1;
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (state === "PENDING" || state === "EXPECTED") {
|
|
157
|
+
summary.pending += 1;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (status && status !== "COMPLETED") {
|
|
161
|
+
summary.pending += 1;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (conclusion === "SUCCESS" || conclusion === "SKIPPED" || conclusion === "NEUTRAL") {
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
if (
|
|
168
|
+
conclusion === "FAILURE" ||
|
|
169
|
+
conclusion === "CANCELLED" ||
|
|
170
|
+
conclusion === "TIMED_OUT" ||
|
|
171
|
+
conclusion === "ACTION_REQUIRED" ||
|
|
172
|
+
conclusion === "STARTUP_FAILURE"
|
|
173
|
+
) {
|
|
174
|
+
summary.failed += 1;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
summary.pending += 1;
|
|
178
|
+
}
|
|
179
|
+
return summary;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function formatChecks(checks: CheckSummary): string {
|
|
183
|
+
if (checks.total === 0) return "no checks";
|
|
184
|
+
if (checks.failed > 0) return `${checks.failed} failing`;
|
|
185
|
+
if (checks.pending > 0) return `${checks.pending} pending`;
|
|
186
|
+
return "checks passing";
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function formatReview(review: ReviewDecision): string {
|
|
190
|
+
if (review === "APPROVED") return "approved";
|
|
191
|
+
if (review === "CHANGES_REQUESTED") return "changes requested";
|
|
192
|
+
if (review === "REVIEW_REQUIRED") return "review required";
|
|
193
|
+
return "";
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function compactStatus(
|
|
197
|
+
state: GithubPrState,
|
|
198
|
+
checks: CheckSummary,
|
|
199
|
+
checksText: string,
|
|
200
|
+
review: string,
|
|
201
|
+
): string {
|
|
202
|
+
if (state === "merged" || state === "closed" || state === "draft") return state;
|
|
203
|
+
if (checks.failed > 0) return checksText;
|
|
204
|
+
if (review === "changes requested") return review;
|
|
205
|
+
if (checks.pending > 0) return checksText;
|
|
206
|
+
if (review === "approved" || review === "review required") return review;
|
|
207
|
+
return checksText;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function osc8Link(value: string, label: string): string {
|
|
211
|
+
if (hasTerminalControls(value)) return label;
|
|
212
|
+
try {
|
|
213
|
+
const url = new URL(value);
|
|
214
|
+
if ((url.protocol !== "http:" && url.protocol !== "https:") || url.username || url.password) {
|
|
215
|
+
return label;
|
|
216
|
+
}
|
|
217
|
+
const normalized = url.toString();
|
|
218
|
+
if (normalized.length > MAX_URL_LENGTH || hasTerminalControls(normalized)) return label;
|
|
219
|
+
return `\u001b]8;;${normalized}\u0007${label}\u001b]8;;\u0007`;
|
|
220
|
+
} catch {
|
|
221
|
+
return label;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function hasTerminalControls(value: string): boolean {
|
|
226
|
+
return Array.from(value).some((character) => {
|
|
227
|
+
const codePoint = character.codePointAt(0) ?? 0;
|
|
228
|
+
return (
|
|
229
|
+
codePoint <= 0x1f ||
|
|
230
|
+
(codePoint >= 0x7f && codePoint <= 0x9f) ||
|
|
231
|
+
(codePoint >= 0xd800 && codePoint <= 0xdfff)
|
|
232
|
+
);
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function record(value: unknown): Record<string, unknown> {
|
|
237
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
238
|
+
throw new Error("Expected PR object");
|
|
239
|
+
}
|
|
240
|
+
return value as Record<string, unknown>;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function positiveInteger(value: unknown): number {
|
|
244
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value <= 0) {
|
|
245
|
+
throw new Error("Invalid PR number");
|
|
246
|
+
}
|
|
247
|
+
return value;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function requiredBoolean(value: unknown): boolean {
|
|
251
|
+
if (typeof value !== "boolean") throw new Error("Invalid PR boolean");
|
|
252
|
+
return value;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function requiredBoundedString(value: unknown, maxLength: number, allowEmpty = false): string {
|
|
256
|
+
if (
|
|
257
|
+
typeof value !== "string" ||
|
|
258
|
+
value.length > maxLength ||
|
|
259
|
+
(!allowEmpty && value.length === 0)
|
|
260
|
+
) {
|
|
261
|
+
throw new Error("Invalid PR string");
|
|
262
|
+
}
|
|
263
|
+
return value;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function optionalUppercase(value: unknown): string | undefined {
|
|
267
|
+
if (value === null || value === undefined || value === "") return undefined;
|
|
268
|
+
return requiredBoundedString(value, MAX_FIELD_LENGTH).toUpperCase();
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function optionalTimestamp(value: unknown): number | undefined {
|
|
272
|
+
if (value === null || value === undefined || value === "") return undefined;
|
|
273
|
+
const text = requiredBoundedString(value, MAX_FIELD_LENGTH);
|
|
274
|
+
const match = RFC3339_TIMESTAMP.exec(text);
|
|
275
|
+
if (!match) throw new Error("Invalid PR timestamp");
|
|
276
|
+
const [
|
|
277
|
+
,
|
|
278
|
+
yearText,
|
|
279
|
+
monthText,
|
|
280
|
+
dayText,
|
|
281
|
+
hourText,
|
|
282
|
+
minuteText,
|
|
283
|
+
secondText,
|
|
284
|
+
offsetHourText,
|
|
285
|
+
offsetMinuteText,
|
|
286
|
+
] = match;
|
|
287
|
+
const year = Number(yearText);
|
|
288
|
+
const month = Number(monthText);
|
|
289
|
+
const day = Number(dayText);
|
|
290
|
+
const hour = Number(hourText);
|
|
291
|
+
const minute = Number(minuteText);
|
|
292
|
+
const second = Number(secondText);
|
|
293
|
+
const offsetHour = Number(offsetHourText ?? 0);
|
|
294
|
+
const offsetMinute = Number(offsetMinuteText ?? 0);
|
|
295
|
+
if (
|
|
296
|
+
month < 1 ||
|
|
297
|
+
month > 12 ||
|
|
298
|
+
day < 1 ||
|
|
299
|
+
day > daysInMonth(year, month) ||
|
|
300
|
+
hour > 23 ||
|
|
301
|
+
minute > 59 ||
|
|
302
|
+
second > 59 ||
|
|
303
|
+
offsetHour > 23 ||
|
|
304
|
+
offsetMinute > 59
|
|
305
|
+
) {
|
|
306
|
+
throw new Error("Invalid PR timestamp");
|
|
307
|
+
}
|
|
308
|
+
const timestamp = Date.parse(text);
|
|
309
|
+
if (!Number.isFinite(timestamp)) throw new Error("Invalid PR timestamp");
|
|
310
|
+
return timestamp;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function daysInMonth(year: number, month: number): number {
|
|
314
|
+
if (month === 2) {
|
|
315
|
+
const leapYear = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
316
|
+
return leapYear ? 29 : 28;
|
|
317
|
+
}
|
|
318
|
+
return month === 4 || month === 6 || month === 9 || month === 11 ? 30 : 31;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function pullRequestState(value: unknown): PullRequestState {
|
|
322
|
+
if (value === "OPEN" || value === "CLOSED" || value === "MERGED") return value;
|
|
323
|
+
throw new Error("Invalid PR state");
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function reviewDecisionValue(value: unknown): ReviewDecision {
|
|
327
|
+
if (value === null || value === undefined || value === "" || value === "UNKNOWN") return "";
|
|
328
|
+
if (value === "APPROVED" || value === "CHANGES_REQUESTED" || value === "REVIEW_REQUIRED") {
|
|
329
|
+
return value;
|
|
330
|
+
}
|
|
331
|
+
throw new Error("Invalid PR review decision");
|
|
332
|
+
}
|
package/src/runtime/helpers.ts
CHANGED
|
@@ -161,6 +161,7 @@ export async function runBounded(
|
|
|
161
161
|
cwd: context.input.cwd,
|
|
162
162
|
timeout: COMMAND_TIMEOUT_MS,
|
|
163
163
|
maxOutputBytes: MAX_COMMAND_OUTPUT_BYTES,
|
|
164
|
+
signal: context.input.signal,
|
|
164
165
|
environment: context.input.environment,
|
|
165
166
|
});
|
|
166
167
|
if (result.code !== 0 || result.killed) return undefined;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export interface AsyncRefreshControllerOptions<Input, Snapshot> {
|
|
2
|
-
read(input: Input): Promise<Snapshot>;
|
|
2
|
+
read(input: Input, signal: AbortSignal): Promise<Snapshot>;
|
|
3
3
|
equal(left: Snapshot | undefined, right: Snapshot): boolean;
|
|
4
4
|
publish(snapshot: Snapshot): void;
|
|
5
5
|
onError?(error: unknown): void;
|
|
@@ -11,17 +11,23 @@ interface RefreshRequest<Input> {
|
|
|
11
11
|
input: Input;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
interface ActiveRefresh<Input> {
|
|
15
|
+
request: RefreshRequest<Input>;
|
|
16
|
+
controller: AbortController;
|
|
17
|
+
}
|
|
18
|
+
|
|
14
19
|
/** Coalesces refreshes to one active read plus the latest pending request. */
|
|
15
20
|
export class AsyncRefreshController<Input, Snapshot> {
|
|
16
21
|
private generation: number | undefined;
|
|
17
22
|
private requestId = 0;
|
|
18
|
-
private
|
|
23
|
+
private active: ActiveRefresh<Input> | undefined;
|
|
19
24
|
private pending: RefreshRequest<Input> | undefined;
|
|
20
25
|
private current: Snapshot | undefined;
|
|
21
26
|
|
|
22
27
|
constructor(private readonly options: AsyncRefreshControllerOptions<Input, Snapshot>) {}
|
|
23
28
|
|
|
24
29
|
start(generation: number): void {
|
|
30
|
+
this.cancelActive("Refresh generation replaced");
|
|
25
31
|
this.generation = generation;
|
|
26
32
|
this.requestId += 1;
|
|
27
33
|
this.pending = undefined;
|
|
@@ -39,7 +45,7 @@ export class AsyncRefreshController<Input, Snapshot> {
|
|
|
39
45
|
requestId: ++this.requestId,
|
|
40
46
|
input,
|
|
41
47
|
};
|
|
42
|
-
if (this.
|
|
48
|
+
if (this.active) {
|
|
43
49
|
this.pending = request;
|
|
44
50
|
return;
|
|
45
51
|
}
|
|
@@ -51,30 +57,46 @@ export class AsyncRefreshController<Input, Snapshot> {
|
|
|
51
57
|
this.requestId += 1;
|
|
52
58
|
this.pending = undefined;
|
|
53
59
|
this.current = undefined;
|
|
60
|
+
this.cancelActive("Refresh controller stopped");
|
|
54
61
|
}
|
|
55
62
|
|
|
56
63
|
private run(request: RefreshRequest<Input>): void {
|
|
57
64
|
if (!this.isCurrentTarget(request)) return;
|
|
58
|
-
|
|
65
|
+
const active: ActiveRefresh<Input> = {
|
|
66
|
+
request,
|
|
67
|
+
controller: new AbortController(),
|
|
68
|
+
};
|
|
69
|
+
this.active = active;
|
|
59
70
|
void this.options
|
|
60
|
-
.read(request.input)
|
|
71
|
+
.read(request.input, active.controller.signal)
|
|
61
72
|
.then((snapshot) => {
|
|
62
|
-
if (!this.isCurrentRequest(request)) return;
|
|
73
|
+
if (this.active !== active || !this.isCurrentRequest(request)) return;
|
|
63
74
|
if (this.options.equal(this.current, snapshot)) return;
|
|
64
75
|
this.current = snapshot;
|
|
65
76
|
this.options.publish(snapshot);
|
|
66
77
|
})
|
|
67
78
|
.catch((error: unknown) => {
|
|
68
|
-
if (
|
|
79
|
+
if (
|
|
80
|
+
this.active === active &&
|
|
81
|
+
this.isCurrentRequest(request) &&
|
|
82
|
+
!active.controller.signal.aborted
|
|
83
|
+
) {
|
|
84
|
+
this.options.onError?.(error);
|
|
85
|
+
}
|
|
69
86
|
})
|
|
70
87
|
.finally(() => {
|
|
71
|
-
this.
|
|
88
|
+
if (this.active !== active) return;
|
|
89
|
+
this.active = undefined;
|
|
72
90
|
const pending = this.pending;
|
|
73
91
|
this.pending = undefined;
|
|
74
92
|
if (pending) this.run(pending);
|
|
75
93
|
});
|
|
76
94
|
}
|
|
77
95
|
|
|
96
|
+
private cancelActive(reason: string): void {
|
|
97
|
+
this.active?.controller.abort(new DOMException(reason, "AbortError"));
|
|
98
|
+
}
|
|
99
|
+
|
|
78
100
|
private isCurrentTarget(request: RefreshRequest<Input>): boolean {
|
|
79
101
|
return this.generation !== undefined && request.generation === this.generation;
|
|
80
102
|
}
|
package/src/runtime/types.ts
CHANGED
|
@@ -16,6 +16,7 @@ export type WorkspaceExec = (
|
|
|
16
16
|
cwd: string;
|
|
17
17
|
timeout: number;
|
|
18
18
|
maxOutputBytes: number;
|
|
19
|
+
signal?: AbortSignal;
|
|
19
20
|
environment: Readonly<Record<string, string | undefined>>;
|
|
20
21
|
},
|
|
21
22
|
) => Promise<WorkspaceExecResult>;
|
|
@@ -45,6 +46,7 @@ export interface WorkspaceRefreshInput {
|
|
|
45
46
|
fileExists?(path: string): Promise<boolean>;
|
|
46
47
|
reason?: "initial" | "event" | "periodic";
|
|
47
48
|
previous?: WorkspaceSnapshot;
|
|
49
|
+
signal?: AbortSignal;
|
|
48
50
|
}
|
|
49
51
|
|
|
50
52
|
export interface CollectorContext {
|
|
@@ -56,4 +58,6 @@ export interface CollectorContext {
|
|
|
56
58
|
needs(name: ModuleName, variable?: string): boolean;
|
|
57
59
|
}
|
|
58
60
|
|
|
61
|
+
export const PRIVATE_STYLE_SELECTOR = "__pi_style_selector";
|
|
62
|
+
|
|
59
63
|
export type MutableModuleSnapshot = Record<string, Record<string, string>>;
|
package/src/runtime/workspace.ts
CHANGED
|
@@ -8,23 +8,24 @@ import { collectExecution } from "./execution.js";
|
|
|
8
8
|
import { createFileSystem } from "./helpers.js";
|
|
9
9
|
import { collectLanguages } from "./languages.js";
|
|
10
10
|
import { collectPackage } from "./package.js";
|
|
11
|
-
import
|
|
12
|
-
CollectorContext,
|
|
13
|
-
MutableModuleSnapshot,
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
import {
|
|
12
|
+
type CollectorContext,
|
|
13
|
+
type MutableModuleSnapshot,
|
|
14
|
+
PRIVATE_STYLE_SELECTOR,
|
|
15
|
+
type WorkspaceEntry,
|
|
16
|
+
type WorkspaceRefreshInput,
|
|
16
17
|
} from "./types.js";
|
|
17
18
|
|
|
18
19
|
export { parseTerraformVersion } from "./deployment.js";
|
|
19
20
|
export { parseDirenvStatus, parseMiseHealth } from "./development.js";
|
|
20
21
|
export { parseRuntimeVersion } from "./languages.js";
|
|
21
|
-
export type { WorkspaceExec, WorkspaceRefreshInput } from "./types.js";
|
|
22
|
+
export type { WorkspaceExec, WorkspaceExecResult, WorkspaceRefreshInput } from "./types.js";
|
|
22
23
|
|
|
23
24
|
export async function collectWorkspaceSnapshot(
|
|
24
25
|
input: WorkspaceRefreshInput,
|
|
25
26
|
): Promise<WorkspaceSnapshot> {
|
|
26
27
|
const requirements = reachableModuleRequirements(input.config);
|
|
27
|
-
if (!hasWorkspaceRequirement(requirements)) return freezeSnapshot({});
|
|
28
|
+
if (input.signal?.aborted || !hasWorkspaceRequirement(requirements)) return freezeSnapshot({});
|
|
28
29
|
const fs = createFileSystem(input);
|
|
29
30
|
let listing: Promise<readonly WorkspaceEntry[]> | undefined;
|
|
30
31
|
const context: CollectorContext = {
|
|
@@ -45,6 +46,7 @@ export async function collectWorkspaceSnapshot(
|
|
|
45
46
|
};
|
|
46
47
|
const modules: MutableModuleSnapshot = {};
|
|
47
48
|
const packageValues = await collectPackage(context);
|
|
49
|
+
if (input.signal?.aborted) return freezeSnapshot({});
|
|
48
50
|
if (packageValues) modules.package = packageValues;
|
|
49
51
|
for (const collector of [
|
|
50
52
|
collectLanguages,
|
|
@@ -53,9 +55,10 @@ export async function collectWorkspaceSnapshot(
|
|
|
53
55
|
collectCloud,
|
|
54
56
|
collectExecution,
|
|
55
57
|
]) {
|
|
58
|
+
if (input.signal?.aborted) return freezeSnapshot({});
|
|
56
59
|
mergeModules(modules, await collector(context));
|
|
57
60
|
}
|
|
58
|
-
return freezeSnapshot(modules);
|
|
61
|
+
return input.signal?.aborted ? freezeSnapshot({}) : freezeSnapshot(modules);
|
|
59
62
|
}
|
|
60
63
|
|
|
61
64
|
export function workspaceSnapshotEqual(
|
|
@@ -79,6 +82,7 @@ const BUILT_IN_ONLY_MODULES = new Set<ModuleName>([
|
|
|
79
82
|
"directory",
|
|
80
83
|
"git_worktree",
|
|
81
84
|
"git_branch",
|
|
85
|
+
"github_pr",
|
|
82
86
|
"git_commit",
|
|
83
87
|
"git_state",
|
|
84
88
|
"git_metrics",
|
|
@@ -105,6 +109,19 @@ function mergeModules(target: MutableModuleSnapshot, source: MutableModuleSnapsh
|
|
|
105
109
|
}
|
|
106
110
|
|
|
107
111
|
function freezeSnapshot(modules: MutableModuleSnapshot): WorkspaceSnapshot {
|
|
108
|
-
|
|
109
|
-
|
|
112
|
+
const styleSelectors: Record<string, string> = {};
|
|
113
|
+
for (const [name, values] of Object.entries(modules)) {
|
|
114
|
+
const selector = Object.hasOwn(values, PRIVATE_STYLE_SELECTOR)
|
|
115
|
+
? values[PRIVATE_STYLE_SELECTOR]
|
|
116
|
+
: undefined;
|
|
117
|
+
delete values[PRIVATE_STYLE_SELECTOR];
|
|
118
|
+
if (selector !== undefined) styleSelectors[name] = selector;
|
|
119
|
+
Object.freeze(values);
|
|
120
|
+
}
|
|
121
|
+
return Object.freeze({
|
|
122
|
+
modules: Object.freeze(modules),
|
|
123
|
+
...(Object.keys(styleSelectors).length > 0
|
|
124
|
+
? { styleSelectors: Object.freeze(styleSelectors) }
|
|
125
|
+
: {}),
|
|
126
|
+
});
|
|
110
127
|
}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
-
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
3
|
-
import { CONFIG_DIR_NAME } from "@earendil-works/pi-coding-agent";
|
|
4
|
-
import {
|
|
5
|
-
buildExtensionStatusIconAliases,
|
|
6
|
-
type ExtensionStatusIconAliasMap,
|
|
7
|
-
} from "./modules/index.js";
|
|
8
|
-
|
|
9
|
-
export interface InstalledPackage {
|
|
10
|
-
packageName: string;
|
|
11
|
-
source: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface InstalledPackageInfo {
|
|
15
|
-
packages: InstalledPackage[];
|
|
16
|
-
aliases: ExtensionStatusIconAliasMap;
|
|
17
|
-
hasStatuslineConflict: boolean;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function readInstalledPackageInfo(
|
|
21
|
-
agentDir: string,
|
|
22
|
-
cwd: string,
|
|
23
|
-
projectTrusted: boolean,
|
|
24
|
-
): InstalledPackageInfo {
|
|
25
|
-
const settingsPaths = [
|
|
26
|
-
join(agentDir, "settings.json"),
|
|
27
|
-
...(projectTrusted ? [join(cwd, CONFIG_DIR_NAME, "settings.json")] : []),
|
|
28
|
-
];
|
|
29
|
-
const packages = settingsPaths.flatMap(readPackagesFromSettings);
|
|
30
|
-
return {
|
|
31
|
-
packages,
|
|
32
|
-
aliases: buildExtensionStatusIconAliases(packages),
|
|
33
|
-
hasStatuslineConflict: packages.some(
|
|
34
|
-
(item) =>
|
|
35
|
-
item.packageName === "@narumitw/pi-statusline" || item.packageName === "pi-statusline",
|
|
36
|
-
),
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function readPackagesFromSettings(settingsPath: string): InstalledPackage[] {
|
|
41
|
-
if (!existsSync(settingsPath)) return [];
|
|
42
|
-
let entries: unknown[];
|
|
43
|
-
try {
|
|
44
|
-
const parsed = JSON.parse(readFileSync(settingsPath, "utf8")) as { packages?: unknown };
|
|
45
|
-
entries = Array.isArray(parsed.packages) ? parsed.packages : [];
|
|
46
|
-
} catch {
|
|
47
|
-
return [];
|
|
48
|
-
}
|
|
49
|
-
const baseDirectory = dirname(settingsPath);
|
|
50
|
-
return entries.flatMap((entry) => {
|
|
51
|
-
const source =
|
|
52
|
-
typeof entry === "string"
|
|
53
|
-
? entry
|
|
54
|
-
: entry &&
|
|
55
|
-
typeof entry === "object" &&
|
|
56
|
-
typeof (entry as { source?: unknown }).source === "string"
|
|
57
|
-
? (entry as { source: string }).source
|
|
58
|
-
: undefined;
|
|
59
|
-
if (!source) return [];
|
|
60
|
-
const packageName = packageNameForSource(source, baseDirectory);
|
|
61
|
-
return packageName ? [{ packageName, source }] : [];
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function packageNameForSource(source: string, baseDirectory: string): string | undefined {
|
|
66
|
-
if (source.startsWith("npm:")) return npmPackageName(source);
|
|
67
|
-
const packageJson = join(resolveSourcePath(source, baseDirectory), "package.json");
|
|
68
|
-
try {
|
|
69
|
-
const parsed = JSON.parse(readFileSync(packageJson, "utf8")) as { name?: unknown };
|
|
70
|
-
return typeof parsed.name === "string" ? parsed.name : undefined;
|
|
71
|
-
} catch {
|
|
72
|
-
return undefined;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function npmPackageName(source: string): string {
|
|
77
|
-
const spec = source.slice("npm:".length);
|
|
78
|
-
if (spec.startsWith("@")) return spec.split("@").slice(0, 2).join("@");
|
|
79
|
-
return spec.split("@")[0] ?? spec;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function resolveSourcePath(source: string, baseDirectory: string): string {
|
|
83
|
-
return isAbsolute(source) ? source : resolve(baseDirectory, source);
|
|
84
|
-
}
|