pi-rtk-optimizer 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/CHANGELOG.md +38 -0
- package/LICENSE +21 -0
- package/README.md +114 -0
- package/config/config.example.json +35 -0
- package/index.ts +3 -0
- package/package.json +59 -0
- package/src/command-completions.ts +49 -0
- package/src/command-rewriter.ts +349 -0
- package/src/compat-commands.ts +207 -0
- package/src/config-modal.ts +600 -0
- package/src/config-store.ts +217 -0
- package/src/constants.ts +6 -0
- package/src/index.ts +291 -0
- package/src/output-compactor-test.ts +120 -0
- package/src/output-compactor.ts +343 -0
- package/src/output-metrics.ts +69 -0
- package/src/rewrite-rules.ts +248 -0
- package/src/techniques/ansi.ts +13 -0
- package/src/techniques/build.ts +155 -0
- package/src/techniques/command-detection.ts +53 -0
- package/src/techniques/git.ts +229 -0
- package/src/techniques/index.ts +16 -0
- package/src/techniques/linter.ts +161 -0
- package/src/techniques/search.ts +76 -0
- package/src/techniques/source.ts +230 -0
- package/src/techniques/test-output.ts +172 -0
- package/src/techniques/truncate.ts +11 -0
- package/src/types-shims.d.ts +131 -0
- package/src/types.ts +114 -0
- package/src/windows-command-helpers.ts +84 -0
- package/src/zellij-modal.ts +1001 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.3.0] - 2026-03-02
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
- Renamed extension/package from `rtk-integration` to `pi-rtk-optimizer` to better reflect its full purpose: RTK command rewrite plus tool-output compaction optimization.
|
|
14
|
+
- Updated extension identity references across config path resolution, modal UI labeling, installation commands, package metadata, and build check artifact naming.
|
|
15
|
+
|
|
16
|
+
## [0.2.0] - 2026-03-02
|
|
17
|
+
|
|
18
|
+
### Changed
|
|
19
|
+
- Reorganized extension into a publish-ready package layout:
|
|
20
|
+
- moved implementation modules into `src/`
|
|
21
|
+
- kept root `index.ts` as stable Pi auto-discovery entrypoint
|
|
22
|
+
- added `config/config.example.json` for distributable config starter
|
|
23
|
+
- Vendored modal UI dependency as `src/zellij-modal.ts` so the package no longer depends on sibling extension paths.
|
|
24
|
+
- Updated TypeScript project includes for the new modular layout.
|
|
25
|
+
|
|
26
|
+
### Added
|
|
27
|
+
- Public repository scaffolding:
|
|
28
|
+
- `README.md`
|
|
29
|
+
- `CHANGELOG.md`
|
|
30
|
+
- `LICENSE`
|
|
31
|
+
- `.gitignore`
|
|
32
|
+
- `.npmignore`
|
|
33
|
+
- Distribution metadata in `package.json`:
|
|
34
|
+
- `description`, `keywords`, `files`, `engines`, `publishConfig`, repository links
|
|
35
|
+
- standard `build`, `lint`, `test`, and `check` scripts
|
|
36
|
+
- Credits section referencing upstream inspiration projects:
|
|
37
|
+
- `mcowger/pi-rtk`
|
|
38
|
+
- `rtk-ai/rtk`
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MasuRii
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# pi-rtk-optimizer
|
|
2
|
+
|
|
3
|
+
RTK rewrite + output compaction extension for the Pi coding agent.
|
|
4
|
+
|
|
5
|
+
`pi-rtk-optimizer` rewrites supported `bash` tool commands to `rtk` equivalents and compacts noisy `tool_result` output (`bash`, `read`, `grep`) to reduce context/token usage while preserving actionable information.
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Command rewrite/suggestion mode for common dev workflows (`git`, `gh`, `cargo`, `npm`, `pytest`, `go`, `docker`, etc.)
|
|
12
|
+
- Runtime guard when `rtk` binary is unavailable
|
|
13
|
+
- Output compaction pipeline:
|
|
14
|
+
- ANSI stripping
|
|
15
|
+
- build/test/linter/git/search summarization
|
|
16
|
+
- source-code filtering (`none`, `minimal`, `aggressive`)
|
|
17
|
+
- smart truncation + hard truncation
|
|
18
|
+
- Interactive TUI settings modal via `/rtk`
|
|
19
|
+
- `/rtk` command completions and utility subcommands (`show`, `verify`, `stats`, `reset`, ...)
|
|
20
|
+
- Session metrics for compaction savings
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
### Local extension folder
|
|
25
|
+
|
|
26
|
+
Place this folder in:
|
|
27
|
+
|
|
28
|
+
- Global: `~/.pi/agent/extensions/pi-rtk-optimizer`
|
|
29
|
+
- Project: `.pi/extensions/pi-rtk-optimizer`
|
|
30
|
+
|
|
31
|
+
Pi auto-discovers these paths.
|
|
32
|
+
|
|
33
|
+
### As an npm package
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pi install npm:pi-rtk-optimizer
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Or from git:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pi install git:github.com/MasuRii/pi-rtk-optimizer
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
Open settings modal:
|
|
48
|
+
|
|
49
|
+
```text
|
|
50
|
+
/rtk
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Subcommands:
|
|
54
|
+
|
|
55
|
+
```text
|
|
56
|
+
/rtk show
|
|
57
|
+
/rtk path
|
|
58
|
+
/rtk verify
|
|
59
|
+
/rtk stats
|
|
60
|
+
/rtk clear-stats
|
|
61
|
+
/rtk reset
|
|
62
|
+
/rtk help
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Configuration
|
|
66
|
+
|
|
67
|
+
Runtime config is stored at:
|
|
68
|
+
|
|
69
|
+
```text
|
|
70
|
+
~/.pi/agent/extensions/pi-rtk-optimizer/config.json
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
A starter file is included as:
|
|
74
|
+
|
|
75
|
+
```text
|
|
76
|
+
config/config.example.json
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Values are normalized/clamped on load and save to prevent invalid runtime state.
|
|
80
|
+
|
|
81
|
+
## Development
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
npm run build
|
|
85
|
+
npm run lint
|
|
86
|
+
npm run test
|
|
87
|
+
npm run check
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Project Layout
|
|
91
|
+
|
|
92
|
+
- `index.ts` - root Pi auto-discovery entrypoint
|
|
93
|
+
- `src/index.ts` - extension bootstrap/event wiring
|
|
94
|
+
- `src/config-modal.ts` - `/rtk` settings modal + command handler
|
|
95
|
+
- `src/config-store.ts` - config normalization/load/save
|
|
96
|
+
- `src/command-rewriter.ts` - command tokenization and rewrite decisions
|
|
97
|
+
- `src/rewrite-rules.ts` - rewrite rule catalog
|
|
98
|
+
- `src/output-compactor.ts` - tool-result compaction pipeline
|
|
99
|
+
- `src/output-metrics.ts` - savings tracking/reporting
|
|
100
|
+
- `src/windows-command-helpers.ts` - Windows bash compatibility helpers
|
|
101
|
+
- `src/techniques/*` - focused compaction techniques
|
|
102
|
+
- `src/zellij-modal.ts` - bundled modal primitives used by settings UI
|
|
103
|
+
- `config/config.example.json` - starter config template
|
|
104
|
+
|
|
105
|
+
## Credits
|
|
106
|
+
|
|
107
|
+
This extension was built with inspiration from:
|
|
108
|
+
|
|
109
|
+
- [`mcowger/pi-rtk`](https://github.com/mcowger/pi-rtk)
|
|
110
|
+
- [`rtk-ai/rtk`](https://github.com/rtk-ai/rtk)
|
|
111
|
+
|
|
112
|
+
## License
|
|
113
|
+
|
|
114
|
+
MIT
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"enabled": true,
|
|
3
|
+
"mode": "rewrite",
|
|
4
|
+
"guardWhenRtkMissing": true,
|
|
5
|
+
"showRewriteNotifications": true,
|
|
6
|
+
"rewriteGitGithub": true,
|
|
7
|
+
"rewriteFilesystem": true,
|
|
8
|
+
"rewriteRust": true,
|
|
9
|
+
"rewriteJavaScript": true,
|
|
10
|
+
"rewritePython": true,
|
|
11
|
+
"rewriteGo": true,
|
|
12
|
+
"rewriteContainers": true,
|
|
13
|
+
"rewriteNetwork": true,
|
|
14
|
+
"rewritePackageManagers": true,
|
|
15
|
+
"outputCompaction": {
|
|
16
|
+
"enabled": true,
|
|
17
|
+
"stripAnsi": true,
|
|
18
|
+
"sourceCodeFilteringEnabled": true,
|
|
19
|
+
"truncate": {
|
|
20
|
+
"enabled": true,
|
|
21
|
+
"maxChars": 12000
|
|
22
|
+
},
|
|
23
|
+
"sourceCodeFiltering": "minimal",
|
|
24
|
+
"smartTruncate": {
|
|
25
|
+
"enabled": true,
|
|
26
|
+
"maxLines": 220
|
|
27
|
+
},
|
|
28
|
+
"aggregateTestOutput": true,
|
|
29
|
+
"filterBuildOutput": true,
|
|
30
|
+
"compactGitOutput": true,
|
|
31
|
+
"aggregateLinterOutput": true,
|
|
32
|
+
"groupSearchOutput": true,
|
|
33
|
+
"trackSavings": true
|
|
34
|
+
}
|
|
35
|
+
}
|
package/index.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pi-rtk-optimizer",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Pi extension that optimizes RTK command rewriting and tool output compaction for the coding agent.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./index.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.ts"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"index.ts",
|
|
12
|
+
"src",
|
|
13
|
+
"config/config.example.json",
|
|
14
|
+
"README.md",
|
|
15
|
+
"CHANGELOG.md",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "npx --yes -p typescript@5.7.3 tsc -p tsconfig.json --noCheck",
|
|
20
|
+
"lint": "npm run build",
|
|
21
|
+
"test": "bun ./src/output-compactor-test.ts",
|
|
22
|
+
"check": "npm run lint && npm run test",
|
|
23
|
+
"build:check": "bunx esbuild ./index.ts --bundle --platform=node --format=esm --outfile=./.pi-rtk-optimizer-check.mjs --external:@mariozechner/pi-coding-agent --external:@mariozechner/pi-tui && bun -e \"import { unlinkSync } from 'node:fs'; unlinkSync('./.pi-rtk-optimizer-check.mjs');\""
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"pi-package",
|
|
27
|
+
"pi",
|
|
28
|
+
"pi-extension",
|
|
29
|
+
"rtk",
|
|
30
|
+
"token-optimization",
|
|
31
|
+
"tool-compaction",
|
|
32
|
+
"coding-agent"
|
|
33
|
+
],
|
|
34
|
+
"author": "MasuRii",
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/MasuRii/pi-rtk-optimizer.git"
|
|
39
|
+
},
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/MasuRii/pi-rtk-optimizer/issues"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/MasuRii/pi-rtk-optimizer#readme",
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=20"
|
|
46
|
+
},
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public"
|
|
49
|
+
},
|
|
50
|
+
"pi": {
|
|
51
|
+
"extensions": [
|
|
52
|
+
"./index.ts"
|
|
53
|
+
]
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"@mariozechner/pi-coding-agent": "*",
|
|
57
|
+
"@mariozechner/pi-tui": "*"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { AutocompleteItem } from "@mariozechner/pi-tui";
|
|
2
|
+
|
|
3
|
+
interface CompletionDefinition {
|
|
4
|
+
name: string;
|
|
5
|
+
description: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const TOP_LEVEL_SUBCOMMANDS: CompletionDefinition[] = [
|
|
9
|
+
{ name: "show", description: "Show current RTK config + runtime summary" },
|
|
10
|
+
{ name: "path", description: "Show RTK config file path" },
|
|
11
|
+
{ name: "verify", description: "Check whether rtk binary is available" },
|
|
12
|
+
{ name: "stats", description: "Show output compaction metrics" },
|
|
13
|
+
{ name: "clear-stats", description: "Clear output compaction metrics" },
|
|
14
|
+
{ name: "reset", description: "Reset RTK settings to defaults" },
|
|
15
|
+
{ name: "help", description: "Show usage help" },
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
function startsWithFilter(value: string, prefix: string): boolean {
|
|
19
|
+
if (!prefix) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
return value.startsWith(prefix);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function mapCompletions(values: CompletionDefinition[]): AutocompleteItem[] {
|
|
26
|
+
return values.map((entry) => ({
|
|
27
|
+
value: entry.name,
|
|
28
|
+
label: entry.name,
|
|
29
|
+
description: entry.description,
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function getRtkArgumentCompletions(argumentPrefix: string): AutocompleteItem[] | null {
|
|
34
|
+
const normalized = argumentPrefix.trimStart().toLowerCase();
|
|
35
|
+
if (!normalized) {
|
|
36
|
+
return mapCompletions(TOP_LEVEL_SUBCOMMANDS);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (normalized.includes(" ")) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const filtered = TOP_LEVEL_SUBCOMMANDS.filter((entry) => startsWithFilter(entry.name, normalized));
|
|
44
|
+
if (filtered.length === 0) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return mapCompletions(filtered);
|
|
49
|
+
}
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import type { RtkIntegrationConfig } from "./types.js";
|
|
2
|
+
import { RTK_REWRITE_RULES, type RtkRewriteCategory, type RtkRewriteRule } from "./rewrite-rules.js";
|
|
3
|
+
|
|
4
|
+
const ENV_PREFIX_PATTERN = /^((?:[A-Za-z_][A-Za-z0-9_]*=[^\s]*\s+)*)/;
|
|
5
|
+
|
|
6
|
+
type CommandToken =
|
|
7
|
+
| {
|
|
8
|
+
type: "segment";
|
|
9
|
+
value: string;
|
|
10
|
+
}
|
|
11
|
+
| {
|
|
12
|
+
type: "separator";
|
|
13
|
+
value: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export interface RewriteDecision {
|
|
17
|
+
changed: boolean;
|
|
18
|
+
originalCommand: string;
|
|
19
|
+
rewrittenCommand: string;
|
|
20
|
+
rule?: RtkRewriteRule;
|
|
21
|
+
reason:
|
|
22
|
+
| "ok"
|
|
23
|
+
| "empty"
|
|
24
|
+
| "already_rtk"
|
|
25
|
+
| "heredoc"
|
|
26
|
+
| "disabled_category"
|
|
27
|
+
| "no_match";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface SegmentRewriteResult {
|
|
31
|
+
value: string;
|
|
32
|
+
changed: boolean;
|
|
33
|
+
rule?: RtkRewriteRule;
|
|
34
|
+
skippedByDisabledCategory: boolean;
|
|
35
|
+
considered: boolean;
|
|
36
|
+
alreadyRtk: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface SingleSegmentRewriteResult {
|
|
40
|
+
changed: boolean;
|
|
41
|
+
rule?: RtkRewriteRule;
|
|
42
|
+
rewrittenBody?: string;
|
|
43
|
+
skippedByDisabledCategory: boolean;
|
|
44
|
+
alreadyRtk: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function categoryEnabled(config: RtkIntegrationConfig, category: RtkRewriteCategory): boolean {
|
|
48
|
+
switch (category) {
|
|
49
|
+
case "gitGithub":
|
|
50
|
+
return config.rewriteGitGithub;
|
|
51
|
+
case "filesystem":
|
|
52
|
+
return config.rewriteFilesystem;
|
|
53
|
+
case "rust":
|
|
54
|
+
return config.rewriteRust;
|
|
55
|
+
case "javascript":
|
|
56
|
+
return config.rewriteJavaScript;
|
|
57
|
+
case "python":
|
|
58
|
+
return config.rewritePython;
|
|
59
|
+
case "go":
|
|
60
|
+
return config.rewriteGo;
|
|
61
|
+
case "containers":
|
|
62
|
+
return config.rewriteContainers;
|
|
63
|
+
case "network":
|
|
64
|
+
return config.rewriteNetwork;
|
|
65
|
+
case "packageManagers":
|
|
66
|
+
return config.rewritePackageManagers;
|
|
67
|
+
default:
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function tokenizeCommand(command: string): CommandToken[] {
|
|
73
|
+
if (!command) {
|
|
74
|
+
return [];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const tokens: CommandToken[] = [];
|
|
78
|
+
let segmentStart = 0;
|
|
79
|
+
let quote: "'" | '"' | "`" | null = null;
|
|
80
|
+
let escaped = false;
|
|
81
|
+
|
|
82
|
+
for (let index = 0; index < command.length; index += 1) {
|
|
83
|
+
const char = command[index];
|
|
84
|
+
|
|
85
|
+
if (escaped) {
|
|
86
|
+
escaped = false;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (quote !== null) {
|
|
91
|
+
if (char === "\\" && quote !== "'") {
|
|
92
|
+
escaped = true;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (char === quote) {
|
|
96
|
+
quote = null;
|
|
97
|
+
}
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (char === "'" || char === '"' || char === "`") {
|
|
102
|
+
quote = char;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const nextChar = command[index + 1] ?? "";
|
|
107
|
+
if ((char === "&" && nextChar === "&") || (char === "|" && nextChar === "|")) {
|
|
108
|
+
const segment = command.slice(segmentStart, index);
|
|
109
|
+
if (segment.length > 0) {
|
|
110
|
+
tokens.push({ type: "segment", value: segment });
|
|
111
|
+
}
|
|
112
|
+
tokens.push({ type: "separator", value: command.slice(index, index + 2) });
|
|
113
|
+
segmentStart = index + 2;
|
|
114
|
+
index += 1;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (char === ";") {
|
|
119
|
+
const segment = command.slice(segmentStart, index);
|
|
120
|
+
if (segment.length > 0) {
|
|
121
|
+
tokens.push({ type: "segment", value: segment });
|
|
122
|
+
}
|
|
123
|
+
tokens.push({ type: "separator", value: ";" });
|
|
124
|
+
segmentStart = index + 1;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const tail = command.slice(segmentStart);
|
|
129
|
+
if (tail.length > 0 || tokens.length === 0) {
|
|
130
|
+
tokens.push({ type: "segment", value: tail });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return tokens;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function isAlreadyRtkCommand(command: string): boolean {
|
|
137
|
+
const trimmed = command.trimStart();
|
|
138
|
+
return /^rtk\s+/.test(trimmed) || /(?:^|\s)[^\s]*\/rtk\s+/.test(trimmed);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function applyPlatformProxyCommandFixups(command: string): string {
|
|
142
|
+
if (process.platform !== "win32") {
|
|
143
|
+
return command;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const windowsProxyExecutables: Array<[string, string]> = [
|
|
147
|
+
["npm", "npm.cmd"],
|
|
148
|
+
["npx", "npx.cmd"],
|
|
149
|
+
["pnpm", "pnpm.cmd"],
|
|
150
|
+
["yarn", "yarn.cmd"],
|
|
151
|
+
];
|
|
152
|
+
|
|
153
|
+
let next = command;
|
|
154
|
+
for (const [base, windowsExecutable] of windowsProxyExecutables) {
|
|
155
|
+
next = next.replace(
|
|
156
|
+
new RegExp(`^(rtk\\s+proxy\\s+)${base}(\\b)`, "i"),
|
|
157
|
+
`$1${windowsExecutable}$2`,
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return next;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function rewriteSingleSegmentCommand(
|
|
165
|
+
segmentCommand: string,
|
|
166
|
+
config: RtkIntegrationConfig,
|
|
167
|
+
): SingleSegmentRewriteResult {
|
|
168
|
+
const envMatch = segmentCommand.match(ENV_PREFIX_PATTERN);
|
|
169
|
+
const envPrefix = envMatch?.[1] ?? "";
|
|
170
|
+
const commandBody = segmentCommand.slice(envPrefix.length);
|
|
171
|
+
|
|
172
|
+
if (isAlreadyRtkCommand(segmentCommand) || isAlreadyRtkCommand(commandBody)) {
|
|
173
|
+
return {
|
|
174
|
+
changed: false,
|
|
175
|
+
alreadyRtk: true,
|
|
176
|
+
skippedByDisabledCategory: false,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
let skippedByDisabledCategory = false;
|
|
181
|
+
|
|
182
|
+
for (const rule of RTK_REWRITE_RULES) {
|
|
183
|
+
if (!categoryEnabled(config, rule.category)) {
|
|
184
|
+
skippedByDisabledCategory = true;
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
rule.matcher.lastIndex = 0;
|
|
189
|
+
if (!rule.matcher.test(commandBody)) {
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
rule.matcher.lastIndex = 0;
|
|
194
|
+
const rewrittenBody = commandBody.replace(rule.matcher, rule.replacement);
|
|
195
|
+
const finalizedRewrittenBody = applyPlatformProxyCommandFixups(rewrittenBody);
|
|
196
|
+
if (finalizedRewrittenBody === commandBody) {
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
changed: true,
|
|
202
|
+
rule,
|
|
203
|
+
rewrittenBody: finalizedRewrittenBody,
|
|
204
|
+
alreadyRtk: false,
|
|
205
|
+
skippedByDisabledCategory,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return {
|
|
210
|
+
changed: false,
|
|
211
|
+
alreadyRtk: false,
|
|
212
|
+
skippedByDisabledCategory,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function rewriteSegment(segment: string, config: RtkIntegrationConfig): SegmentRewriteResult {
|
|
217
|
+
const leadingWhitespace = segment.match(/^\s*/)?.[0] ?? "";
|
|
218
|
+
const trailingWhitespace = segment.match(/\s*$/)?.[0] ?? "";
|
|
219
|
+
const core = segment.trim();
|
|
220
|
+
|
|
221
|
+
if (!core) {
|
|
222
|
+
return {
|
|
223
|
+
value: segment,
|
|
224
|
+
changed: false,
|
|
225
|
+
skippedByDisabledCategory: false,
|
|
226
|
+
considered: false,
|
|
227
|
+
alreadyRtk: false,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const rewrite = rewriteSingleSegmentCommand(core, config);
|
|
232
|
+
if (!rewrite.changed || !rewrite.rule) {
|
|
233
|
+
return {
|
|
234
|
+
value: segment,
|
|
235
|
+
changed: false,
|
|
236
|
+
rule: undefined,
|
|
237
|
+
skippedByDisabledCategory: rewrite.skippedByDisabledCategory,
|
|
238
|
+
considered: true,
|
|
239
|
+
alreadyRtk: rewrite.alreadyRtk,
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const envMatch = core.match(ENV_PREFIX_PATTERN);
|
|
244
|
+
const envPrefix = envMatch?.[1] ?? "";
|
|
245
|
+
const commandBody = core.slice(envPrefix.length);
|
|
246
|
+
rewrite.rule.matcher.lastIndex = 0;
|
|
247
|
+
const rewrittenBody = rewrite.rewrittenBody ?? commandBody.replace(rewrite.rule.matcher, rewrite.rule.replacement);
|
|
248
|
+
|
|
249
|
+
return {
|
|
250
|
+
value: `${leadingWhitespace}${envPrefix}${rewrittenBody}${trailingWhitespace}`,
|
|
251
|
+
changed: true,
|
|
252
|
+
rule: rewrite.rule,
|
|
253
|
+
skippedByDisabledCategory: rewrite.skippedByDisabledCategory,
|
|
254
|
+
considered: true,
|
|
255
|
+
alreadyRtk: false,
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export function computeRewriteDecision(command: string, config: RtkIntegrationConfig): RewriteDecision {
|
|
260
|
+
const original = command;
|
|
261
|
+
const trimmed = command.trim();
|
|
262
|
+
if (!trimmed) {
|
|
263
|
+
return {
|
|
264
|
+
changed: false,
|
|
265
|
+
originalCommand: original,
|
|
266
|
+
rewrittenCommand: original,
|
|
267
|
+
reason: "empty",
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (trimmed.includes("<<")) {
|
|
272
|
+
return {
|
|
273
|
+
changed: false,
|
|
274
|
+
originalCommand: original,
|
|
275
|
+
rewrittenCommand: original,
|
|
276
|
+
reason: "heredoc",
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const tokens = tokenizeCommand(command);
|
|
281
|
+
if (tokens.length === 0) {
|
|
282
|
+
return {
|
|
283
|
+
changed: false,
|
|
284
|
+
originalCommand: original,
|
|
285
|
+
rewrittenCommand: original,
|
|
286
|
+
reason: "no_match",
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
let changed = false;
|
|
291
|
+
let skippedByDisabledCategory = false;
|
|
292
|
+
let firstRule: RtkRewriteRule | undefined;
|
|
293
|
+
let consideredSegments = 0;
|
|
294
|
+
let alreadyRtkSegments = 0;
|
|
295
|
+
|
|
296
|
+
const rewrittenTokens = tokens.map((token) => {
|
|
297
|
+
if (token.type === "separator") {
|
|
298
|
+
return token;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const result = rewriteSegment(token.value, config);
|
|
302
|
+
if (result.considered) {
|
|
303
|
+
consideredSegments += 1;
|
|
304
|
+
if (result.alreadyRtk) {
|
|
305
|
+
alreadyRtkSegments += 1;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
if (result.skippedByDisabledCategory) {
|
|
309
|
+
skippedByDisabledCategory = true;
|
|
310
|
+
}
|
|
311
|
+
if (result.changed) {
|
|
312
|
+
changed = true;
|
|
313
|
+
if (!firstRule) {
|
|
314
|
+
firstRule = result.rule;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
return {
|
|
319
|
+
type: "segment" as const,
|
|
320
|
+
value: result.value,
|
|
321
|
+
};
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
if (changed) {
|
|
325
|
+
return {
|
|
326
|
+
changed: true,
|
|
327
|
+
originalCommand: original,
|
|
328
|
+
rewrittenCommand: rewrittenTokens.map((token) => token.value).join(""),
|
|
329
|
+
rule: firstRule,
|
|
330
|
+
reason: "ok",
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (consideredSegments > 0 && consideredSegments === alreadyRtkSegments) {
|
|
335
|
+
return {
|
|
336
|
+
changed: false,
|
|
337
|
+
originalCommand: original,
|
|
338
|
+
rewrittenCommand: original,
|
|
339
|
+
reason: "already_rtk",
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
return {
|
|
344
|
+
changed: false,
|
|
345
|
+
originalCommand: original,
|
|
346
|
+
rewrittenCommand: original,
|
|
347
|
+
reason: skippedByDisabledCategory ? "disabled_category" : "no_match",
|
|
348
|
+
};
|
|
349
|
+
}
|