@mrclrchtr/supi-tree-sitter 1.7.0 → 1.9.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.
Files changed (62) hide show
  1. package/README.md +44 -16
  2. package/node_modules/@mrclrchtr/supi-code-runtime/README.md +13 -0
  3. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/README.md +97 -0
  4. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/package.json +53 -0
  5. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/api.ts +30 -0
  6. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/config/config-settings.ts +76 -0
  7. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/config/config.ts +186 -0
  8. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/config.ts +11 -0
  9. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/context/context-messages.ts +119 -0
  10. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/context/context-provider-registry.ts +36 -0
  11. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/context/context-tag.ts +31 -0
  12. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/context.ts +16 -0
  13. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/debug-registry.ts +255 -0
  14. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/index.ts +30 -0
  15. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/path-utils.ts +40 -0
  16. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/path.ts +2 -0
  17. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/project-roots.ts +170 -0
  18. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/project.ts +15 -0
  19. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/registry-utils.ts +86 -0
  20. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/session-utils.ts +29 -0
  21. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/session.ts +4 -0
  22. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/settings/settings-command.ts +15 -0
  23. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/settings/settings-registry.ts +41 -0
  24. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/settings/settings-ui.ts +226 -0
  25. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/settings-ui.ts +2 -0
  26. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/settings.ts +9 -0
  27. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/substrate-types.ts +11 -0
  28. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/terminal.ts +60 -0
  29. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/tool-framework.ts +116 -0
  30. package/node_modules/@mrclrchtr/supi-code-runtime/node_modules/@mrclrchtr/supi-core/src/types.ts +2 -0
  31. package/node_modules/@mrclrchtr/supi-code-runtime/package.json +40 -0
  32. package/node_modules/@mrclrchtr/supi-code-runtime/src/api.ts +35 -0
  33. package/node_modules/@mrclrchtr/supi-code-runtime/src/capability/types.ts +68 -0
  34. package/node_modules/@mrclrchtr/supi-code-runtime/src/index.ts +31 -0
  35. package/node_modules/@mrclrchtr/supi-code-runtime/src/types.ts +110 -0
  36. package/node_modules/@mrclrchtr/supi-code-runtime/src/workspace/context.ts +41 -0
  37. package/node_modules/@mrclrchtr/supi-code-runtime/src/workspace/runtime.ts +170 -0
  38. package/node_modules/@mrclrchtr/supi-core/README.md +3 -13
  39. package/node_modules/@mrclrchtr/supi-core/package.json +13 -8
  40. package/node_modules/@mrclrchtr/supi-core/src/api.ts +26 -92
  41. package/node_modules/@mrclrchtr/supi-core/src/config.ts +11 -0
  42. package/node_modules/@mrclrchtr/supi-core/src/context.ts +16 -0
  43. package/node_modules/@mrclrchtr/supi-core/src/index.ts +26 -92
  44. package/node_modules/@mrclrchtr/supi-core/src/path.ts +2 -0
  45. package/node_modules/@mrclrchtr/supi-core/src/project.ts +15 -0
  46. package/node_modules/@mrclrchtr/supi-core/src/session.ts +4 -0
  47. package/node_modules/@mrclrchtr/supi-core/src/settings-ui.ts +2 -0
  48. package/node_modules/@mrclrchtr/supi-core/src/settings.ts +9 -0
  49. package/node_modules/@mrclrchtr/supi-core/src/substrate-types.ts +11 -0
  50. package/node_modules/@mrclrchtr/supi-core/src/types.ts +2 -0
  51. package/package.json +9 -6
  52. package/src/provider/tree-sitter-provider.ts +144 -0
  53. package/src/session/runtime-registration.ts +37 -0
  54. package/src/session/runtime.ts +1 -1
  55. package/src/session/service-registry.ts +1 -1
  56. package/src/tool/guidance.ts +29 -25
  57. package/src/tool/handlers.ts +196 -0
  58. package/src/tool/register-tools.ts +107 -0
  59. package/src/tool/tool-specs.ts +117 -0
  60. package/src/tree-sitter.ts +21 -307
  61. package/node_modules/@mrclrchtr/supi-core/src/extension.ts +0 -1
  62. package/src/tool/action-specs.ts +0 -92
package/README.md CHANGED
@@ -1,6 +1,8 @@
1
+ ![SuPi](assets/logo.png)
2
+
1
3
  # @mrclrchtr/supi-tree-sitter
2
4
 
3
- Adds a `tree_sitter` tool to the [pi coding agent](https://github.com/earendil-works/pi) for parser-based structural code analysis.
5
+ Adds focused structural code analysis tools to the [pi coding agent](https://github.com/earendil-works/pi) using Tree-sitter parsers.
4
6
 
5
7
  ## Install
6
8
 
@@ -14,31 +16,39 @@ For local development:
14
16
  pi install ./packages/supi-tree-sitter
15
17
  ```
16
18
 
17
- After editing the source, run `/reload`.
19
+ ![Tree-sitter outline in action](https://raw.githubusercontent.com/mrclrchtr/supi/main/screenshots/supi-tree-sitter.png)
18
20
 
19
21
  ## What you get
20
22
 
21
- After install, pi gets one tool:
23
+ After install, pi gets **6 focused tools** for parser-based structural analysis:
24
+
25
+ - `tree_sitter_outline` — shallow structural outline of declarations in JavaScript/TypeScript files
26
+ - `tree_sitter_imports` — list imports in JavaScript/TypeScript files
27
+ - `tree_sitter_exports` — list exports in JavaScript/TypeScript files
28
+ - `tree_sitter_node_at` — find the exact syntax node and ancestry at a position (any supported grammar)
29
+ - `tree_sitter_query` — run a custom Tree-sitter query against a file (any supported grammar)
30
+ - `tree_sitter_callees` — find outgoing calls from the enclosing function or method at a position (most grammars)
31
+
32
+ ### Outline, imports, exports
33
+
34
+ These three tools work on JavaScript, TypeScript, JSX, and TSX files. They provide parser-level structural information without needing a language server:
35
+
36
+ - `tree_sitter_outline` — top-level declarations plus class/interface/enum members
37
+ - `tree_sitter_imports` — module specifiers and source locations for each import
38
+ - `tree_sitter_exports` — kind, name, and module specifier (for re-exports) of each export
22
39
 
23
- - `tree_sitter` — inspect code structure through Tree-sitter parsers instead of plain text search
40
+ ### Node-at, query, callees
24
41
 
25
- ## `tree_sitter` actions
42
+ These tools work across all or most supported grammars:
26
43
 
27
- | Action | What it is for | Current language coverage |
28
- | --- | --- | --- |
29
- | `outline` | List structural declarations such as functions, classes, interfaces, and methods | JavaScript / TypeScript only |
30
- | `imports` | List import statements | JavaScript / TypeScript only |
31
- | `exports` | List export declarations, re-exports, and export assignments | JavaScript / TypeScript only |
32
- | `node_at` | Show the syntax node at a position, including ancestry | Any supported grammar |
33
- | `query` | Run a custom Tree-sitter query against a file | Any supported grammar |
34
- | `callees` | Find outgoing calls from the enclosing function or method at a position | Supported for most grammars, but not all |
44
+ - `tree_sitter_node_at` exact syntax node type and ancestry at a given file position
45
+ - `tree_sitter_query` custom Tree-sitter query pattern matching on any supported grammar
46
+ - `tree_sitter_callees` outgoing calls from the enclosing function or method at a given position
35
47
 
36
48
  Coordinates use **1-based** line and character columns. Character positions use UTF-16 code units. Relative paths resolve from the session cwd, and a leading `@` on file paths is stripped.
37
49
 
38
50
  ## Supported file families
39
51
 
40
- The current tool description covers:
41
-
42
52
  - JavaScript / TypeScript (`.js`, `.jsx`, `.ts`, `.tsx`, `.mjs`, `.cjs`, `.mts`, `.cts`)
43
53
  - Python (`.py`, `.pyi`)
44
54
  - Rust (`.rs`)
@@ -52,10 +62,25 @@ The current tool description covers:
52
62
  - R (`.r`)
53
63
  - SQL (`.sql`)
54
64
 
65
+ ## Architecture
66
+
67
+ `@mrclrchtr/supi-tree-sitter` is the **structural substrate** in SuPi's
68
+ code-understanding stack. It depends on `@mrclrchtr/supi-core` and
69
+ `@mrclrchtr/supi-code-runtime` for shared contracts, and provides structural
70
+ analysis via a session-scoped Tree-sitter service that publishes its
71
+ capabilities into the shared workspace runtime.
72
+
73
+ ```text
74
+ supi-code-runtime ← shared contracts + workspace runtime
75
+
76
+ supi-tree-sitter ← Tree-sitter WASM + session-scoped service + runtime capabilities
77
+ ```
78
+
55
79
  ## Package surfaces
56
80
 
57
81
  - `@mrclrchtr/supi-tree-sitter/api` — reusable parsing session factory, shared session-scoped structural service access, and shared result types
58
82
  - `@mrclrchtr/supi-tree-sitter/extension` — pi extension entrypoint
83
+ - `@mrclrchtr/supi-tree-sitter/provider/tree-sitter-provider` — shared StructuralProvider adapter
59
84
 
60
85
  Owned session example:
61
86
 
@@ -84,7 +109,10 @@ if (state.kind === "ready") {
84
109
 
85
110
  ## Source
86
111
 
87
- - `src/tree-sitter.ts` — tool registration and action handling
112
+ - `src/tool/tool-specs.ts` — single source of truth for the public tool surface
113
+ - `src/tool/guidance.ts` — prompt surfaces derived from tool specs
114
+ - `src/tool/register-tools.ts` — focused tool registration driven by tool specs
115
+ - `src/tree-sitter.ts` — extension entrypoint (thin wire-up)
88
116
  - `src/session/runtime.ts` — parser and query runtime
89
117
  - `src/session/session.ts` — runtime-backed service helpers and owned session API
90
118
  - `src/session/service-registry.ts` — shared session-scoped structural service registry
@@ -0,0 +1,13 @@
1
+ # @mrclrchtr/supi-code-runtime
2
+
3
+ Shared workspace context, capability contracts, and canonical types for the SuPi code-understanding stack.
4
+
5
+ This is a **library-only package** — it has no pi extension surface, no user-facing tools, and no UI. It provides the shared abstractions that `supi-lsp`, `supi-tree-sitter`, and `supi-code-intelligence` use to communicate capability availability.
6
+
7
+ ## Package surfaces
8
+
9
+ - `@mrclrchtr/supi-code-runtime/api` — shared canonical types, capability interfaces, workspace runtime registry, and typed request context
10
+
11
+ ## License
12
+
13
+ MIT
@@ -0,0 +1,97 @@
1
+ ![SuPi](assets/logo.png)
2
+
3
+ # @mrclrchtr/supi-core
4
+
5
+ Shared infrastructure for SuPi extensions.
6
+
7
+ This is a **pure library** — it does not register any pi commands or tools. The `/supi-settings` command is now available through `@mrclrchtr/supi-settings`.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ pnpm add @mrclrchtr/supi-core
13
+ ```
14
+
15
+ ## Package surfaces
16
+
17
+ - `@mrclrchtr/supi-core/api` — reusable helpers for other packages and extensions
18
+
19
+ ## What you get from the API
20
+
21
+ ### Config helpers
22
+
23
+ - `loadSupiConfig()` — merged config with resolution order `defaults <- global <- project`
24
+ - `loadSupiConfigForScope()` — load one scope at a time for settings UIs
25
+ - `writeSupiConfig()` — persist values
26
+ - `removeSupiConfigKey()` — remove a key or override
27
+
28
+ Config file locations:
29
+
30
+ - global: `~/.pi/agent/supi/config.json`
31
+ - project: `.pi/supi/config.json`
32
+
33
+ ### Settings helpers
34
+
35
+ - `registerSettings()` — register an arbitrary settings section
36
+ - `registerConfigSettings()` — register a config-backed settings section with scoped persistence helpers
37
+ - `registerSettingsCommand()` — register `/supi-settings`
38
+ - `openSettingsOverlay()` — open the shared settings UI directly
39
+ - `createInputSubmenu()` — helper for simple text-entry submenus
40
+
41
+ The built-in settings UI supports:
42
+
43
+ - project/global scope toggle
44
+ - grouped extension sections
45
+ - searchable setting lists
46
+
47
+ ### Context helpers
48
+
49
+ - `wrapExtensionContext()` — wrap injected text in SuPi's `<extension-context>` tag
50
+ - `findLastUserMessageIndex()`
51
+ - `getContextToken()`
52
+ - `getPromptContent()`
53
+ - `pruneAndReorderContextMessages()`
54
+ - `restorePromptContent()`
55
+
56
+ ### Shared registries
57
+
58
+ - context-provider registry for `/supi-context`
59
+ - debug-event registry for producers that want shared debug capture
60
+ - settings registry used by `/supi-settings`
61
+
62
+ ### Project and session helpers
63
+
64
+ - project-root detection and directory walking helpers such as `findProjectRoot()` and `walkProject()`
65
+ - active-branch session helper: `getActiveBranchEntries()`
66
+ - terminal helpers such as `formatTitle()`, `signalWaiting()`, and `signalDone()`
67
+
68
+ ## Example
69
+
70
+ ```ts
71
+ import { loadSupiConfig, registerConfigSettings, wrapExtensionContext } from "@mrclrchtr/supi-core/api";
72
+
73
+ const config = loadSupiConfig("my-extension", process.cwd(), {
74
+ enabled: true,
75
+ });
76
+
77
+ registerConfigSettings({
78
+ id: "my-extension",
79
+ label: "My Extension",
80
+ section: "my-extension",
81
+ defaults: { enabled: true },
82
+ buildItems: () => [],
83
+ persistChange: () => {},
84
+ });
85
+
86
+ const message = wrapExtensionContext("my-extension", "hello", {
87
+ file: "CLAUDE.md",
88
+ turn: 1,
89
+ });
90
+ ```
91
+
92
+ ## Source
93
+
94
+ - `src/api.ts` — exported library surface
95
+ - `src/config.ts` — shared config loading and writing
96
+ - `src/config-settings.ts` — config-backed settings registration helper
97
+ - `src/settings-ui.ts` — shared settings overlay
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@mrclrchtr/supi-core",
3
+ "version": "1.9.0",
4
+ "description": "SuPi core — shared infrastructure for SuPi extensions (XML context tags, config system)",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/mrclrchtr/supi.git"
9
+ },
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "keywords": [
14
+ "pi",
15
+ "pi-coding-agent"
16
+ ],
17
+ "files": [
18
+ "src/**/*.ts",
19
+ "!__tests__"
20
+ ],
21
+ "peerDependencies": {
22
+ "@earendil-works/pi-coding-agent": "*",
23
+ "@earendil-works/pi-tui": "*",
24
+ "typebox": "*"
25
+ },
26
+ "peerDependenciesMeta": {
27
+ "@earendil-works/pi-coding-agent": {
28
+ "optional": true
29
+ },
30
+ "@earendil-works/pi-tui": {
31
+ "optional": true
32
+ },
33
+ "typebox": {
34
+ "optional": true
35
+ }
36
+ },
37
+ "main": "src/api.ts",
38
+ "exports": {
39
+ "./api": "./src/api.ts",
40
+ "./config": "./src/config.ts",
41
+ "./context": "./src/context.ts",
42
+ "./debug": "./src/debug-registry.ts",
43
+ "./package.json": "./package.json",
44
+ "./path": "./src/path.ts",
45
+ "./project": "./src/project.ts",
46
+ "./session": "./src/session.ts",
47
+ "./settings": "./src/settings.ts",
48
+ "./settings-ui": "./src/settings-ui.ts",
49
+ "./terminal": "./src/terminal.ts",
50
+ "./tool-framework": "./src/tool-framework.ts",
51
+ "./types": "./src/types.ts"
52
+ }
53
+ }
@@ -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,76 @@
1
+ // Config-aware settings helper for SuPi config-backed settings sections.
2
+ // Wraps registerSettings() and centralizes selected-scope loading + scoped persistence.
3
+
4
+ import type { SettingItem } from "@earendil-works/pi-tui";
5
+ import type { SettingsScope } from "../settings/settings-registry.ts";
6
+ import { registerSettings } from "../settings/settings-registry.ts";
7
+ import { loadSupiConfigForScope, removeSupiConfigKey, writeSupiConfig } from "./config.ts";
8
+
9
+ export interface ConfigSettingsHelpers {
10
+ /** Write a key to the selected scope's config section. */
11
+ set(key: string, value: unknown): void;
12
+ /** Remove a key from the selected scope's config section. */
13
+ unset(key: string): void;
14
+ }
15
+
16
+ export interface ConfigSettingsOptions<T> {
17
+ /** Extension identifier — e.g. "lsp", "claude-md" */
18
+ id: string;
19
+ /** Human-readable label shown in the UI */
20
+ label: string;
21
+ /** SuPi config section name — e.g. "lsp", "claude-md" */
22
+ section: string;
23
+ /** Default config values */
24
+ defaults: T;
25
+ /** Build SettingItem[] from scoped config. Called by loadValues. */
26
+ buildItems: (settings: T, scope: SettingsScope, cwd: string) => SettingItem[];
27
+ /** Handle a settings change with scoped persistence helpers. */
28
+ persistChange: (
29
+ scope: SettingsScope,
30
+ cwd: string,
31
+ settingId: string,
32
+ value: string,
33
+ helpers: ConfigSettingsHelpers,
34
+ ) => void;
35
+ /** Optional home directory for config resolution (testing). */
36
+ homeDir?: string;
37
+ }
38
+
39
+ /**
40
+ * Register a config-backed settings section.
41
+ *
42
+ * Loads display values from the selected scope only (`defaults <- selected scope`)
43
+ * instead of merged effective runtime config. Provides scoped `set` / `unset`
44
+ * persistence helpers so extensions don't need to wire `writeSupiConfig` /
45
+ * `removeSupiConfigKey` by hand.
46
+ */
47
+ export function registerConfigSettings<T>(options: ConfigSettingsOptions<T>): void {
48
+ registerSettings({
49
+ id: options.id,
50
+ label: options.label,
51
+ loadValues: (scope, cwd) => {
52
+ const settings = loadSupiConfigForScope(options.section, cwd, options.defaults, {
53
+ scope,
54
+ homeDir: options.homeDir,
55
+ });
56
+ return options.buildItems(settings, scope, cwd);
57
+ },
58
+ persistChange: (scope, cwd, settingId, value) => {
59
+ const helpers: ConfigSettingsHelpers = {
60
+ set: (key, val) => {
61
+ writeSupiConfig(
62
+ { section: options.section, scope, cwd },
63
+ { [key]: val },
64
+ { homeDir: options.homeDir },
65
+ );
66
+ },
67
+ unset: (key) => {
68
+ removeSupiConfigKey({ section: options.section, scope, cwd }, key, {
69
+ homeDir: options.homeDir,
70
+ });
71
+ },
72
+ };
73
+ options.persistChange(scope, cwd, settingId, value, helpers);
74
+ },
75
+ });
76
+ }
@@ -0,0 +1,186 @@
1
+ // Shared config system for SuPi extensions.
2
+ //
3
+ // Global config: ~/.pi/agent/supi/config.json
4
+ // Project config: .pi/supi/config.json (relative to cwd)
5
+ // Resolution: hardcoded defaults ← global ← project
6
+
7
+ import * as fs from "node:fs";
8
+ import * as os from "node:os";
9
+ import * as path from "node:path";
10
+
11
+ const GLOBAL_CONFIG_DIR = ".pi/agent/supi";
12
+ const PROJECT_CONFIG_DIR = ".pi/supi";
13
+ const CONFIG_FILE = "config.json";
14
+
15
+ function getGlobalConfigPath(homeDir?: string): string {
16
+ return path.join(homeDir ?? os.homedir(), GLOBAL_CONFIG_DIR, CONFIG_FILE);
17
+ }
18
+
19
+ function getProjectConfigPath(cwd: string): string {
20
+ return path.join(cwd, PROJECT_CONFIG_DIR, CONFIG_FILE);
21
+ }
22
+
23
+ export function readJsonFile(filePath: string): Record<string, unknown> | null {
24
+ let content: string;
25
+ try {
26
+ content = fs.readFileSync(filePath, "utf-8");
27
+ } catch {
28
+ // ENOENT or permission error — silent, file may not exist
29
+ return null;
30
+ }
31
+
32
+ let parsed: unknown;
33
+ try {
34
+ parsed = JSON.parse(content);
35
+ } catch {
36
+ // biome-ignore lint/suspicious/noConsole: deliberate config parse warning
37
+ console.warn(`[supi-core] Failed to parse config file, ignoring: ${filePath}`);
38
+ return null;
39
+ }
40
+
41
+ if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
42
+ return parsed as Record<string, unknown>;
43
+ }
44
+
45
+ // biome-ignore lint/suspicious/noConsole: deliberate config parse warning
46
+ console.warn(`[supi-core] Config file root is not an object, ignoring: ${filePath}`);
47
+ return null;
48
+ }
49
+
50
+ function shallowMerge<T>(base: T, ...overrides: Array<Record<string, unknown> | null>): T {
51
+ let result = { ...base };
52
+ for (const override of overrides) {
53
+ if (!override) continue;
54
+ result = { ...result, ...override };
55
+ }
56
+ return result;
57
+ }
58
+
59
+ export interface SupiConfigOptions {
60
+ homeDir?: string;
61
+ }
62
+
63
+ /**
64
+ * Load and merge config for a given extension section.
65
+ *
66
+ * Resolution order: defaults ← global ← project
67
+ */
68
+ export function loadSupiConfig<T>(
69
+ section: string,
70
+ cwd: string,
71
+ defaults: T,
72
+ options?: SupiConfigOptions,
73
+ ): T {
74
+ const globalConfig = readJsonFile(getGlobalConfigPath(options?.homeDir));
75
+ const projectConfig = readJsonFile(getProjectConfigPath(cwd));
76
+
77
+ const globalSection = extractSection(globalConfig, section);
78
+ const projectSection = extractSection(projectConfig, section);
79
+
80
+ return shallowMerge(defaults, globalSection, projectSection);
81
+ }
82
+
83
+ /**
84
+ * Load config for a single scope only.
85
+ *
86
+ * Resolution order: defaults ← selected scope
87
+ *
88
+ * This is useful for settings UIs that need to show the raw values stored in
89
+ * one scope, rather than the effective merged config.
90
+ */
91
+ export function loadSupiConfigForScope<T>(
92
+ section: string,
93
+ cwd: string,
94
+ defaults: T,
95
+ options: { scope: "global" | "project" } & SupiConfigOptions,
96
+ ): T {
97
+ const config =
98
+ options.scope === "global"
99
+ ? readJsonFile(getGlobalConfigPath(options.homeDir))
100
+ : readJsonFile(getProjectConfigPath(cwd));
101
+
102
+ const scopedSection = extractSection(config, section);
103
+ return shallowMerge(defaults, scopedSection);
104
+ }
105
+
106
+ export interface SupiConfigLocation {
107
+ section: string;
108
+ scope: "global" | "project";
109
+ cwd: string;
110
+ }
111
+
112
+ /**
113
+ * Write config values for a given extension section.
114
+ */
115
+ export function writeSupiConfig(
116
+ loc: SupiConfigLocation,
117
+ value: Record<string, unknown>,
118
+ options?: SupiConfigOptions,
119
+ ): void {
120
+ const configPath =
121
+ loc.scope === "global" ? getGlobalConfigPath(options?.homeDir) : getProjectConfigPath(loc.cwd);
122
+
123
+ const dir = path.dirname(configPath);
124
+ fs.mkdirSync(dir, { recursive: true });
125
+
126
+ const existing = readJsonFile(configPath) ?? {};
127
+ existing[loc.section] = {
128
+ ...((existing[loc.section] as Record<string, unknown>) ?? {}),
129
+ ...value,
130
+ };
131
+
132
+ fs.writeFileSync(configPath, `${JSON.stringify(existing, null, 2)}\n`, "utf-8");
133
+ }
134
+
135
+ /**
136
+ * Remove a key from a config section.
137
+ * Used by `interval default` to remove the project override.
138
+ */
139
+ export function removeSupiConfigKey(
140
+ loc: SupiConfigLocation,
141
+ key: string,
142
+ options?: SupiConfigOptions,
143
+ ): void {
144
+ const configPath =
145
+ loc.scope === "global" ? getGlobalConfigPath(options?.homeDir) : getProjectConfigPath(loc.cwd);
146
+
147
+ const existing = readJsonFile(configPath);
148
+ if (!existing) return;
149
+
150
+ const sectionData = existing[loc.section] as Record<string, unknown> | undefined;
151
+ if (!sectionData) return;
152
+
153
+ delete sectionData[key];
154
+
155
+ if (Object.keys(sectionData).length === 0) {
156
+ delete existing[loc.section];
157
+ }
158
+
159
+ const dir = path.dirname(configPath);
160
+ fs.mkdirSync(dir, { recursive: true });
161
+
162
+ const content = Object.keys(existing).length > 0 ? `${JSON.stringify(existing, null, 2)}\n` : "";
163
+
164
+ if (content) {
165
+ // Directory guaranteed to exist since we just read from it
166
+ fs.writeFileSync(configPath, content, "utf-8");
167
+ } else {
168
+ try {
169
+ fs.unlinkSync(configPath);
170
+ } catch {
171
+ // File may not exist
172
+ }
173
+ }
174
+ }
175
+
176
+ function extractSection(
177
+ config: Record<string, unknown> | null,
178
+ section: string,
179
+ ): Record<string, unknown> | null {
180
+ if (!config) return null;
181
+ const data = config[section];
182
+ if (typeof data === "object" && data !== null && !Array.isArray(data)) {
183
+ return data as Record<string, unknown>;
184
+ }
185
+ return null;
186
+ }
@@ -0,0 +1,11 @@
1
+ // supi-core config domain — config loading and config-settings helpers.
2
+ export type { SupiConfigLocation, SupiConfigOptions } from "./config/config.ts";
3
+ export {
4
+ loadSupiConfig,
5
+ loadSupiConfigForScope,
6
+ readJsonFile,
7
+ removeSupiConfigKey,
8
+ writeSupiConfig,
9
+ } from "./config/config.ts";
10
+ export type { ConfigSettingsHelpers, ConfigSettingsOptions } from "./config/config-settings.ts";
11
+ export { registerConfigSettings } from "./config/config-settings.ts";