@pencil-agent/nano-pencil 2.0.0-beta.2 → 2.0.0-beta.4

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 (179) hide show
  1. package/dist/build-meta.json +3 -3
  2. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-continuations.d.ts +17 -0
  3. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-continuations.js +60 -0
  4. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-stream-events.d.ts +19 -0
  5. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-stream-events.js +55 -0
  6. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-tool-results.d.ts +10 -0
  7. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-tool-results.js +137 -0
  8. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-tool-summaries.d.ts +22 -0
  9. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-tool-summaries.js +64 -0
  10. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop.d.ts +26 -0
  11. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop.js +913 -0
  12. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-run-result.d.ts +9 -0
  13. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-run-result.js +32 -0
  14. package/dist/node_modules/@pencil-agent/agent-core/dist/agent.d.ts +215 -0
  15. package/dist/node_modules/@pencil-agent/agent-core/dist/agent.js +522 -0
  16. package/dist/node_modules/@pencil-agent/agent-core/dist/errors.d.ts +62 -0
  17. package/dist/node_modules/@pencil-agent/agent-core/dist/errors.js +146 -0
  18. package/dist/node_modules/@pencil-agent/agent-core/dist/index.d.ts +14 -0
  19. package/dist/node_modules/@pencil-agent/agent-core/dist/index.js +19 -0
  20. package/dist/node_modules/@pencil-agent/agent-core/dist/proxy.d.ts +91 -0
  21. package/dist/node_modules/@pencil-agent/agent-core/dist/proxy.js +279 -0
  22. package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-agent-loop.d.ts +15 -0
  23. package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-agent-loop.js +625 -0
  24. package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-streaming-tool-executor.d.ts +33 -0
  25. package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-streaming-tool-executor.js +189 -0
  26. package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-tool-orchestration.d.ts +35 -0
  27. package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-tool-orchestration.js +319 -0
  28. package/dist/node_modules/@pencil-agent/agent-core/dist/types.d.ts +417 -0
  29. package/dist/node_modules/@pencil-agent/agent-core/dist/types.js +13 -0
  30. package/dist/node_modules/@pencil-agent/agent-core/package.json +28 -0
  31. package/dist/node_modules/@pencil-agent/ai/dist/api-registry.d.ts +27 -0
  32. package/dist/node_modules/@pencil-agent/ai/dist/api-registry.js +152 -0
  33. package/dist/node_modules/@pencil-agent/ai/dist/cli.d.ts +2 -0
  34. package/dist/node_modules/@pencil-agent/ai/dist/cli.js +121 -0
  35. package/dist/node_modules/@pencil-agent/ai/dist/config-path.d.ts +1 -0
  36. package/dist/node_modules/@pencil-agent/ai/dist/config-path.js +17 -0
  37. package/dist/node_modules/@pencil-agent/ai/dist/debug-logger.d.ts +94 -0
  38. package/dist/node_modules/@pencil-agent/ai/dist/debug-logger.js +218 -0
  39. package/dist/node_modules/@pencil-agent/ai/dist/env-api-keys.d.ts +8 -0
  40. package/dist/node_modules/@pencil-agent/ai/dist/env-api-keys.js +107 -0
  41. package/dist/node_modules/@pencil-agent/ai/dist/env.d.ts +7 -0
  42. package/dist/node_modules/@pencil-agent/ai/dist/env.js +7 -0
  43. package/dist/node_modules/@pencil-agent/ai/dist/events.d.ts +8 -0
  44. package/dist/node_modules/@pencil-agent/ai/dist/events.js +7 -0
  45. package/dist/node_modules/@pencil-agent/ai/dist/index.d.ts +27 -0
  46. package/dist/node_modules/@pencil-agent/ai/dist/index.js +20 -0
  47. package/dist/node_modules/@pencil-agent/ai/dist/json.d.ts +7 -0
  48. package/dist/node_modules/@pencil-agent/ai/dist/json.js +7 -0
  49. package/dist/node_modules/@pencil-agent/ai/dist/models.d.ts +31 -0
  50. package/dist/node_modules/@pencil-agent/ai/dist/models.generated.d.ts +15159 -0
  51. package/dist/node_modules/@pencil-agent/ai/dist/models.generated.js +14928 -0
  52. package/dist/node_modules/@pencil-agent/ai/dist/models.js +60 -0
  53. package/dist/node_modules/@pencil-agent/ai/dist/overflow.d.ts +7 -0
  54. package/dist/node_modules/@pencil-agent/ai/dist/overflow.js +7 -0
  55. package/dist/node_modules/@pencil-agent/ai/dist/providers/amazon-bedrock.d.ts +20 -0
  56. package/dist/node_modules/@pencil-agent/ai/dist/providers/amazon-bedrock.js +606 -0
  57. package/dist/node_modules/@pencil-agent/ai/dist/providers/anthropic.d.ts +38 -0
  58. package/dist/node_modules/@pencil-agent/ai/dist/providers/anthropic.js +737 -0
  59. package/dist/node_modules/@pencil-agent/ai/dist/providers/azure-openai-responses.d.ts +21 -0
  60. package/dist/node_modules/@pencil-agent/ai/dist/providers/azure-openai-responses.js +193 -0
  61. package/dist/node_modules/@pencil-agent/ai/dist/providers/github-copilot-headers.d.ts +13 -0
  62. package/dist/node_modules/@pencil-agent/ai/dist/providers/github-copilot-headers.js +34 -0
  63. package/dist/node_modules/@pencil-agent/ai/dist/providers/google-gemini-cli.d.ts +79 -0
  64. package/dist/node_modules/@pencil-agent/ai/dist/providers/google-gemini-cli.js +753 -0
  65. package/dist/node_modules/@pencil-agent/ai/dist/providers/google-shared.d.ts +70 -0
  66. package/dist/node_modules/@pencil-agent/ai/dist/providers/google-shared.js +311 -0
  67. package/dist/node_modules/@pencil-agent/ai/dist/providers/google-vertex.d.ts +20 -0
  68. package/dist/node_modules/@pencil-agent/ai/dist/providers/google-vertex.js +380 -0
  69. package/dist/node_modules/@pencil-agent/ai/dist/providers/google.d.ts +18 -0
  70. package/dist/node_modules/@pencil-agent/ai/dist/providers/google.js +360 -0
  71. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-codex-responses.d.ts +8 -0
  72. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-codex-responses.js +704 -0
  73. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-completions.d.ts +20 -0
  74. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-completions.js +870 -0
  75. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-responses-shared.d.ts +22 -0
  76. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-responses-shared.js +432 -0
  77. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-responses.d.ts +19 -0
  78. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-responses.js +207 -0
  79. package/dist/node_modules/@pencil-agent/ai/dist/providers/register-builtins.d.ts +8 -0
  80. package/dist/node_modules/@pencil-agent/ai/dist/providers/register-builtins.js +86 -0
  81. package/dist/node_modules/@pencil-agent/ai/dist/providers/simple-options.d.ts +13 -0
  82. package/dist/node_modules/@pencil-agent/ai/dist/providers/simple-options.js +40 -0
  83. package/dist/node_modules/@pencil-agent/ai/dist/providers/transform-messages.d.ts +13 -0
  84. package/dist/node_modules/@pencil-agent/ai/dist/providers/transform-messages.js +175 -0
  85. package/dist/node_modules/@pencil-agent/ai/dist/registry.d.ts +8 -0
  86. package/dist/node_modules/@pencil-agent/ai/dist/registry.js +8 -0
  87. package/dist/node_modules/@pencil-agent/ai/dist/schema.d.ts +10 -0
  88. package/dist/node_modules/@pencil-agent/ai/dist/schema.js +9 -0
  89. package/dist/node_modules/@pencil-agent/ai/dist/stream.d.ts +25 -0
  90. package/dist/node_modules/@pencil-agent/ai/dist/stream.js +324 -0
  91. package/dist/node_modules/@pencil-agent/ai/dist/types.d.ts +306 -0
  92. package/dist/node_modules/@pencil-agent/ai/dist/types.js +7 -0
  93. package/dist/node_modules/@pencil-agent/ai/dist/utils/event-stream-types.d.ts +12 -0
  94. package/dist/node_modules/@pencil-agent/ai/dist/utils/event-stream-types.js +7 -0
  95. package/dist/node_modules/@pencil-agent/ai/dist/utils/event-stream.d.ts +31 -0
  96. package/dist/node_modules/@pencil-agent/ai/dist/utils/event-stream.js +98 -0
  97. package/dist/node_modules/@pencil-agent/ai/dist/utils/http-proxy.d.ts +13 -0
  98. package/dist/node_modules/@pencil-agent/ai/dist/utils/http-proxy.js +20 -0
  99. package/dist/node_modules/@pencil-agent/ai/dist/utils/json-parse.d.ts +14 -0
  100. package/dist/node_modules/@pencil-agent/ai/dist/utils/json-parse.js +34 -0
  101. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/anthropic.d.ts +22 -0
  102. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/anthropic.js +109 -0
  103. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/decode-credential.d.ts +12 -0
  104. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/decode-credential.js +25 -0
  105. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/github-copilot.d.ts +35 -0
  106. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/github-copilot.js +286 -0
  107. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/google-antigravity.d.ts +31 -0
  108. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/google-antigravity.js +378 -0
  109. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/google-gemini-cli.d.ts +31 -0
  110. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/google-gemini-cli.js +483 -0
  111. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/index.d.ts +60 -0
  112. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/index.js +131 -0
  113. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/openai-codex.d.ts +39 -0
  114. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/openai-codex.js +385 -0
  115. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/pkce.d.ts +18 -0
  116. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/pkce.js +36 -0
  117. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/types.d.ts +52 -0
  118. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/types.js +7 -0
  119. package/dist/node_modules/@pencil-agent/ai/dist/utils/overflow.d.ts +57 -0
  120. package/dist/node_modules/@pencil-agent/ai/dist/utils/overflow.js +120 -0
  121. package/dist/node_modules/@pencil-agent/ai/dist/utils/sanitize-unicode.d.ts +16 -0
  122. package/dist/node_modules/@pencil-agent/ai/dist/utils/sanitize-unicode.js +20 -0
  123. package/dist/node_modules/@pencil-agent/ai/dist/utils/typebox-helpers.d.ts +22 -0
  124. package/dist/node_modules/@pencil-agent/ai/dist/utils/typebox-helpers.js +26 -0
  125. package/dist/node_modules/@pencil-agent/ai/dist/utils/validation.d.ts +23 -0
  126. package/dist/node_modules/@pencil-agent/ai/dist/utils/validation.js +78 -0
  127. package/dist/node_modules/@pencil-agent/ai/package.json +106 -0
  128. package/dist/node_modules/@pencil-agent/tui/dist/autocomplete.d.ts +62 -0
  129. package/dist/node_modules/@pencil-agent/tui/dist/autocomplete.js +624 -0
  130. package/dist/node_modules/@pencil-agent/tui/dist/components/box.d.ts +27 -0
  131. package/dist/node_modules/@pencil-agent/tui/dist/components/box.js +109 -0
  132. package/dist/node_modules/@pencil-agent/tui/dist/components/cancellable-loader.d.ts +27 -0
  133. package/dist/node_modules/@pencil-agent/tui/dist/components/cancellable-loader.js +40 -0
  134. package/dist/node_modules/@pencil-agent/tui/dist/components/editor.d.ts +218 -0
  135. package/dist/node_modules/@pencil-agent/tui/dist/components/editor.js +1697 -0
  136. package/dist/node_modules/@pencil-agent/tui/dist/components/image.d.ts +33 -0
  137. package/dist/node_modules/@pencil-agent/tui/dist/components/image.js +74 -0
  138. package/dist/node_modules/@pencil-agent/tui/dist/components/input.d.ts +42 -0
  139. package/dist/node_modules/@pencil-agent/tui/dist/components/input.js +438 -0
  140. package/dist/node_modules/@pencil-agent/tui/dist/components/loader.d.ts +26 -0
  141. package/dist/node_modules/@pencil-agent/tui/dist/components/loader.js +54 -0
  142. package/dist/node_modules/@pencil-agent/tui/dist/components/markdown.d.ts +100 -0
  143. package/dist/node_modules/@pencil-agent/tui/dist/components/markdown.js +634 -0
  144. package/dist/node_modules/@pencil-agent/tui/dist/components/select-list.d.ts +37 -0
  145. package/dist/node_modules/@pencil-agent/tui/dist/components/select-list.js +157 -0
  146. package/dist/node_modules/@pencil-agent/tui/dist/components/settings-list.d.ts +55 -0
  147. package/dist/node_modules/@pencil-agent/tui/dist/components/settings-list.js +190 -0
  148. package/dist/node_modules/@pencil-agent/tui/dist/components/spacer.d.ts +17 -0
  149. package/dist/node_modules/@pencil-agent/tui/dist/components/spacer.js +28 -0
  150. package/dist/node_modules/@pencil-agent/tui/dist/components/text.d.ts +24 -0
  151. package/dist/node_modules/@pencil-agent/tui/dist/components/text.js +94 -0
  152. package/dist/node_modules/@pencil-agent/tui/dist/components/truncated-text.d.ts +18 -0
  153. package/dist/node_modules/@pencil-agent/tui/dist/components/truncated-text.js +56 -0
  154. package/dist/node_modules/@pencil-agent/tui/dist/editor-component.d.ts +51 -0
  155. package/dist/node_modules/@pencil-agent/tui/dist/editor-component.js +7 -0
  156. package/dist/node_modules/@pencil-agent/tui/dist/fuzzy.d.ts +32 -0
  157. package/dist/node_modules/@pencil-agent/tui/dist/fuzzy.js +152 -0
  158. package/dist/node_modules/@pencil-agent/tui/dist/index.d.ts +28 -0
  159. package/dist/node_modules/@pencil-agent/tui/dist/index.js +37 -0
  160. package/dist/node_modules/@pencil-agent/tui/dist/keybindings.d.ts +44 -0
  161. package/dist/node_modules/@pencil-agent/tui/dist/keybindings.js +119 -0
  162. package/dist/node_modules/@pencil-agent/tui/dist/keys.d.ts +149 -0
  163. package/dist/node_modules/@pencil-agent/tui/dist/keys.js +948 -0
  164. package/dist/node_modules/@pencil-agent/tui/dist/kill-ring.d.ts +33 -0
  165. package/dist/node_modules/@pencil-agent/tui/dist/kill-ring.js +49 -0
  166. package/dist/node_modules/@pencil-agent/tui/dist/stdin-buffer.d.ts +38 -0
  167. package/dist/node_modules/@pencil-agent/tui/dist/stdin-buffer.js +307 -0
  168. package/dist/node_modules/@pencil-agent/tui/dist/terminal-image.d.ts +73 -0
  169. package/dist/node_modules/@pencil-agent/tui/dist/terminal-image.js +287 -0
  170. package/dist/node_modules/@pencil-agent/tui/dist/terminal.d.ts +86 -0
  171. package/dist/node_modules/@pencil-agent/tui/dist/terminal.js +266 -0
  172. package/dist/node_modules/@pencil-agent/tui/dist/tui.d.ts +219 -0
  173. package/dist/node_modules/@pencil-agent/tui/dist/tui.js +1001 -0
  174. package/dist/node_modules/@pencil-agent/tui/dist/undo-stack.d.ts +22 -0
  175. package/dist/node_modules/@pencil-agent/tui/dist/undo-stack.js +30 -0
  176. package/dist/node_modules/@pencil-agent/tui/dist/utils.d.ts +83 -0
  177. package/dist/node_modules/@pencil-agent/tui/dist/utils.js +811 -0
  178. package/dist/node_modules/@pencil-agent/tui/package.json +37 -0
  179. package/package.json +3 -2
@@ -0,0 +1,624 @@
1
+ /**
2
+ * [WHO]: AutocompleteItem, SlashCommand, AutocompleteProvider, CombinedAutocompleteProvider
3
+ * [FROM]: Depends on child_process, fs, os, path, ./fuzzy.js
4
+ * [TO]: Consumed by core/lib/tui/src/index.ts
5
+ * [HERE]: core/lib/tui/src/autocomplete.ts -
6
+ */
7
+ import { spawnSync } from "child_process";
8
+ import { readdirSync, statSync } from "fs";
9
+ import { homedir } from "os";
10
+ import { basename, dirname, join } from "path";
11
+ import { weightedFuzzyFilter } from "./fuzzy.js";
12
+ const PATH_DELIMITERS = new Set([" ", "\t", '"', "'", "="]);
13
+ function findLastDelimiter(text) {
14
+ for (let i = text.length - 1; i >= 0; i -= 1) {
15
+ if (PATH_DELIMITERS.has(text[i] ?? "")) {
16
+ return i;
17
+ }
18
+ }
19
+ return -1;
20
+ }
21
+ function findUnclosedQuoteStart(text) {
22
+ let inQuotes = false;
23
+ let quoteStart = -1;
24
+ for (let i = 0; i < text.length; i += 1) {
25
+ if (text[i] === '"') {
26
+ inQuotes = !inQuotes;
27
+ if (inQuotes) {
28
+ quoteStart = i;
29
+ }
30
+ }
31
+ }
32
+ return inQuotes ? quoteStart : null;
33
+ }
34
+ function isTokenStart(text, index) {
35
+ return index === 0 || PATH_DELIMITERS.has(text[index - 1] ?? "");
36
+ }
37
+ function extractQuotedPrefix(text) {
38
+ const quoteStart = findUnclosedQuoteStart(text);
39
+ if (quoteStart === null) {
40
+ return null;
41
+ }
42
+ if (quoteStart > 0 && text[quoteStart - 1] === "@") {
43
+ if (!isTokenStart(text, quoteStart - 1)) {
44
+ return null;
45
+ }
46
+ return text.slice(quoteStart - 1);
47
+ }
48
+ if (!isTokenStart(text, quoteStart)) {
49
+ return null;
50
+ }
51
+ return text.slice(quoteStart);
52
+ }
53
+ function parsePathPrefix(prefix) {
54
+ if (prefix.startsWith('@"')) {
55
+ return { rawPrefix: prefix.slice(2), isAtPrefix: true, isQuotedPrefix: true };
56
+ }
57
+ if (prefix.startsWith('"')) {
58
+ return { rawPrefix: prefix.slice(1), isAtPrefix: false, isQuotedPrefix: true };
59
+ }
60
+ if (prefix.startsWith("@")) {
61
+ return { rawPrefix: prefix.slice(1), isAtPrefix: true, isQuotedPrefix: false };
62
+ }
63
+ return { rawPrefix: prefix, isAtPrefix: false, isQuotedPrefix: false };
64
+ }
65
+ function getCurrentArgumentPrefix(argumentText) {
66
+ const match = argumentText.match(/(?:^|\s)(\S*)$/);
67
+ return match?.[1] ?? argumentText;
68
+ }
69
+ function getArgumentCompletionContext(commandName, argumentText, argumentPrefix) {
70
+ const beforePrefix = argumentText.slice(0, argumentText.length - argumentPrefix.length);
71
+ const previousTokens = beforePrefix.trim() ? beforePrefix.trim().split(/\s+/) : [];
72
+ return {
73
+ commandName,
74
+ argumentText,
75
+ argumentPrefix,
76
+ tokenIndex: previousTokens.length,
77
+ previousTokens,
78
+ };
79
+ }
80
+ function buildCompletionValue(path, options) {
81
+ const needsQuotes = options.isQuotedPrefix || path.includes(" ");
82
+ const prefix = options.isAtPrefix ? "@" : "";
83
+ if (!needsQuotes) {
84
+ return `${prefix}${path}`;
85
+ }
86
+ const openQuote = `${prefix}"`;
87
+ const closeQuote = '"';
88
+ return `${openQuote}${path}${closeQuote}`;
89
+ }
90
+ // Use fd to walk directory tree (fast, respects .gitignore)
91
+ function walkDirectoryWithFd(baseDir, fdPath, query, maxResults) {
92
+ const args = [
93
+ "--base-directory",
94
+ baseDir,
95
+ "--max-results",
96
+ String(maxResults),
97
+ "--type",
98
+ "f",
99
+ "--type",
100
+ "d",
101
+ "--full-path",
102
+ "--hidden",
103
+ "--exclude",
104
+ ".git",
105
+ "--exclude",
106
+ ".git/*",
107
+ "--exclude",
108
+ ".git/**",
109
+ ];
110
+ // Add query as pattern if provided
111
+ if (query) {
112
+ args.push(query);
113
+ }
114
+ const result = spawnSync(fdPath, args, {
115
+ encoding: "utf-8",
116
+ stdio: ["pipe", "pipe", "pipe"],
117
+ maxBuffer: 10 * 1024 * 1024,
118
+ });
119
+ if (result.status !== 0 || !result.stdout) {
120
+ return [];
121
+ }
122
+ const lines = result.stdout.trim().split("\n").filter(Boolean);
123
+ const results = [];
124
+ for (const line of lines) {
125
+ const normalizedPath = line.endsWith("/") ? line.slice(0, -1) : line;
126
+ if (normalizedPath === ".git" || normalizedPath.startsWith(".git/") || normalizedPath.includes("/.git/")) {
127
+ continue;
128
+ }
129
+ // fd outputs directories with trailing /
130
+ const isDirectory = line.endsWith("/");
131
+ results.push({
132
+ path: line,
133
+ isDirectory,
134
+ });
135
+ }
136
+ return results;
137
+ }
138
+ // Combined provider that handles both slash commands and file paths
139
+ export class CombinedAutocompleteProvider {
140
+ commands;
141
+ basePath;
142
+ fdPath;
143
+ constructor(commands = [], basePath = process.cwd(), fdPath = null) {
144
+ this.commands = commands;
145
+ this.basePath = basePath;
146
+ this.fdPath = fdPath;
147
+ }
148
+ getSuggestions(lines, cursorLine, cursorCol) {
149
+ const currentLine = lines[cursorLine] || "";
150
+ const textBeforeCursor = currentLine.slice(0, cursorCol);
151
+ // Check for @ file reference (fuzzy search) - must be after a delimiter or at start
152
+ const atPrefix = this.extractAtPrefix(textBeforeCursor);
153
+ if (atPrefix) {
154
+ const { rawPrefix, isQuotedPrefix } = parsePathPrefix(atPrefix);
155
+ const suggestions = this.getFuzzyFileSuggestions(rawPrefix, { isQuotedPrefix: isQuotedPrefix });
156
+ if (suggestions.length === 0)
157
+ return null;
158
+ return {
159
+ items: suggestions,
160
+ prefix: atPrefix,
161
+ };
162
+ }
163
+ // Check for slash commands
164
+ if (textBeforeCursor.startsWith("/")) {
165
+ const spaceIndex = textBeforeCursor.indexOf(" ");
166
+ if (spaceIndex === -1) {
167
+ // No space yet - complete command names with weighted fuzzy matching
168
+ const prefix = textBeforeCursor.slice(1); // Remove the "/"
169
+ const commandItems = this.commands.map((cmd) => ({
170
+ name: "name" in cmd ? cmd.name : cmd.value,
171
+ label: "name" in cmd ? cmd.name : cmd.label,
172
+ description: cmd.description,
173
+ }));
174
+ const filtered = weightedFuzzyFilter(commandItems, prefix, [
175
+ { name: "name", getText: (i) => i.name, weight: 3 },
176
+ ...(prefix.length > 0
177
+ ? []
178
+ : [{ name: "description", getText: (i) => i.description ?? "", weight: 0.5 }]),
179
+ ])
180
+ .slice(0, 10)
181
+ .map((item) => ({
182
+ value: item.name,
183
+ label: item.label,
184
+ ...(item.description && { description: item.description }),
185
+ }));
186
+ if (filtered.length === 0)
187
+ return null;
188
+ return {
189
+ items: filtered,
190
+ prefix: textBeforeCursor,
191
+ };
192
+ }
193
+ else {
194
+ // Space found - complete command arguments
195
+ const commandName = textBeforeCursor.slice(1, spaceIndex); // Command without "/"
196
+ const argumentText = textBeforeCursor.slice(spaceIndex + 1); // Text after space
197
+ const argumentPrefix = getCurrentArgumentPrefix(argumentText);
198
+ const command = this.commands.find((cmd) => {
199
+ const name = "name" in cmd ? cmd.name : cmd.value;
200
+ return name === commandName;
201
+ });
202
+ if (!command || !("getArgumentCompletions" in command) || !command.getArgumentCompletions) {
203
+ return null; // No argument completion for this command
204
+ }
205
+ const argumentSuggestions = command.getArgumentCompletions(argumentPrefix, getArgumentCompletionContext(commandName, argumentText, argumentPrefix));
206
+ if (!argumentSuggestions || argumentSuggestions.length === 0) {
207
+ return null;
208
+ }
209
+ return {
210
+ items: argumentSuggestions,
211
+ prefix: argumentPrefix,
212
+ };
213
+ }
214
+ }
215
+ // Check for file paths - triggered by Tab or if we detect a path pattern
216
+ const pathMatch = this.extractPathPrefix(textBeforeCursor, false);
217
+ if (pathMatch !== null) {
218
+ const suggestions = this.getFileSuggestions(pathMatch);
219
+ if (suggestions.length === 0)
220
+ return null;
221
+ // Check if we have an exact match that is a directory
222
+ // In that case, we might want to return suggestions for the directory content instead
223
+ // But only if the prefix ends with /
224
+ if (suggestions.length === 1 && suggestions[0]?.value === pathMatch && !pathMatch.endsWith("/")) {
225
+ // Exact match found (e.g. user typed "src" and "src/" is the only match)
226
+ // We still return it so user can select it and add /
227
+ return {
228
+ items: suggestions,
229
+ prefix: pathMatch,
230
+ };
231
+ }
232
+ return {
233
+ items: suggestions,
234
+ prefix: pathMatch,
235
+ };
236
+ }
237
+ return null;
238
+ }
239
+ applyCompletion(lines, cursorLine, cursorCol, item, prefix) {
240
+ const currentLine = lines[cursorLine] || "";
241
+ const beforePrefix = currentLine.slice(0, cursorCol - prefix.length);
242
+ const afterCursor = currentLine.slice(cursorCol);
243
+ const isQuotedPrefix = prefix.startsWith('"') || prefix.startsWith('@"');
244
+ const hasLeadingQuoteAfterCursor = afterCursor.startsWith('"');
245
+ const hasTrailingQuoteInItem = item.value.endsWith('"');
246
+ const adjustedAfterCursor = isQuotedPrefix && hasTrailingQuoteInItem && hasLeadingQuoteAfterCursor ? afterCursor.slice(1) : afterCursor;
247
+ // Check if we're completing a slash command (prefix starts with "/" but NOT a file path)
248
+ // Slash commands are at the start of the line and don't contain path separators after the first /
249
+ const isSlashCommand = prefix.startsWith("/") && beforePrefix.trim() === "" && !prefix.slice(1).includes("/");
250
+ if (isSlashCommand) {
251
+ // This is a command name completion
252
+ const newLine = `${beforePrefix}/${item.value} ${adjustedAfterCursor}`;
253
+ const newLines = [...lines];
254
+ newLines[cursorLine] = newLine;
255
+ return {
256
+ lines: newLines,
257
+ cursorLine,
258
+ cursorCol: beforePrefix.length + item.value.length + 2, // +2 for "/" and space
259
+ };
260
+ }
261
+ // Check if we're completing a file attachment (prefix starts with "@")
262
+ if (prefix.startsWith("@")) {
263
+ // This is a file attachment completion
264
+ // Don't add space after directories so user can continue autocompleting
265
+ const isDirectory = item.label.endsWith("/");
266
+ const suffix = isDirectory ? "" : " ";
267
+ const newLine = `${beforePrefix + item.value}${suffix}${adjustedAfterCursor}`;
268
+ const newLines = [...lines];
269
+ newLines[cursorLine] = newLine;
270
+ const hasTrailingQuote = item.value.endsWith('"');
271
+ const cursorOffset = isDirectory && hasTrailingQuote ? item.value.length - 1 : item.value.length;
272
+ return {
273
+ lines: newLines,
274
+ cursorLine,
275
+ cursorCol: beforePrefix.length + cursorOffset + suffix.length,
276
+ };
277
+ }
278
+ // Check if we're in a slash command context (beforePrefix contains "/command ")
279
+ const textBeforeCursor = currentLine.slice(0, cursorCol);
280
+ if (textBeforeCursor.includes("/") && textBeforeCursor.includes(" ")) {
281
+ // This is likely a command argument completion
282
+ const newLine = beforePrefix + item.value + adjustedAfterCursor;
283
+ const newLines = [...lines];
284
+ newLines[cursorLine] = newLine;
285
+ const isDirectory = item.label.endsWith("/");
286
+ const hasTrailingQuote = item.value.endsWith('"');
287
+ const cursorOffset = isDirectory && hasTrailingQuote ? item.value.length - 1 : item.value.length;
288
+ return {
289
+ lines: newLines,
290
+ cursorLine,
291
+ cursorCol: beforePrefix.length + cursorOffset,
292
+ };
293
+ }
294
+ // For file paths, complete the path
295
+ const newLine = beforePrefix + item.value + adjustedAfterCursor;
296
+ const newLines = [...lines];
297
+ newLines[cursorLine] = newLine;
298
+ const isDirectory = item.label.endsWith("/");
299
+ const hasTrailingQuote = item.value.endsWith('"');
300
+ const cursorOffset = isDirectory && hasTrailingQuote ? item.value.length - 1 : item.value.length;
301
+ return {
302
+ lines: newLines,
303
+ cursorLine,
304
+ cursorCol: beforePrefix.length + cursorOffset,
305
+ };
306
+ }
307
+ // Extract @ prefix for fuzzy file suggestions
308
+ extractAtPrefix(text) {
309
+ const quotedPrefix = extractQuotedPrefix(text);
310
+ if (quotedPrefix?.startsWith('@"')) {
311
+ return quotedPrefix;
312
+ }
313
+ const lastDelimiterIndex = findLastDelimiter(text);
314
+ const tokenStart = lastDelimiterIndex === -1 ? 0 : lastDelimiterIndex + 1;
315
+ if (text[tokenStart] === "@") {
316
+ return text.slice(tokenStart);
317
+ }
318
+ return null;
319
+ }
320
+ // Extract a path-like prefix from the text before cursor
321
+ extractPathPrefix(text, forceExtract = false) {
322
+ const quotedPrefix = extractQuotedPrefix(text);
323
+ if (quotedPrefix) {
324
+ return quotedPrefix;
325
+ }
326
+ const lastDelimiterIndex = findLastDelimiter(text);
327
+ const pathPrefix = lastDelimiterIndex === -1 ? text : text.slice(lastDelimiterIndex + 1);
328
+ // For forced extraction (Tab key), always return something
329
+ if (forceExtract) {
330
+ return pathPrefix;
331
+ }
332
+ // For natural triggers, return if it looks like a path, ends with /, starts with ~/, .
333
+ // Only return empty string if the text looks like it's starting a path context
334
+ if (pathPrefix.includes("/") || pathPrefix.startsWith(".") || pathPrefix.startsWith("~/")) {
335
+ return pathPrefix;
336
+ }
337
+ // Return empty string only after a space (not for completely empty text)
338
+ // Empty text should not trigger file suggestions - that's for forced Tab completion
339
+ if (pathPrefix === "" && text.endsWith(" ")) {
340
+ return pathPrefix;
341
+ }
342
+ return null;
343
+ }
344
+ // Expand home directory (~/) to actual home path
345
+ expandHomePath(path) {
346
+ if (path.startsWith("~/")) {
347
+ const expandedPath = join(homedir(), path.slice(2));
348
+ // Preserve trailing slash if original path had one
349
+ return path.endsWith("/") && !expandedPath.endsWith("/") ? `${expandedPath}/` : expandedPath;
350
+ }
351
+ else if (path === "~") {
352
+ return homedir();
353
+ }
354
+ return path;
355
+ }
356
+ resolveScopedFuzzyQuery(rawQuery) {
357
+ const slashIndex = rawQuery.lastIndexOf("/");
358
+ if (slashIndex === -1) {
359
+ return null;
360
+ }
361
+ const displayBase = rawQuery.slice(0, slashIndex + 1);
362
+ const query = rawQuery.slice(slashIndex + 1);
363
+ let baseDir;
364
+ if (displayBase.startsWith("~/")) {
365
+ baseDir = this.expandHomePath(displayBase);
366
+ }
367
+ else if (displayBase.startsWith("/")) {
368
+ baseDir = displayBase;
369
+ }
370
+ else {
371
+ baseDir = join(this.basePath, displayBase);
372
+ }
373
+ try {
374
+ if (!statSync(baseDir).isDirectory()) {
375
+ return null;
376
+ }
377
+ }
378
+ catch {
379
+ return null;
380
+ }
381
+ return { baseDir, query, displayBase };
382
+ }
383
+ scopedPathForDisplay(displayBase, relativePath) {
384
+ if (displayBase === "/") {
385
+ return `/${relativePath}`;
386
+ }
387
+ return `${displayBase}${relativePath}`;
388
+ }
389
+ // Get file/directory suggestions for a given path prefix
390
+ getFileSuggestions(prefix) {
391
+ try {
392
+ let searchDir;
393
+ let searchPrefix;
394
+ const { rawPrefix, isAtPrefix, isQuotedPrefix } = parsePathPrefix(prefix);
395
+ let expandedPrefix = rawPrefix;
396
+ // Handle home directory expansion
397
+ if (expandedPrefix.startsWith("~")) {
398
+ expandedPrefix = this.expandHomePath(expandedPrefix);
399
+ }
400
+ const isRootPrefix = rawPrefix === "" ||
401
+ rawPrefix === "./" ||
402
+ rawPrefix === "../" ||
403
+ rawPrefix === "~" ||
404
+ rawPrefix === "~/" ||
405
+ rawPrefix === "/" ||
406
+ (isAtPrefix && rawPrefix === "");
407
+ if (isRootPrefix) {
408
+ // Complete from specified position
409
+ if (rawPrefix.startsWith("~") || expandedPrefix.startsWith("/")) {
410
+ searchDir = expandedPrefix;
411
+ }
412
+ else {
413
+ searchDir = join(this.basePath, expandedPrefix);
414
+ }
415
+ searchPrefix = "";
416
+ }
417
+ else if (rawPrefix.endsWith("/")) {
418
+ // If prefix ends with /, show contents of that directory
419
+ if (rawPrefix.startsWith("~") || expandedPrefix.startsWith("/")) {
420
+ searchDir = expandedPrefix;
421
+ }
422
+ else {
423
+ searchDir = join(this.basePath, expandedPrefix);
424
+ }
425
+ searchPrefix = "";
426
+ }
427
+ else {
428
+ // Split into directory and file prefix
429
+ const dir = dirname(expandedPrefix);
430
+ const file = basename(expandedPrefix);
431
+ if (rawPrefix.startsWith("~") || expandedPrefix.startsWith("/")) {
432
+ searchDir = dir;
433
+ }
434
+ else {
435
+ searchDir = join(this.basePath, dir);
436
+ }
437
+ searchPrefix = file;
438
+ }
439
+ const entries = readdirSync(searchDir, { withFileTypes: true });
440
+ const suggestions = [];
441
+ for (const entry of entries) {
442
+ if (!entry.name.toLowerCase().startsWith(searchPrefix.toLowerCase())) {
443
+ continue;
444
+ }
445
+ // Check if entry is a directory (or a symlink pointing to a directory)
446
+ let isDirectory = entry.isDirectory();
447
+ if (!isDirectory && entry.isSymbolicLink()) {
448
+ try {
449
+ const fullPath = join(searchDir, entry.name);
450
+ isDirectory = statSync(fullPath).isDirectory();
451
+ }
452
+ catch {
453
+ // Broken symlink or permission error - treat as file
454
+ }
455
+ }
456
+ let relativePath;
457
+ const name = entry.name;
458
+ const displayPrefix = rawPrefix;
459
+ if (displayPrefix.endsWith("/")) {
460
+ // If prefix ends with /, append entry to the prefix
461
+ relativePath = displayPrefix + name;
462
+ }
463
+ else if (displayPrefix.includes("/")) {
464
+ // Preserve ~/ format for home directory paths
465
+ if (displayPrefix.startsWith("~/")) {
466
+ const homeRelativeDir = displayPrefix.slice(2); // Remove ~/
467
+ const dir = dirname(homeRelativeDir);
468
+ relativePath = `~/${dir === "." ? name : join(dir, name)}`;
469
+ }
470
+ else if (displayPrefix.startsWith("/")) {
471
+ // Absolute path - construct properly
472
+ const dir = dirname(displayPrefix);
473
+ if (dir === "/") {
474
+ relativePath = `/${name}`;
475
+ }
476
+ else {
477
+ relativePath = `${dir}/${name}`;
478
+ }
479
+ }
480
+ else {
481
+ relativePath = join(dirname(displayPrefix), name);
482
+ }
483
+ }
484
+ else {
485
+ // For standalone entries, preserve ~/ if original prefix was ~/
486
+ if (displayPrefix.startsWith("~")) {
487
+ relativePath = `~/${name}`;
488
+ }
489
+ else {
490
+ relativePath = name;
491
+ }
492
+ }
493
+ const pathValue = isDirectory ? `${relativePath}/` : relativePath;
494
+ const value = buildCompletionValue(pathValue, {
495
+ isDirectory,
496
+ isAtPrefix,
497
+ isQuotedPrefix,
498
+ });
499
+ suggestions.push({
500
+ value,
501
+ label: name + (isDirectory ? "/" : ""),
502
+ });
503
+ }
504
+ // Sort directories first, then alphabetically
505
+ suggestions.sort((a, b) => {
506
+ const aIsDir = a.value.endsWith("/");
507
+ const bIsDir = b.value.endsWith("/");
508
+ if (aIsDir && !bIsDir)
509
+ return -1;
510
+ if (!aIsDir && bIsDir)
511
+ return 1;
512
+ return a.label.localeCompare(b.label);
513
+ });
514
+ return suggestions;
515
+ }
516
+ catch (_e) {
517
+ // Directory doesn't exist or not accessible
518
+ return [];
519
+ }
520
+ }
521
+ // Score an entry against the query (higher = better match)
522
+ // isDirectory adds bonus to prioritize folders
523
+ scoreEntry(filePath, query, isDirectory) {
524
+ const fileName = basename(filePath);
525
+ const lowerFileName = fileName.toLowerCase();
526
+ const lowerQuery = query.toLowerCase();
527
+ let score = 0;
528
+ // Exact filename match (highest)
529
+ if (lowerFileName === lowerQuery)
530
+ score = 100;
531
+ // Filename starts with query
532
+ else if (lowerFileName.startsWith(lowerQuery))
533
+ score = 80;
534
+ // Substring match in filename
535
+ else if (lowerFileName.includes(lowerQuery))
536
+ score = 50;
537
+ // Substring match in full path
538
+ else if (filePath.toLowerCase().includes(lowerQuery))
539
+ score = 30;
540
+ // Directories get a bonus to appear first
541
+ if (isDirectory && score > 0)
542
+ score += 10;
543
+ return score;
544
+ }
545
+ // Fuzzy file search using fd (fast, respects .gitignore)
546
+ getFuzzyFileSuggestions(query, options) {
547
+ if (!this.fdPath) {
548
+ // fd not available, return empty results
549
+ return [];
550
+ }
551
+ try {
552
+ const scopedQuery = this.resolveScopedFuzzyQuery(query);
553
+ const fdBaseDir = scopedQuery?.baseDir ?? this.basePath;
554
+ const fdQuery = scopedQuery?.query ?? query;
555
+ const entries = walkDirectoryWithFd(fdBaseDir, this.fdPath, fdQuery, 100);
556
+ // Score entries
557
+ const scoredEntries = entries
558
+ .map((entry) => ({
559
+ ...entry,
560
+ score: fdQuery ? this.scoreEntry(entry.path, fdQuery, entry.isDirectory) : 1,
561
+ }))
562
+ .filter((entry) => entry.score > 0);
563
+ // Sort by score (descending) and take top 20
564
+ scoredEntries.sort((a, b) => b.score - a.score);
565
+ const topEntries = scoredEntries.slice(0, 20);
566
+ // Build suggestions
567
+ const suggestions = [];
568
+ for (const { path: entryPath, isDirectory } of topEntries) {
569
+ // fd already includes trailing / for directories
570
+ const pathWithoutSlash = isDirectory ? entryPath.slice(0, -1) : entryPath;
571
+ const displayPath = scopedQuery
572
+ ? this.scopedPathForDisplay(scopedQuery.displayBase, pathWithoutSlash)
573
+ : pathWithoutSlash;
574
+ const entryName = basename(pathWithoutSlash);
575
+ const completionPath = isDirectory ? `${displayPath}/` : displayPath;
576
+ const value = buildCompletionValue(completionPath, {
577
+ isDirectory,
578
+ isAtPrefix: true,
579
+ isQuotedPrefix: options.isQuotedPrefix,
580
+ });
581
+ suggestions.push({
582
+ value,
583
+ label: entryName + (isDirectory ? "/" : ""),
584
+ description: displayPath,
585
+ });
586
+ }
587
+ return suggestions;
588
+ }
589
+ catch {
590
+ return [];
591
+ }
592
+ }
593
+ // Force file completion (called on Tab key) - always returns suggestions
594
+ getForceFileSuggestions(lines, cursorLine, cursorCol) {
595
+ const currentLine = lines[cursorLine] || "";
596
+ const textBeforeCursor = currentLine.slice(0, cursorCol);
597
+ // Don't trigger if we're typing a slash command at the start of the line
598
+ if (textBeforeCursor.trim().startsWith("/") && !textBeforeCursor.trim().includes(" ")) {
599
+ return null;
600
+ }
601
+ // Force extract path prefix - this will always return something
602
+ const pathMatch = this.extractPathPrefix(textBeforeCursor, true);
603
+ if (pathMatch !== null) {
604
+ const suggestions = this.getFileSuggestions(pathMatch);
605
+ if (suggestions.length === 0)
606
+ return null;
607
+ return {
608
+ items: suggestions,
609
+ prefix: pathMatch,
610
+ };
611
+ }
612
+ return null;
613
+ }
614
+ // Check if we should trigger file completion (called on Tab key)
615
+ shouldTriggerFileCompletion(lines, cursorLine, cursorCol) {
616
+ const currentLine = lines[cursorLine] || "";
617
+ const textBeforeCursor = currentLine.slice(0, cursorCol);
618
+ // Don't trigger if we're typing a slash command at the start of the line
619
+ if (textBeforeCursor.trim().startsWith("/") && !textBeforeCursor.trim().includes(" ")) {
620
+ return false;
621
+ }
622
+ return true;
623
+ }
624
+ }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * [WHO]: Box
3
+ * [FROM]: Depends on ../utils.js
4
+ * [TO]: Consumed by core/lib/tui/src/index.ts
5
+ * [HERE]: core/lib/tui/src/components/box.ts -
6
+ */
7
+ import type { Component } from "../tui.js";
8
+ /**
9
+ * Box component - a container that applies padding and background to all children
10
+ */
11
+ export declare class Box implements Component {
12
+ children: Component[];
13
+ private paddingX;
14
+ private paddingY;
15
+ private bgFn?;
16
+ private cache?;
17
+ constructor(paddingX?: number, paddingY?: number, bgFn?: (text: string) => string);
18
+ addChild(component: Component): void;
19
+ removeChild(component: Component): void;
20
+ clear(): void;
21
+ setBgFn(bgFn?: (text: string) => string): void;
22
+ private invalidateCache;
23
+ private matchCache;
24
+ invalidate(): void;
25
+ render(width: number): string[];
26
+ private applyBg;
27
+ }