pi-rtk-optimizer 0.3.3 → 0.5.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 (38) hide show
  1. package/CHANGELOG.md +102 -67
  2. package/README.md +292 -290
  3. package/config/config.example.json +36 -35
  4. package/package.json +4 -4
  5. package/src/additional-coverage-test.ts +278 -0
  6. package/src/boolean-format.ts +3 -0
  7. package/src/command-rewriter-test.ts +160 -120
  8. package/src/command-rewriter.ts +594 -585
  9. package/src/config-modal-test.ts +168 -0
  10. package/src/config-modal.ts +613 -600
  11. package/src/config-store.ts +224 -217
  12. package/src/index-test.ts +54 -0
  13. package/src/index.ts +410 -289
  14. package/src/output-compactor-test.ts +500 -158
  15. package/src/output-compactor.ts +432 -349
  16. package/src/record-utils.ts +6 -0
  17. package/src/rewrite-bypass.ts +332 -173
  18. package/src/rewrite-pipeline-safety.ts +154 -0
  19. package/src/rewrite-rules.ts +255 -255
  20. package/src/rtk-command-environment.ts +64 -0
  21. package/src/runtime-guard-test.ts +42 -50
  22. package/src/runtime-guard.ts +14 -14
  23. package/src/techniques/build.ts +155 -155
  24. package/src/techniques/emoji.ts +91 -0
  25. package/src/techniques/git.ts +231 -229
  26. package/src/techniques/index.ts +10 -16
  27. package/src/techniques/linter.ts +151 -161
  28. package/src/techniques/path-utils.ts +67 -0
  29. package/src/techniques/rtk.ts +136 -0
  30. package/src/techniques/search.ts +67 -76
  31. package/src/techniques/source.ts +253 -253
  32. package/src/techniques/test-output.ts +172 -172
  33. package/src/test-helpers.ts +10 -0
  34. package/src/tool-execution-sanitizer.ts +69 -0
  35. package/src/types-shims.d.ts +192 -183
  36. package/src/types.ts +103 -114
  37. package/src/zellij-modal.ts +1001 -1001
  38. package/src/compat-commands.ts +0 -207
@@ -1,253 +1,253 @@
1
- export type Language =
2
- | "typescript"
3
- | "javascript"
4
- | "python"
5
- | "rust"
6
- | "go"
7
- | "java"
8
- | "c"
9
- | "cpp"
10
- | "unknown";
11
-
12
- const LANGUAGE_EXTENSIONS: Record<string, Language> = {
13
- ".ts": "typescript",
14
- ".tsx": "typescript",
15
- ".js": "javascript",
16
- ".jsx": "javascript",
17
- ".mjs": "javascript",
18
- ".py": "python",
19
- ".pyw": "python",
20
- ".rs": "rust",
21
- ".go": "go",
22
- ".java": "java",
23
- ".c": "c",
24
- ".h": "c",
25
- ".cpp": "cpp",
26
- ".hpp": "cpp",
27
- ".cc": "cpp",
28
- };
29
-
30
- interface CommentPatterns {
31
- line?: string;
32
- blockStart?: string;
33
- blockEnd?: string;
34
- docLine?: string;
35
- docBlockStart?: string;
36
- }
37
-
38
- const COMMENT_PATTERNS: Record<Language, CommentPatterns> = {
39
- typescript: { line: "//", blockStart: "/*", blockEnd: "*/", docBlockStart: "/**" },
40
- javascript: { line: "//", blockStart: "/*", blockEnd: "*/", docBlockStart: "/**" },
41
- python: { line: "#", blockStart: '"""', blockEnd: '"""', docBlockStart: '"""' },
42
- rust: { line: "//", blockStart: "/*", blockEnd: "*/", docLine: "///", docBlockStart: "/**" },
43
- go: { line: "//", blockStart: "/*", blockEnd: "*/", docBlockStart: "/**" },
44
- java: { line: "//", blockStart: "/*", blockEnd: "*/", docBlockStart: "/**" },
45
- c: { line: "//", blockStart: "/*", blockEnd: "*/", docBlockStart: "/**" },
46
- cpp: { line: "//", blockStart: "/*", blockEnd: "*/", docBlockStart: "/**" },
47
- unknown: { line: "//", blockStart: "/*", blockEnd: "*/" },
48
- };
49
-
50
- const IMPORT_PATTERN = /^(use\s+|import\s+|from\s+|require\(|#include)/;
51
- const SIGNATURE_PATTERN = /^(pub\s+)?(async\s+)?(fn|def|function|func|class|struct|enum|trait|interface|type)\s+\w+/;
52
- const CONST_PATTERN = /^(const|static|let|pub\s+const|pub\s+static)\s+/;
53
-
54
- export function detectLanguage(filePath: string): Language {
55
- const lastDot = filePath.lastIndexOf(".");
56
- if (lastDot === -1) {
57
- return "unknown";
58
- }
59
- const extension = filePath.slice(lastDot).toLowerCase();
60
- return LANGUAGE_EXTENSIONS[extension] ?? "unknown";
61
- }
62
-
63
- export function filterMinimal(content: string, language: Language): string {
64
- const patterns = COMMENT_PATTERNS[language];
65
- const lines = content.split("\n");
66
- const result: string[] = [];
67
- let inBlockComment = false;
68
- let inDocstring = false;
69
- let inUserscriptMetadataBlock = false;
70
- const userscriptMetadataStartPattern = /^\/\/\s*==\s*userscript\s*==$/i;
71
- const userscriptMetadataContentPattern = /^\/\/\s*@\w+/;
72
- const userscriptMetadataEndPattern = /^\/\/\s*==\s*\/userscript\s*==$/i;
73
-
74
- for (const line of lines) {
75
- const trimmed = line.trim();
76
- const isUserscriptMetadataStart = userscriptMetadataStartPattern.test(trimmed);
77
- const isUserscriptMetadataContent = userscriptMetadataContentPattern.test(trimmed);
78
- const isUserscriptMetadataEnd = userscriptMetadataEndPattern.test(trimmed);
79
-
80
- if (isUserscriptMetadataStart) {
81
- inUserscriptMetadataBlock = true;
82
- result.push(line);
83
- continue;
84
- }
85
-
86
- if (inUserscriptMetadataBlock) {
87
- result.push(line);
88
- if (isUserscriptMetadataEnd) {
89
- inUserscriptMetadataBlock = false;
90
- } else if (isUserscriptMetadataContent) {
91
- // Preserve metadata key/value lines (e.g. // @name) within the userscript block.
92
- }
93
- continue;
94
- }
95
-
96
- if (patterns.blockStart && patterns.blockEnd) {
97
- if (
98
- !inDocstring &&
99
- trimmed.includes(patterns.blockStart) &&
100
- !(patterns.docBlockStart && trimmed.startsWith(patterns.docBlockStart))
101
- ) {
102
- inBlockComment = true;
103
- }
104
-
105
- if (inBlockComment) {
106
- if (trimmed.includes(patterns.blockEnd)) {
107
- inBlockComment = false;
108
- }
109
- continue;
110
- }
111
- }
112
-
113
- if (language === "python" && trimmed.startsWith('"""')) {
114
- inDocstring = !inDocstring;
115
- result.push(line);
116
- continue;
117
- }
118
-
119
- if (inDocstring) {
120
- result.push(line);
121
- continue;
122
- }
123
-
124
- if (patterns.line && trimmed.startsWith(patterns.line)) {
125
- if (patterns.docLine && trimmed.startsWith(patterns.docLine)) {
126
- result.push(line);
127
- }
128
- continue;
129
- }
130
-
131
- if (trimmed.length === 0) {
132
- result.push("");
133
- continue;
134
- }
135
-
136
- result.push(line);
137
- }
138
-
139
- return result
140
- .join("\n")
141
- .replace(/\n{3,}/g, "\n\n")
142
- .trim();
143
- }
144
-
145
- export function filterAggressive(content: string, language: Language): string {
146
- const minimal = filterMinimal(content, language);
147
- const lines = minimal.split("\n");
148
- const result: string[] = [];
149
- let braceDepth = 0;
150
- let inImplementation = false;
151
-
152
- for (const line of lines) {
153
- const trimmed = line.trim();
154
-
155
- if (IMPORT_PATTERN.test(trimmed)) {
156
- result.push(line);
157
- continue;
158
- }
159
-
160
- if (SIGNATURE_PATTERN.test(trimmed)) {
161
- result.push(line);
162
- inImplementation = true;
163
- braceDepth = 0;
164
- continue;
165
- }
166
-
167
- const openBraces = (trimmed.match(/\{/g) ?? []).length;
168
- const closeBraces = (trimmed.match(/\}/g) ?? []).length;
169
-
170
- if (inImplementation) {
171
- braceDepth += openBraces;
172
- braceDepth -= closeBraces;
173
-
174
- if (braceDepth <= 1 && (trimmed === "{" || trimmed === "}" || trimmed.endsWith("{"))) {
175
- result.push(line);
176
- }
177
-
178
- if (braceDepth <= 0) {
179
- inImplementation = false;
180
- if (trimmed.length > 0 && trimmed !== "}") {
181
- result.push(" // ... implementation");
182
- }
183
- }
184
- continue;
185
- }
186
-
187
- if (CONST_PATTERN.test(trimmed)) {
188
- result.push(line);
189
- }
190
- }
191
-
192
- return result.join("\n").trim();
193
- }
194
-
195
- export function smartTruncate(content: string, maxLines: number, _language: Language): string {
196
- const lines = content.split("\n");
197
- if (lines.length <= maxLines) {
198
- return content;
199
- }
200
-
201
- const result: string[] = [];
202
- let keptLines = 0;
203
- let skippedSection = false;
204
-
205
- for (const line of lines) {
206
- const trimmed = line.trim();
207
- const isImportant =
208
- SIGNATURE_PATTERN.test(trimmed) ||
209
- IMPORT_PATTERN.test(trimmed) ||
210
- trimmed.startsWith("pub ") ||
211
- trimmed.startsWith("export ") ||
212
- trimmed === "}" ||
213
- trimmed === "{";
214
-
215
- if (isImportant || keptLines < maxLines / 2) {
216
- if (skippedSection) {
217
- result.push(` // ... ${lines.length - keptLines} lines omitted`);
218
- skippedSection = false;
219
- }
220
- result.push(line);
221
- keptLines += 1;
222
- } else {
223
- skippedSection = true;
224
- }
225
-
226
- if (keptLines >= maxLines - 1) {
227
- break;
228
- }
229
- }
230
-
231
- if (skippedSection || keptLines < lines.length) {
232
- result.push(`// ... ${lines.length - keptLines} more lines (total: ${lines.length})`);
233
- }
234
-
235
- return result.join("\n");
236
- }
237
-
238
- export function filterSourceCode(
239
- content: string,
240
- language: Language,
241
- level: "none" | "minimal" | "aggressive",
242
- ): string {
243
- switch (level) {
244
- case "none":
245
- return content;
246
- case "minimal":
247
- return filterMinimal(content, language);
248
- case "aggressive":
249
- return filterAggressive(content, language);
250
- default:
251
- return content;
252
- }
253
- }
1
+ export type Language =
2
+ | "typescript"
3
+ | "javascript"
4
+ | "python"
5
+ | "rust"
6
+ | "go"
7
+ | "java"
8
+ | "c"
9
+ | "cpp"
10
+ | "unknown";
11
+
12
+ const LANGUAGE_EXTENSIONS: Record<string, Language> = {
13
+ ".ts": "typescript",
14
+ ".tsx": "typescript",
15
+ ".js": "javascript",
16
+ ".jsx": "javascript",
17
+ ".mjs": "javascript",
18
+ ".py": "python",
19
+ ".pyw": "python",
20
+ ".rs": "rust",
21
+ ".go": "go",
22
+ ".java": "java",
23
+ ".c": "c",
24
+ ".h": "c",
25
+ ".cpp": "cpp",
26
+ ".hpp": "cpp",
27
+ ".cc": "cpp",
28
+ };
29
+
30
+ interface CommentPatterns {
31
+ line?: string;
32
+ blockStart?: string;
33
+ blockEnd?: string;
34
+ docLine?: string;
35
+ docBlockStart?: string;
36
+ }
37
+
38
+ const COMMENT_PATTERNS: Record<Language, CommentPatterns> = {
39
+ typescript: { line: "//", blockStart: "/*", blockEnd: "*/", docBlockStart: "/**" },
40
+ javascript: { line: "//", blockStart: "/*", blockEnd: "*/", docBlockStart: "/**" },
41
+ python: { line: "#", blockStart: '"""', blockEnd: '"""', docBlockStart: '"""' },
42
+ rust: { line: "//", blockStart: "/*", blockEnd: "*/", docLine: "///", docBlockStart: "/**" },
43
+ go: { line: "//", blockStart: "/*", blockEnd: "*/", docBlockStart: "/**" },
44
+ java: { line: "//", blockStart: "/*", blockEnd: "*/", docBlockStart: "/**" },
45
+ c: { line: "//", blockStart: "/*", blockEnd: "*/", docBlockStart: "/**" },
46
+ cpp: { line: "//", blockStart: "/*", blockEnd: "*/", docBlockStart: "/**" },
47
+ unknown: { line: "//", blockStart: "/*", blockEnd: "*/" },
48
+ };
49
+
50
+ const IMPORT_PATTERN = /^(use\s+|import\s+|from\s+|require\(|#include)/;
51
+ const SIGNATURE_PATTERN = /^(pub\s+)?(async\s+)?(fn|def|function|func|class|struct|enum|trait|interface|type)\s+\w+/;
52
+ const CONST_PATTERN = /^(const|static|let|pub\s+const|pub\s+static)\s+/;
53
+
54
+ export function detectLanguage(filePath: string): Language {
55
+ const lastDot = filePath.lastIndexOf(".");
56
+ if (lastDot === -1) {
57
+ return "unknown";
58
+ }
59
+ const extension = filePath.slice(lastDot).toLowerCase();
60
+ return LANGUAGE_EXTENSIONS[extension] ?? "unknown";
61
+ }
62
+
63
+ export function filterMinimal(content: string, language: Language): string {
64
+ const patterns = COMMENT_PATTERNS[language];
65
+ const lines = content.split("\n");
66
+ const result: string[] = [];
67
+ let inBlockComment = false;
68
+ let inDocstring = false;
69
+ let inUserscriptMetadataBlock = false;
70
+ const userscriptMetadataStartPattern = /^\/\/\s*==\s*userscript\s*==$/i;
71
+ const userscriptMetadataContentPattern = /^\/\/\s*@\w+/;
72
+ const userscriptMetadataEndPattern = /^\/\/\s*==\s*\/userscript\s*==$/i;
73
+
74
+ for (const line of lines) {
75
+ const trimmed = line.trim();
76
+ const isUserscriptMetadataStart = userscriptMetadataStartPattern.test(trimmed);
77
+ const isUserscriptMetadataContent = userscriptMetadataContentPattern.test(trimmed);
78
+ const isUserscriptMetadataEnd = userscriptMetadataEndPattern.test(trimmed);
79
+
80
+ if (isUserscriptMetadataStart) {
81
+ inUserscriptMetadataBlock = true;
82
+ result.push(line);
83
+ continue;
84
+ }
85
+
86
+ if (inUserscriptMetadataBlock) {
87
+ result.push(line);
88
+ if (isUserscriptMetadataEnd) {
89
+ inUserscriptMetadataBlock = false;
90
+ } else if (isUserscriptMetadataContent) {
91
+ // Preserve metadata key/value lines (e.g. // @name) within the userscript block.
92
+ }
93
+ continue;
94
+ }
95
+
96
+ if (patterns.blockStart && patterns.blockEnd) {
97
+ if (
98
+ !inDocstring &&
99
+ trimmed.includes(patterns.blockStart) &&
100
+ !(patterns.docBlockStart && trimmed.startsWith(patterns.docBlockStart))
101
+ ) {
102
+ inBlockComment = true;
103
+ }
104
+
105
+ if (inBlockComment) {
106
+ if (trimmed.includes(patterns.blockEnd)) {
107
+ inBlockComment = false;
108
+ }
109
+ continue;
110
+ }
111
+ }
112
+
113
+ if (language === "python" && trimmed.startsWith('"""')) {
114
+ inDocstring = !inDocstring;
115
+ result.push(line);
116
+ continue;
117
+ }
118
+
119
+ if (inDocstring) {
120
+ result.push(line);
121
+ continue;
122
+ }
123
+
124
+ if (patterns.line && trimmed.startsWith(patterns.line)) {
125
+ if (patterns.docLine && trimmed.startsWith(patterns.docLine)) {
126
+ result.push(line);
127
+ }
128
+ continue;
129
+ }
130
+
131
+ if (trimmed.length === 0) {
132
+ result.push("");
133
+ continue;
134
+ }
135
+
136
+ result.push(line);
137
+ }
138
+
139
+ return result
140
+ .join("\n")
141
+ .replace(/\n{3,}/g, "\n\n")
142
+ .trim();
143
+ }
144
+
145
+ export function filterAggressive(content: string, language: Language): string {
146
+ const minimal = filterMinimal(content, language);
147
+ const lines = minimal.split("\n");
148
+ const result: string[] = [];
149
+ let braceDepth = 0;
150
+ let inImplementation = false;
151
+
152
+ for (const line of lines) {
153
+ const trimmed = line.trim();
154
+
155
+ if (IMPORT_PATTERN.test(trimmed)) {
156
+ result.push(line);
157
+ continue;
158
+ }
159
+
160
+ if (SIGNATURE_PATTERN.test(trimmed)) {
161
+ result.push(line);
162
+ inImplementation = true;
163
+ braceDepth = 0;
164
+ continue;
165
+ }
166
+
167
+ const openBraces = (trimmed.match(/\{/g) ?? []).length;
168
+ const closeBraces = (trimmed.match(/\}/g) ?? []).length;
169
+
170
+ if (inImplementation) {
171
+ braceDepth += openBraces;
172
+ braceDepth -= closeBraces;
173
+
174
+ if (braceDepth <= 1 && (trimmed === "{" || trimmed === "}" || trimmed.endsWith("{"))) {
175
+ result.push(line);
176
+ }
177
+
178
+ if (braceDepth <= 0) {
179
+ inImplementation = false;
180
+ if (trimmed.length > 0 && trimmed !== "}") {
181
+ result.push(" // ... implementation");
182
+ }
183
+ }
184
+ continue;
185
+ }
186
+
187
+ if (CONST_PATTERN.test(trimmed)) {
188
+ result.push(line);
189
+ }
190
+ }
191
+
192
+ return result.join("\n").trim();
193
+ }
194
+
195
+ export function smartTruncate(content: string, maxLines: number, _language: Language): string {
196
+ const lines = content.split("\n");
197
+ if (lines.length <= maxLines) {
198
+ return content;
199
+ }
200
+
201
+ const result: string[] = [];
202
+ let keptLines = 0;
203
+ let skippedSection = false;
204
+
205
+ for (const line of lines) {
206
+ const trimmed = line.trim();
207
+ const isImportant =
208
+ SIGNATURE_PATTERN.test(trimmed) ||
209
+ IMPORT_PATTERN.test(trimmed) ||
210
+ trimmed.startsWith("pub ") ||
211
+ trimmed.startsWith("export ") ||
212
+ trimmed === "}" ||
213
+ trimmed === "{";
214
+
215
+ if (isImportant || keptLines < maxLines / 2) {
216
+ if (skippedSection) {
217
+ result.push(` // ... ${lines.length - keptLines} lines omitted`);
218
+ skippedSection = false;
219
+ }
220
+ result.push(line);
221
+ keptLines += 1;
222
+ } else {
223
+ skippedSection = true;
224
+ }
225
+
226
+ if (keptLines >= maxLines - 1) {
227
+ break;
228
+ }
229
+ }
230
+
231
+ if (skippedSection || keptLines < lines.length) {
232
+ result.push(`// ... ${lines.length - keptLines} more lines (total: ${lines.length})`);
233
+ }
234
+
235
+ return result.join("\n");
236
+ }
237
+
238
+ export function filterSourceCode(
239
+ content: string,
240
+ language: Language,
241
+ level: "none" | "minimal" | "aggressive",
242
+ ): string {
243
+ switch (level) {
244
+ case "none":
245
+ return content;
246
+ case "minimal":
247
+ return filterMinimal(content, language);
248
+ case "aggressive":
249
+ return filterAggressive(content, language);
250
+ default:
251
+ return content;
252
+ }
253
+ }