@mrclrchtr/supi-settings 1.8.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/node_modules/@mrclrchtr/supi-core/README.md +97 -0
- package/node_modules/@mrclrchtr/supi-core/package.json +57 -0
- package/node_modules/@mrclrchtr/supi-core/src/api.ts +30 -0
- package/node_modules/@mrclrchtr/supi-core/src/config/config-settings.ts +76 -0
- package/node_modules/@mrclrchtr/supi-core/src/config/config.ts +186 -0
- package/node_modules/@mrclrchtr/supi-core/src/config.ts +11 -0
- package/node_modules/@mrclrchtr/supi-core/src/context/context-messages.ts +119 -0
- package/node_modules/@mrclrchtr/supi-core/src/context/context-provider-registry.ts +36 -0
- package/node_modules/@mrclrchtr/supi-core/src/context/context-tag.ts +31 -0
- package/node_modules/@mrclrchtr/supi-core/src/context.ts +16 -0
- package/node_modules/@mrclrchtr/supi-core/src/debug-registry.ts +255 -0
- package/node_modules/@mrclrchtr/supi-core/src/index.ts +30 -0
- package/node_modules/@mrclrchtr/supi-core/src/path-utils.ts +40 -0
- package/node_modules/@mrclrchtr/supi-core/src/path.ts +2 -0
- package/node_modules/@mrclrchtr/supi-core/src/project-roots.ts +170 -0
- package/node_modules/@mrclrchtr/supi-core/src/project.ts +15 -0
- package/node_modules/@mrclrchtr/supi-core/src/registry-utils.ts +86 -0
- package/node_modules/@mrclrchtr/supi-core/src/session-utils.ts +29 -0
- package/node_modules/@mrclrchtr/supi-core/src/session.ts +4 -0
- package/node_modules/@mrclrchtr/supi-core/src/settings/settings-command.ts +15 -0
- package/node_modules/@mrclrchtr/supi-core/src/settings/settings-registry.ts +41 -0
- package/node_modules/@mrclrchtr/supi-core/src/settings/settings-ui.ts +226 -0
- package/node_modules/@mrclrchtr/supi-core/src/settings-ui.ts +2 -0
- package/node_modules/@mrclrchtr/supi-core/src/settings.ts +9 -0
- package/node_modules/@mrclrchtr/supi-core/src/substrate-types.ts +11 -0
- package/node_modules/@mrclrchtr/supi-core/src/terminal.ts +60 -0
- package/node_modules/@mrclrchtr/supi-core/src/tool-framework.ts +116 -0
- package/node_modules/@mrclrchtr/supi-core/src/types.ts +2 -0
- package/package.json +58 -0
- package/src/api.ts +1 -0
- package/src/extension.ts +6 -0
- package/src/index.ts +1 -0
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
// Shared debug event registry for SuPi extensions.
|
|
2
|
+
//
|
|
3
|
+
// Extensions record session-local diagnostic events here. The user-facing
|
|
4
|
+
// supi-debug extension owns policy/configuration and exposes events through a
|
|
5
|
+
// command/tool while this module stays dependency-free for producers.
|
|
6
|
+
|
|
7
|
+
export type DebugLevel = "debug" | "info" | "warning" | "error";
|
|
8
|
+
export type DebugAgentAccess = "off" | "sanitized" | "raw";
|
|
9
|
+
export type DebugNotifyLevel = "off" | "warning" | "error";
|
|
10
|
+
|
|
11
|
+
export interface DebugRegistryConfig {
|
|
12
|
+
/** Whether producers should retain debug events. */
|
|
13
|
+
enabled: boolean;
|
|
14
|
+
/** What the agent-callable debug tool may return. */
|
|
15
|
+
agentAccess: DebugAgentAccess;
|
|
16
|
+
/** Maximum number of session-local events to keep in memory. */
|
|
17
|
+
maxEvents: number;
|
|
18
|
+
/** Minimum level that should notify the user; interpreted by UI extensions. */
|
|
19
|
+
notifyLevel: DebugNotifyLevel;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const DEBUG_REGISTRY_DEFAULTS: DebugRegistryConfig = {
|
|
23
|
+
enabled: false,
|
|
24
|
+
agentAccess: "sanitized",
|
|
25
|
+
maxEvents: 100,
|
|
26
|
+
notifyLevel: "off",
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export interface DebugEventInput {
|
|
30
|
+
source: string;
|
|
31
|
+
level: DebugLevel;
|
|
32
|
+
category: string;
|
|
33
|
+
message: string;
|
|
34
|
+
cwd?: string;
|
|
35
|
+
data?: unknown;
|
|
36
|
+
rawData?: unknown;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface DebugEvent extends DebugEventInput {
|
|
40
|
+
id: number;
|
|
41
|
+
timestamp: number;
|
|
42
|
+
data?: unknown;
|
|
43
|
+
rawData?: unknown;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface DebugEventQuery {
|
|
47
|
+
source?: string;
|
|
48
|
+
level?: DebugLevel;
|
|
49
|
+
category?: string;
|
|
50
|
+
limit?: number;
|
|
51
|
+
includeRaw?: boolean;
|
|
52
|
+
allowRaw?: boolean;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface DebugEventView {
|
|
56
|
+
id: number;
|
|
57
|
+
timestamp: number;
|
|
58
|
+
source: string;
|
|
59
|
+
level: DebugLevel;
|
|
60
|
+
category: string;
|
|
61
|
+
message: string;
|
|
62
|
+
cwd?: string;
|
|
63
|
+
data?: unknown;
|
|
64
|
+
rawData?: unknown;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface DebugEventQueryResult {
|
|
68
|
+
events: DebugEventView[];
|
|
69
|
+
rawAccessDenied: boolean;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface DebugSummary {
|
|
73
|
+
total: number;
|
|
74
|
+
byLevel: Partial<Record<DebugLevel, number>>;
|
|
75
|
+
bySource: Record<string, number>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface DebugRegistryState {
|
|
79
|
+
config: DebugRegistryConfig;
|
|
80
|
+
events: DebugEvent[];
|
|
81
|
+
nextId: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const REGISTRY_KEY = Symbol.for("@mrclrchtr/supi-core/debug-registry");
|
|
85
|
+
const SECRET_KEY_RE = /(?:token|password|passwd|secret|api[_-]?key|authorization|credential)/i;
|
|
86
|
+
const ENV_SECRET_RE =
|
|
87
|
+
/\b([A-Za-z0-9_]*(?:token|password|passwd|secret|api[_-]?key|authorization|credential)[A-Za-z0-9_]*)=(?:'[^']*'|"[^"]*"|\S+)/gi;
|
|
88
|
+
const AUTH_HEADER_RE = /\b(authorization\s*[:=]\s*)(?:bearer\s+)?[^\s;&|]+/gi;
|
|
89
|
+
const URL_SECRET_RE =
|
|
90
|
+
/([?&](?:token|password|passwd|secret|api[_-]?key|authorization|credential)=)[^&\s]+/gi;
|
|
91
|
+
const REDACTED = "[REDACTED]";
|
|
92
|
+
|
|
93
|
+
function cloneConfig(config: DebugRegistryConfig): DebugRegistryConfig {
|
|
94
|
+
return { ...config };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function getState(): DebugRegistryState {
|
|
98
|
+
let state = (globalThis as Record<symbol, unknown>)[REGISTRY_KEY] as
|
|
99
|
+
| DebugRegistryState
|
|
100
|
+
| undefined;
|
|
101
|
+
if (!state) {
|
|
102
|
+
state = {
|
|
103
|
+
config: cloneConfig(DEBUG_REGISTRY_DEFAULTS),
|
|
104
|
+
events: [],
|
|
105
|
+
nextId: 1,
|
|
106
|
+
};
|
|
107
|
+
(globalThis as Record<symbol, unknown>)[REGISTRY_KEY] = state;
|
|
108
|
+
}
|
|
109
|
+
return state;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function normalizeMaxEvents(value: number): number {
|
|
113
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
114
|
+
return DEBUG_REGISTRY_DEFAULTS.maxEvents;
|
|
115
|
+
}
|
|
116
|
+
return Math.floor(value);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function trimToMaxEvents(state: DebugRegistryState): void {
|
|
120
|
+
const maxEvents = normalizeMaxEvents(state.config.maxEvents);
|
|
121
|
+
if (state.events.length <= maxEvents) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
state.events.splice(0, state.events.length - maxEvents);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function matchesQuery(event: DebugEvent, query: DebugEventQuery): boolean {
|
|
128
|
+
if (query.source && event.source !== query.source) return false;
|
|
129
|
+
if (query.level && event.level !== query.level) return false;
|
|
130
|
+
if (query.category && event.category !== query.category) return false;
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function sanitizeString(value: string): string {
|
|
135
|
+
return value
|
|
136
|
+
.replace(ENV_SECRET_RE, (_match, key: string) => `${key}=${REDACTED}`)
|
|
137
|
+
.replace(AUTH_HEADER_RE, (_match, prefix: string) => `${prefix}${REDACTED}`)
|
|
138
|
+
.replace(URL_SECRET_RE, (_match, prefix: string) => `${prefix}${REDACTED}`);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function redactValue(value: unknown, depth: number): unknown {
|
|
142
|
+
if (depth <= 0) return "[MaxDepth]";
|
|
143
|
+
if (typeof value === "string") return sanitizeString(value);
|
|
144
|
+
if (typeof value !== "object" || value === null) return value;
|
|
145
|
+
if (Array.isArray(value)) return value.map((item) => redactValue(item, depth - 1));
|
|
146
|
+
|
|
147
|
+
const redacted: Record<string, unknown> = {};
|
|
148
|
+
for (const [key, item] of Object.entries(value)) {
|
|
149
|
+
redacted[key] = SECRET_KEY_RE.test(key) ? REDACTED : redactValue(item, depth - 1);
|
|
150
|
+
}
|
|
151
|
+
return redacted;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Configure the shared debug registry. Existing events are trimmed to the new max size. */
|
|
155
|
+
export function configureDebugRegistry(config: Partial<DebugRegistryConfig>): DebugRegistryConfig {
|
|
156
|
+
const state = getState();
|
|
157
|
+
state.config = {
|
|
158
|
+
...state.config,
|
|
159
|
+
...config,
|
|
160
|
+
maxEvents: normalizeMaxEvents(config.maxEvents ?? state.config.maxEvents),
|
|
161
|
+
};
|
|
162
|
+
trimToMaxEvents(state);
|
|
163
|
+
return getDebugRegistryConfig();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Return the active debug registry configuration. */
|
|
167
|
+
export function getDebugRegistryConfig(): DebugRegistryConfig {
|
|
168
|
+
return cloneConfig(getState().config);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Best-effort redaction helper for data exposed through sanitized debug views. */
|
|
172
|
+
export function redactDebugData<T>(value: T): T {
|
|
173
|
+
return redactValue(value, 8) as T;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Record a session-local debug event if debugging is enabled. */
|
|
177
|
+
export function recordDebugEvent(input: DebugEventInput): DebugEvent | null {
|
|
178
|
+
const state = getState();
|
|
179
|
+
if (!state.config.enabled) {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const event: DebugEvent = {
|
|
184
|
+
...input,
|
|
185
|
+
id: state.nextId++,
|
|
186
|
+
timestamp: Date.now(),
|
|
187
|
+
data: input.data === undefined ? undefined : redactDebugData(input.data),
|
|
188
|
+
};
|
|
189
|
+
state.events.push(event);
|
|
190
|
+
trimToMaxEvents(state);
|
|
191
|
+
return { ...event };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Query retained debug events newest-first. Results are sanitized unless raw access is requested and allowed. */
|
|
195
|
+
export function getDebugEvents(query: DebugEventQuery = {}): DebugEventQueryResult {
|
|
196
|
+
const state = getState();
|
|
197
|
+
const allowRaw = Boolean(
|
|
198
|
+
query.includeRaw && query.allowRaw && state.config.agentAccess === "raw",
|
|
199
|
+
);
|
|
200
|
+
const rawAccessDenied = Boolean(query.includeRaw && !allowRaw);
|
|
201
|
+
const limit = query.limit && query.limit > 0 ? Math.floor(query.limit) : state.config.maxEvents;
|
|
202
|
+
|
|
203
|
+
const events = state.events
|
|
204
|
+
.filter((event) => matchesQuery(event, query))
|
|
205
|
+
.slice()
|
|
206
|
+
.reverse()
|
|
207
|
+
.slice(0, limit)
|
|
208
|
+
.map((event): DebugEventView => {
|
|
209
|
+
const view: DebugEventView = {
|
|
210
|
+
id: event.id,
|
|
211
|
+
timestamp: event.timestamp,
|
|
212
|
+
source: event.source,
|
|
213
|
+
level: event.level,
|
|
214
|
+
category: event.category,
|
|
215
|
+
message: event.message,
|
|
216
|
+
cwd: event.cwd,
|
|
217
|
+
data: event.data,
|
|
218
|
+
};
|
|
219
|
+
if (allowRaw && event.rawData !== undefined) {
|
|
220
|
+
view.rawData = event.rawData;
|
|
221
|
+
}
|
|
222
|
+
return view;
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
return { events, rawAccessDenied };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Return aggregate debug counts suitable for summary displays. */
|
|
229
|
+
export function getDebugSummary(): DebugSummary | null {
|
|
230
|
+
const events = getState().events;
|
|
231
|
+
if (events.length === 0) {
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const summary: DebugSummary = { total: events.length, byLevel: {}, bySource: {} };
|
|
236
|
+
for (const event of events) {
|
|
237
|
+
summary.byLevel[event.level] = (summary.byLevel[event.level] ?? 0) + 1;
|
|
238
|
+
summary.bySource[event.source] = (summary.bySource[event.source] ?? 0) + 1;
|
|
239
|
+
}
|
|
240
|
+
return summary;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Clear retained events while preserving configuration. */
|
|
244
|
+
export function clearDebugEvents(): void {
|
|
245
|
+
const state = getState();
|
|
246
|
+
state.events = [];
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** Reset the debug registry to defaults; intended for tests. */
|
|
250
|
+
export function resetDebugRegistry(): void {
|
|
251
|
+
const state = getState();
|
|
252
|
+
state.config = cloneConfig(DEBUG_REGISTRY_DEFAULTS);
|
|
253
|
+
state.events = [];
|
|
254
|
+
state.nextId = 1;
|
|
255
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// supi-core — shared infrastructure for SuPi extensions.
|
|
2
|
+
// Provides XML context tag wrapping, unified config system, context-message utilities,
|
|
3
|
+
// settings registry for supi-wide TUI settings, and a shared tool-spec/registration framework.
|
|
4
|
+
//
|
|
5
|
+
// Convenience barrel — re-exports all domain entry points.
|
|
6
|
+
// For lighter imports, use one of the domain subpaths directly
|
|
7
|
+
// (e.g. @mrclrchtr/supi-core/config, @mrclrchtr/supi-core/context).
|
|
8
|
+
|
|
9
|
+
// biome-ignore lint/performance/noReExportAll: intentional convenience barrel
|
|
10
|
+
export * from "./config.ts";
|
|
11
|
+
// biome-ignore lint/performance/noReExportAll: intentional convenience barrel
|
|
12
|
+
export * from "./context.ts";
|
|
13
|
+
// biome-ignore lint/performance/noReExportAll: intentional convenience barrel
|
|
14
|
+
export * from "./debug-registry.ts";
|
|
15
|
+
// biome-ignore lint/performance/noReExportAll: intentional convenience barrel
|
|
16
|
+
export * from "./path.ts";
|
|
17
|
+
// biome-ignore lint/performance/noReExportAll: intentional convenience barrel
|
|
18
|
+
export * from "./project.ts";
|
|
19
|
+
// biome-ignore lint/performance/noReExportAll: intentional convenience barrel
|
|
20
|
+
export * from "./session.ts";
|
|
21
|
+
// biome-ignore lint/performance/noReExportAll: intentional convenience barrel
|
|
22
|
+
export * from "./settings.ts";
|
|
23
|
+
// biome-ignore lint/performance/noReExportAll: intentional convenience barrel
|
|
24
|
+
export * from "./settings-ui.ts";
|
|
25
|
+
// biome-ignore lint/performance/noReExportAll: intentional convenience barrel
|
|
26
|
+
export * from "./terminal.ts";
|
|
27
|
+
// biome-ignore lint/performance/noReExportAll: intentional convenience barrel
|
|
28
|
+
export * from "./tool-framework.ts";
|
|
29
|
+
// biome-ignore lint/performance/noReExportAll: intentional convenience barrel
|
|
30
|
+
export * from "./types.ts";
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as path from "node:path";
|
|
2
|
+
|
|
3
|
+
/** Strip pi's optional leading `@` file-path prefix from a tool input. */
|
|
4
|
+
export function stripToolPathPrefix(target: string): string {
|
|
5
|
+
return target.startsWith("@") ? target.slice(1) : target;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Resolve a tool-style file path from a session cwd.
|
|
10
|
+
*
|
|
11
|
+
* Built-in pi file tools accept a leading `@` prefix in path arguments, so
|
|
12
|
+
* shared SuPi path helpers normalize that prefix before resolving relative
|
|
13
|
+
* paths.
|
|
14
|
+
*/
|
|
15
|
+
export function resolveToolPath(cwd: string, target: string): string {
|
|
16
|
+
return path.resolve(cwd, stripToolPathPrefix(target));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Convert a file path to a file:// URI. */
|
|
20
|
+
export function fileToUri(filePath: string): string {
|
|
21
|
+
const resolved = path.resolve(filePath);
|
|
22
|
+
if (process.platform === "win32") {
|
|
23
|
+
return `file:///${resolved.replace(/\\/g, "/")}`;
|
|
24
|
+
}
|
|
25
|
+
return `file://${resolved}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Convert a file:// URI to a file path. */
|
|
29
|
+
export function uriToFile(uri: string): string {
|
|
30
|
+
if (!uri.startsWith("file://")) return uri;
|
|
31
|
+
let filePath = decodeURIComponent(uri.slice(7));
|
|
32
|
+
if (
|
|
33
|
+
process.platform === "win32" &&
|
|
34
|
+
filePath.startsWith("/") &&
|
|
35
|
+
/^[A-Za-z]:/.test(filePath.slice(1))
|
|
36
|
+
) {
|
|
37
|
+
filePath = filePath.slice(1);
|
|
38
|
+
}
|
|
39
|
+
return filePath;
|
|
40
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
|
|
4
|
+
const IGNORED_DIRECTORIES = new Set(["node_modules", ".git", ".pnpm"]);
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Walk a project directory tree, calling `onDirectory` for each directory.
|
|
8
|
+
* Skips `node_modules`, `.git`, and `.pnpm`.
|
|
9
|
+
* Stops at depth 0.
|
|
10
|
+
*/
|
|
11
|
+
export function walkProject(
|
|
12
|
+
directory: string,
|
|
13
|
+
depth: number,
|
|
14
|
+
onDirectory: (directory: string, entryNames: Set<string>) => void,
|
|
15
|
+
): void {
|
|
16
|
+
let entries: fs.Dirent[];
|
|
17
|
+
try {
|
|
18
|
+
entries = fs.readdirSync(directory, { withFileTypes: true });
|
|
19
|
+
} catch {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const entryNames = new Set(entries.map((entry) => entry.name));
|
|
24
|
+
onDirectory(directory, entryNames);
|
|
25
|
+
|
|
26
|
+
if (depth <= 0) return;
|
|
27
|
+
|
|
28
|
+
for (const entry of entries) {
|
|
29
|
+
if (!entry.isDirectory()) continue;
|
|
30
|
+
if (IGNORED_DIRECTORIES.has(entry.name)) continue;
|
|
31
|
+
walkProject(path.join(directory, entry.name), depth - 1, onDirectory);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Search upward from `startDir` for any of the `markers` files/dirs.
|
|
37
|
+
* Returns the directory containing the first found marker, or `fallback`.
|
|
38
|
+
*/
|
|
39
|
+
export function findProjectRoot(startDir: string, markers: string[], fallback: string): string {
|
|
40
|
+
let dir = path.resolve(startDir);
|
|
41
|
+
const root = path.parse(dir).root;
|
|
42
|
+
|
|
43
|
+
while (dir !== root) {
|
|
44
|
+
for (const marker of markers) {
|
|
45
|
+
if (fs.existsSync(path.join(dir, marker))) {
|
|
46
|
+
return dir;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const parent = path.dirname(dir);
|
|
50
|
+
if (parent === dir) break;
|
|
51
|
+
dir = parent;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return fallback;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Deduplicate overlapping roots, keeping only the topmost (shortest path) roots.
|
|
59
|
+
*/
|
|
60
|
+
export function dedupeTopmostRoots(roots: string[]): string[] {
|
|
61
|
+
const accepted: string[] = [];
|
|
62
|
+
|
|
63
|
+
for (const root of [...new Set(roots.map((entry) => path.resolve(entry)))].sort(byPathDepth)) {
|
|
64
|
+
const isChild = accepted.some((parent) => root !== parent && isWithin(parent, root));
|
|
65
|
+
if (!isChild) {
|
|
66
|
+
accepted.push(root);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return accepted;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Minimal shape accepted by `buildKnownRootsMap`.
|
|
75
|
+
* Structurally compatible with `DetectedProjectServer` and similar
|
|
76
|
+
* `{ name, root }` records — callers may pass a wider type safely.
|
|
77
|
+
*/
|
|
78
|
+
export type KnownRootEntry = { name: string; root: string };
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Build a map of language/server name to sorted, deduplicated root paths.
|
|
82
|
+
*
|
|
83
|
+
* Accepts an array of detected project entries (e.g. from LSP project discovery)
|
|
84
|
+
* and groups them by name with roots sorted by specificity.
|
|
85
|
+
*/
|
|
86
|
+
export function buildKnownRootsMap(detected: KnownRootEntry[]): Map<string, string[]> {
|
|
87
|
+
const next = new Map<string, string[]>();
|
|
88
|
+
|
|
89
|
+
for (const entry of detected) {
|
|
90
|
+
const roots = next.get(entry.name) ?? [];
|
|
91
|
+
if (!roots.includes(entry.root)) roots.push(entry.root);
|
|
92
|
+
next.set(entry.name, sortRootsBySpecificity(roots));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return next;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Merge a new root into an existing list, deduplicating and sorting.
|
|
100
|
+
*
|
|
101
|
+
* Returns the original reference when the root is already present.
|
|
102
|
+
*/
|
|
103
|
+
export function mergeKnownRoots(roots: string[], root: string): string[] {
|
|
104
|
+
if (roots.includes(root)) return roots;
|
|
105
|
+
return sortRootsBySpecificity([...roots, root]);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Resolve the most specific known root that contains `filePath`.
|
|
110
|
+
*
|
|
111
|
+
* Searches the given roots list (presumed sorted by specificity) and returns
|
|
112
|
+
* the first root that contains or equals `filePath`.
|
|
113
|
+
*
|
|
114
|
+
* @returns The matching root string, or `null` when none match.
|
|
115
|
+
*/
|
|
116
|
+
export function resolveKnownRoot(filePath: string, roots: string[]): string | null {
|
|
117
|
+
const resolvedPath = path.resolve(filePath);
|
|
118
|
+
return roots.find((root) => isWithinOrEqual(root, resolvedPath)) ?? null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Sort roots by specificity (deepest/longest first), then alphabetically.
|
|
123
|
+
*
|
|
124
|
+
* Deduplicates by resolved path before sorting.
|
|
125
|
+
*/
|
|
126
|
+
export function sortRootsBySpecificity(roots: string[]): string[] {
|
|
127
|
+
return [...new Set(roots.map((root) => path.resolve(root)))].sort(
|
|
128
|
+
(a, b) => b.length - a.length || a.localeCompare(b),
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Check if `child` is strictly inside `parent`.
|
|
134
|
+
*
|
|
135
|
+
* Returns `true` when `child` is a subdirectory of `parent`.
|
|
136
|
+
* Returns `false` for the same path.
|
|
137
|
+
*/
|
|
138
|
+
export function isWithin(parent: string, child: string): boolean {
|
|
139
|
+
const relative = path.relative(parent, child);
|
|
140
|
+
return relative !== "" && !relative.startsWith(`..${path.sep}`) && relative !== "..";
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Check if `filePath` is inside `root` or is the same path.
|
|
145
|
+
*
|
|
146
|
+
* Combines exact-path equality with `isWithin` semantics.
|
|
147
|
+
*/
|
|
148
|
+
export function isWithinOrEqual(root: string, filePath: string): boolean {
|
|
149
|
+
const relative = path.relative(root, filePath);
|
|
150
|
+
return relative === "" || isWithin(root, filePath);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Comparator for sorting paths by depth (shallowest first), then alphabetically.
|
|
155
|
+
*
|
|
156
|
+
* Useful with `.sort()` on arrays of path strings.
|
|
157
|
+
*/
|
|
158
|
+
export function byPathDepth(a: string, b: string): number {
|
|
159
|
+
const depthDiff = segmentCount(a) - segmentCount(b);
|
|
160
|
+
return depthDiff !== 0 ? depthDiff : a.localeCompare(b);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Count path segments in a resolved absolute path.
|
|
165
|
+
*
|
|
166
|
+
* @example segmentCount("/a/b/c") // 3
|
|
167
|
+
*/
|
|
168
|
+
export function segmentCount(target: string): number {
|
|
169
|
+
return path.resolve(target).split(path.sep).filter(Boolean).length;
|
|
170
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// supi-core project domain — project root discovery and traversal.
|
|
2
|
+
export type { KnownRootEntry } from "./project-roots.ts";
|
|
3
|
+
export {
|
|
4
|
+
buildKnownRootsMap,
|
|
5
|
+
byPathDepth,
|
|
6
|
+
dedupeTopmostRoots,
|
|
7
|
+
findProjectRoot,
|
|
8
|
+
isWithin,
|
|
9
|
+
isWithinOrEqual,
|
|
10
|
+
mergeKnownRoots,
|
|
11
|
+
resolveKnownRoot,
|
|
12
|
+
segmentCount,
|
|
13
|
+
sortRootsBySpecificity,
|
|
14
|
+
walkProject,
|
|
15
|
+
} from "./project-roots.ts";
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Shared registry utility for SuPi extensions.
|
|
2
|
+
//
|
|
3
|
+
// Provides a globalThis-backed registry pattern so that all jiti module instances
|
|
4
|
+
// (resolved through different node_modules symlinks) share the same Map.
|
|
5
|
+
// Without this, each symlink path gets its own module copy and its own Map,
|
|
6
|
+
// so registrations from one instance are invisible to consumers in another.
|
|
7
|
+
|
|
8
|
+
import * as path from "node:path";
|
|
9
|
+
|
|
10
|
+
const SYMBOL_PREFIX = "@mrclrchtr/supi-core/";
|
|
11
|
+
|
|
12
|
+
function getGlobalRegistryMap<T>(name: string): Map<string, T> {
|
|
13
|
+
const key = Symbol.for(SYMBOL_PREFIX + name);
|
|
14
|
+
let map = (globalThis as Record<symbol, unknown>)[key] as Map<string, T> | undefined;
|
|
15
|
+
if (!map) {
|
|
16
|
+
map = new Map<string, T>();
|
|
17
|
+
(globalThis as Record<symbol, unknown>)[key] = map;
|
|
18
|
+
}
|
|
19
|
+
return map;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Create a named registry backed by `globalThis` + `Symbol.for`.
|
|
24
|
+
*
|
|
25
|
+
* The registry is lazily initialized on first access and shared across all
|
|
26
|
+
* jiti module instances via the global symbol namespace.
|
|
27
|
+
*
|
|
28
|
+
* @typeParam T - The value type stored in the registry.
|
|
29
|
+
* @param name - Unique registry name (used to construct the `Symbol.for` key).
|
|
30
|
+
* @returns An object with `register`, `getAll`, and `clear` functions.
|
|
31
|
+
*/
|
|
32
|
+
export function createRegistry<T>(name: string) {
|
|
33
|
+
const getMap = (): Map<string, T> => getGlobalRegistryMap<T>(name);
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
/**
|
|
37
|
+
* Register a value by id. Duplicate ids silently replace the previous registration.
|
|
38
|
+
*/
|
|
39
|
+
register: (id: string, value: T): void => {
|
|
40
|
+
getMap().set(id, value);
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Get all registered values in registration order.
|
|
45
|
+
*/
|
|
46
|
+
getAll: (): T[] => {
|
|
47
|
+
return Array.from(getMap().values());
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Clear all entries from the registry (primarily for tests).
|
|
52
|
+
*/
|
|
53
|
+
clear: (): void => {
|
|
54
|
+
getMap().clear();
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Create a named session-state registry keyed by normalized cwd.
|
|
61
|
+
*
|
|
62
|
+
* This helper is intended for session-scoped runtime services that should be
|
|
63
|
+
* shared across duplicate jiti module instances while keeping package-specific
|
|
64
|
+
* state unions and convenience wrappers local to the calling package.
|
|
65
|
+
*/
|
|
66
|
+
export function createSessionStateRegistry<TState>(name: string) {
|
|
67
|
+
const getMap = (): Map<string, TState> => getGlobalRegistryMap<TState>(name);
|
|
68
|
+
const normalizeCwd = (cwd: string): string => path.resolve(cwd);
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
/** Get the current state for one session cwd. */
|
|
72
|
+
get: (cwd: string): TState | undefined => {
|
|
73
|
+
return getMap().get(normalizeCwd(cwd));
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
/** Store the current state for one session cwd. */
|
|
77
|
+
set: (cwd: string, state: TState): void => {
|
|
78
|
+
getMap().set(normalizeCwd(cwd), state);
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
/** Clear the current state for one session cwd. */
|
|
82
|
+
clear: (cwd: string): void => {
|
|
83
|
+
getMap().delete(normalizeCwd(cwd));
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Generic session-file tree-walking utilities.
|
|
2
|
+
|
|
3
|
+
import type { FileEntry, SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Resolve the active branch path using PI's append-only tree semantics.
|
|
7
|
+
*
|
|
8
|
+
* The active branch is the path from the **last entry** (current leaf)
|
|
9
|
+
* back to the root via `parentId`. This follows PI's tree structure where
|
|
10
|
+
* entries are append-only and the last entry in the file is always the
|
|
11
|
+
* current leaf of the active branch.
|
|
12
|
+
*/
|
|
13
|
+
export function getActiveBranchEntries(entries: FileEntry[]): SessionEntry[] {
|
|
14
|
+
const sessionEntries = entries.filter((e): e is SessionEntry => e.type !== "session");
|
|
15
|
+
const byId = new Map(sessionEntries.map((entry) => [entry.id, entry]));
|
|
16
|
+
const leaf = sessionEntries.at(-1);
|
|
17
|
+
if (!leaf) return [];
|
|
18
|
+
|
|
19
|
+
const path: SessionEntry[] = [];
|
|
20
|
+
const visited = new Set<string>();
|
|
21
|
+
let current: SessionEntry | undefined = leaf;
|
|
22
|
+
while (current) {
|
|
23
|
+
if (visited.has(current.id)) break;
|
|
24
|
+
visited.add(current.id);
|
|
25
|
+
path.unshift(current);
|
|
26
|
+
current = current.parentId ? byId.get(current.parentId) : undefined;
|
|
27
|
+
}
|
|
28
|
+
return path;
|
|
29
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// /supi-settings command registration.
|
|
2
|
+
//
|
|
3
|
+
// Thin wrapper that registers the command and delegates to openSettingsOverlay.
|
|
4
|
+
|
|
5
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import { openSettingsOverlay } from "./settings-ui.ts";
|
|
7
|
+
|
|
8
|
+
export function registerSettingsCommand(pi: ExtensionAPI): void {
|
|
9
|
+
pi.registerCommand("supi-settings", {
|
|
10
|
+
description: "Manage SuPi extension settings",
|
|
11
|
+
handler: async (_args, ctx) => {
|
|
12
|
+
openSettingsOverlay(ctx);
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
}
|