@duckmind/dm-windows-x64 0.63.0 → 0.63.6
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/dm.exe +0 -0
- package/extensions/.dm-extensions.json +200 -4
- package/extensions/dm-context/package.json +1 -1
- package/extensions/dm-context/skills/context-management/SKILL.md +173 -223
- package/extensions/dm-context/skills/context-management/references/development-and-troubleshooting.md +75 -0
- package/extensions/dm-context/skills/context-management/references/interleaved-async-work.md +143 -0
- package/extensions/dm-context/skills/context-management/references/planning-and-execution.md +81 -0
- package/extensions/dm-context/skills/context-management/references/repeated-items-and-batch-work.md +69 -0
- package/extensions/dm-context/skills/context-management/references/retry-branch-and-pivot.md +80 -0
- package/extensions/dm-context/skills/context-management/references/search-research-and-reading.md +103 -0
- package/extensions/dm-context/skills/context-management/references/task-switching-and-cleanup.md +73 -0
- package/extensions/dm-context/src/context.js +3 -2
- package/extensions/dm-context/src/index.js +168 -84
- package/extensions/dm-skills-manager/THIRD_PARTY_NOTICES.md +27 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/components.js +265 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/constants.js +31 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/creation-fallback.js +20 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/creation.js +145 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/dialog.js +738 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/dm-ai-compat.js +15 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/format.js +125 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/glyphs.js +142 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/layout.js +93 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/paths.js +76 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/registry.js +95 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/settings.js +93 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/startup.js +32 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/toggle.js +54 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/types.js +14 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/ui.js +121 -0
- package/extensions/dm-skills-manager/extensions/skills-manager.js +111 -0
- package/extensions/dm-skills-manager/package.json +121 -0
- package/extensions/dm-web-access/SECURITY.md +5 -0
- package/extensions/dm-web-access/activity.js +65 -0
- package/extensions/dm-web-access/anysearch.js +158 -0
- package/extensions/dm-web-access/auth-fetch.js +131 -0
- package/extensions/dm-web-access/bocha.js +214 -0
- package/extensions/dm-web-access/brave.js +196 -0
- package/extensions/dm-web-access/brightdata-unlocker.js +202 -0
- package/extensions/dm-web-access/brightdata.js +334 -0
- package/extensions/dm-web-access/chrome-cookies.js +627 -0
- package/extensions/dm-web-access/content-find.js +114 -0
- package/extensions/dm-web-access/credential-source.js +150 -0
- package/extensions/dm-web-access/curator-page.js +3559 -0
- package/extensions/dm-web-access/curator-server.js +691 -0
- package/extensions/dm-web-access/data-uri-sanitize.js +312 -0
- package/extensions/dm-web-access/datalab-pdf-extract.js +346 -0
- package/extensions/dm-web-access/declared-web-links.js +167 -0
- package/extensions/dm-web-access/dm-web-fetch-demo.mp4 +0 -0
- package/extensions/dm-web-access/duckduckgo.js +118 -0
- package/extensions/dm-web-access/exa.js +401 -0
- package/extensions/dm-web-access/extract.js +1220 -0
- package/extensions/dm-web-access/feature-config.js +24 -0
- package/extensions/dm-web-access/fetch-params.js +81 -0
- package/extensions/dm-web-access/firecrawl.js +378 -0
- package/extensions/dm-web-access/gemini-adc.js +241 -0
- package/extensions/dm-web-access/gemini-api.js +258 -0
- package/extensions/dm-web-access/gemini-pdf-extract.js +74 -0
- package/extensions/dm-web-access/gemini-search.js +889 -0
- package/extensions/dm-web-access/gemini-url-context.js +97 -0
- package/extensions/dm-web-access/gemini-web-config.js +84 -0
- package/extensions/dm-web-access/gemini-web.js +351 -0
- package/extensions/dm-web-access/github-api.js +166 -0
- package/extensions/dm-web-access/github-extract.js +991 -0
- package/extensions/dm-web-access/github-issue-pr.js +750 -0
- package/extensions/dm-web-access/index.js +3117 -0
- package/extensions/dm-web-access/jina-search.js +242 -0
- package/extensions/dm-web-access/kagi.js +255 -0
- package/extensions/dm-web-access/kimi-search.js +214 -0
- package/extensions/dm-web-access/ollama.js +209 -0
- package/extensions/dm-web-access/openai-search.js +510 -0
- package/extensions/dm-web-access/package.json +34 -0
- package/extensions/dm-web-access/page-query.js +124 -0
- package/extensions/dm-web-access/parallel-mcp.js +223 -0
- package/extensions/dm-web-access/parallel.js +345 -0
- package/extensions/dm-web-access/pdf-extract.js +257 -0
- package/extensions/dm-web-access/perplexity.js +151 -0
- package/extensions/dm-web-access/querit.js +327 -0
- package/extensions/dm-web-access/query-rewrite.js +40 -0
- package/extensions/dm-web-access/render-search-error.js +80 -0
- package/extensions/dm-web-access/rsc-extract.js +347 -0
- package/extensions/dm-web-access/search1api.js +245 -0
- package/extensions/dm-web-access/searchinfinity.js +221 -0
- package/extensions/dm-web-access/searxng.js +223 -0
- package/extensions/dm-web-access/serpbase.js +205 -0
- package/extensions/dm-web-access/serpdive.js +238 -0
- package/extensions/dm-web-access/serper.js +200 -0
- package/extensions/dm-web-access/source-check.js +198 -0
- package/extensions/dm-web-access/ssrf-protection.js +436 -0
- package/extensions/dm-web-access/storage.js +451 -0
- package/extensions/dm-web-access/summary-model-scope.js +83 -0
- package/extensions/dm-web-access/summary-review.js +364 -0
- package/extensions/dm-web-access/tavily.js +199 -0
- package/extensions/dm-web-access/tinyfish.js +325 -0
- package/extensions/dm-web-access/utils.js +476 -0
- package/extensions/dm-web-access/valyu.js +189 -0
- package/extensions/dm-web-access/video-extract.js +336 -0
- package/extensions/dm-web-access/xai-search.js +285 -0
- package/extensions/dm-web-access/xcrawl.js +221 -0
- package/extensions/dm-web-access/youtube-extract.js +279 -0
- package/package.json +9 -1
|
@@ -0,0 +1,991 @@
|
|
|
1
|
+
import { chmodSync, closeSync, constants as fsConstants, existsSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, readSync, readdirSync, realpathSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { lstat as lstatAsync, open as openAsync, opendir as opendirAsync, readFile as readFileAsync, realpath as realpathAsync, rm as rmAsync } from "node:fs/promises";
|
|
3
|
+
import { execFile, spawn } from "node:child_process";
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
import { basename, dirname, extname, join, resolve as resolvePath, sep as pathSep } from "node:path";
|
|
6
|
+
import { activityMonitor } from "./activity.js";
|
|
7
|
+
import { checkGhAvailable, checkRepoSize, fetchViaApi, showGhHint } from "./github-api.js";
|
|
8
|
+
import { getWebSearchConfigPath } from "./utils.js";
|
|
9
|
+
const CONFIG_PATH = getWebSearchConfigPath();
|
|
10
|
+
const BINARY_EXTENSIONS = new Set([
|
|
11
|
+
".png",
|
|
12
|
+
".jpg",
|
|
13
|
+
".jpeg",
|
|
14
|
+
".gif",
|
|
15
|
+
".bmp",
|
|
16
|
+
".ico",
|
|
17
|
+
".webp",
|
|
18
|
+
".svg",
|
|
19
|
+
".tiff",
|
|
20
|
+
".tif",
|
|
21
|
+
".mp3",
|
|
22
|
+
".mp4",
|
|
23
|
+
".avi",
|
|
24
|
+
".mov",
|
|
25
|
+
".mkv",
|
|
26
|
+
".flv",
|
|
27
|
+
".wmv",
|
|
28
|
+
".wav",
|
|
29
|
+
".ogg",
|
|
30
|
+
".webm",
|
|
31
|
+
".flac",
|
|
32
|
+
".aac",
|
|
33
|
+
".zip",
|
|
34
|
+
".tar",
|
|
35
|
+
".gz",
|
|
36
|
+
".bz2",
|
|
37
|
+
".xz",
|
|
38
|
+
".7z",
|
|
39
|
+
".rar",
|
|
40
|
+
".zst",
|
|
41
|
+
".exe",
|
|
42
|
+
".dll",
|
|
43
|
+
".so",
|
|
44
|
+
".dylib",
|
|
45
|
+
".bin",
|
|
46
|
+
".o",
|
|
47
|
+
".a",
|
|
48
|
+
".lib",
|
|
49
|
+
".woff",
|
|
50
|
+
".woff2",
|
|
51
|
+
".ttf",
|
|
52
|
+
".otf",
|
|
53
|
+
".eot",
|
|
54
|
+
".pdf",
|
|
55
|
+
".doc",
|
|
56
|
+
".docx",
|
|
57
|
+
".xls",
|
|
58
|
+
".xlsx",
|
|
59
|
+
".ppt",
|
|
60
|
+
".pptx",
|
|
61
|
+
".sqlite",
|
|
62
|
+
".db",
|
|
63
|
+
".sqlite3",
|
|
64
|
+
".pyc",
|
|
65
|
+
".pyo",
|
|
66
|
+
".class",
|
|
67
|
+
".jar",
|
|
68
|
+
".war",
|
|
69
|
+
".iso",
|
|
70
|
+
".img",
|
|
71
|
+
".dmg"
|
|
72
|
+
]);
|
|
73
|
+
const NOISE_DIRS = new Set([
|
|
74
|
+
"node_modules",
|
|
75
|
+
"vendor",
|
|
76
|
+
".next",
|
|
77
|
+
"dist",
|
|
78
|
+
"build",
|
|
79
|
+
"__pycache__",
|
|
80
|
+
".venv",
|
|
81
|
+
"venv",
|
|
82
|
+
".tox",
|
|
83
|
+
".mypy_cache",
|
|
84
|
+
".pytest_cache",
|
|
85
|
+
"target",
|
|
86
|
+
".gradle",
|
|
87
|
+
".idea",
|
|
88
|
+
".vscode"
|
|
89
|
+
]);
|
|
90
|
+
const MAX_INLINE_FILE_CHARS = 1e5;
|
|
91
|
+
const MAX_TREE_ENTRIES = 200;
|
|
92
|
+
const CLONE_RUNTIME_OWNER_FILENAME = ".owner.json";
|
|
93
|
+
const CLONE_RUNTIME_OWNER_VERSION = 1;
|
|
94
|
+
const MAX_CLONE_RUNTIME_OWNER_BYTES = 1024;
|
|
95
|
+
const cloneCache = new Map;
|
|
96
|
+
const startedCloneRuntimeCleanups = new Set;
|
|
97
|
+
let cachedConfig = null;
|
|
98
|
+
let cloneRuntime = null;
|
|
99
|
+
function normalizeEnabled(value, fallback) {
|
|
100
|
+
return typeof value === "boolean" ? value : fallback;
|
|
101
|
+
}
|
|
102
|
+
function normalizePositiveNumber(value, fallback) {
|
|
103
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
104
|
+
return fallback;
|
|
105
|
+
return value > 0 ? value : fallback;
|
|
106
|
+
}
|
|
107
|
+
function expandPath(value) {
|
|
108
|
+
let expanded = value;
|
|
109
|
+
if (expanded.startsWith("~/") || expanded === "~") {
|
|
110
|
+
expanded = expanded.replace(/^~/, process.env.HOME || process.env.USERPROFILE || "");
|
|
111
|
+
}
|
|
112
|
+
expanded = expanded.replace(/\$([A-Z_][A-Z0-9_]*)/gi, (match, varName) => {
|
|
113
|
+
return process.env[varName] ?? match;
|
|
114
|
+
});
|
|
115
|
+
return expanded;
|
|
116
|
+
}
|
|
117
|
+
function normalizeClonePath(value, fallback) {
|
|
118
|
+
if (typeof value !== "string")
|
|
119
|
+
return fallback;
|
|
120
|
+
const normalized = value.trim();
|
|
121
|
+
if (normalized.length === 0)
|
|
122
|
+
return fallback;
|
|
123
|
+
return expandPath(normalized);
|
|
124
|
+
}
|
|
125
|
+
function loadGitHubConfig() {
|
|
126
|
+
if (cachedConfig)
|
|
127
|
+
return cachedConfig;
|
|
128
|
+
const defaults = {
|
|
129
|
+
enabled: true,
|
|
130
|
+
maxRepoSizeMB: 350,
|
|
131
|
+
cloneTimeoutSeconds: 30,
|
|
132
|
+
clonePath: "/tmp/pi-github-repos"
|
|
133
|
+
};
|
|
134
|
+
if (!existsSync(CONFIG_PATH)) {
|
|
135
|
+
cachedConfig = defaults;
|
|
136
|
+
return cachedConfig;
|
|
137
|
+
}
|
|
138
|
+
const rawText = readFileSync(CONFIG_PATH, "utf-8");
|
|
139
|
+
let raw;
|
|
140
|
+
try {
|
|
141
|
+
raw = JSON.parse(rawText);
|
|
142
|
+
} catch (err) {
|
|
143
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
144
|
+
throw new Error(`Failed to parse ${CONFIG_PATH}: ${message}`);
|
|
145
|
+
}
|
|
146
|
+
const gc = raw.githubClone ?? {};
|
|
147
|
+
cachedConfig = {
|
|
148
|
+
enabled: normalizeEnabled(gc.enabled, defaults.enabled),
|
|
149
|
+
maxRepoSizeMB: normalizePositiveNumber(gc.maxRepoSizeMB, defaults.maxRepoSizeMB),
|
|
150
|
+
cloneTimeoutSeconds: normalizePositiveNumber(gc.cloneTimeoutSeconds, defaults.cloneTimeoutSeconds),
|
|
151
|
+
clonePath: normalizeClonePath(gc.clonePath, defaults.clonePath)
|
|
152
|
+
};
|
|
153
|
+
return cachedConfig;
|
|
154
|
+
}
|
|
155
|
+
const NON_CODE_SEGMENTS = new Set([
|
|
156
|
+
"issues",
|
|
157
|
+
"pull",
|
|
158
|
+
"pulls",
|
|
159
|
+
"discussions",
|
|
160
|
+
"releases",
|
|
161
|
+
"wiki",
|
|
162
|
+
"actions",
|
|
163
|
+
"settings",
|
|
164
|
+
"security",
|
|
165
|
+
"projects",
|
|
166
|
+
"graphs",
|
|
167
|
+
"compare",
|
|
168
|
+
"commits",
|
|
169
|
+
"tags",
|
|
170
|
+
"branches",
|
|
171
|
+
"stargazers",
|
|
172
|
+
"watchers",
|
|
173
|
+
"network",
|
|
174
|
+
"forks",
|
|
175
|
+
"milestone",
|
|
176
|
+
"labels",
|
|
177
|
+
"packages",
|
|
178
|
+
"codespaces",
|
|
179
|
+
"contribute",
|
|
180
|
+
"community",
|
|
181
|
+
"sponsors",
|
|
182
|
+
"invitations",
|
|
183
|
+
"notifications",
|
|
184
|
+
"insights"
|
|
185
|
+
]);
|
|
186
|
+
export function parseGitHubUrl(url) {
|
|
187
|
+
let parsed;
|
|
188
|
+
try {
|
|
189
|
+
parsed = new URL(url);
|
|
190
|
+
} catch {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
const host = parsed.hostname.toLowerCase();
|
|
194
|
+
if (host !== "github.com" && host !== "www.github.com")
|
|
195
|
+
return null;
|
|
196
|
+
const segments = [];
|
|
197
|
+
for (const segment of parsed.pathname.split("/").filter(Boolean)) {
|
|
198
|
+
try {
|
|
199
|
+
segments.push(decodeURIComponent(segment));
|
|
200
|
+
} catch {
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (segments.length < 2)
|
|
205
|
+
return null;
|
|
206
|
+
const owner = segments[0];
|
|
207
|
+
const repo = segments[1].replace(/\.git$/, "");
|
|
208
|
+
if (!/^[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/.test(owner))
|
|
209
|
+
return null;
|
|
210
|
+
if (owner.includes("--"))
|
|
211
|
+
return null;
|
|
212
|
+
if (!/^[A-Za-z0-9._-]{1,100}$/.test(repo) || repo === "." || repo === "..")
|
|
213
|
+
return null;
|
|
214
|
+
if (NON_CODE_SEGMENTS.has(segments[2]?.toLowerCase()))
|
|
215
|
+
return null;
|
|
216
|
+
if (segments.length === 2) {
|
|
217
|
+
return { owner, repo, refIsFullSha: false, type: "root" };
|
|
218
|
+
}
|
|
219
|
+
const action = segments[2];
|
|
220
|
+
if (action !== "blob" && action !== "tree")
|
|
221
|
+
return null;
|
|
222
|
+
if (segments.length < 4)
|
|
223
|
+
return null;
|
|
224
|
+
const ref = segments[3];
|
|
225
|
+
if (ref.length === 0 || ref.length > 1024 || /[\0-\x1f\x7f]/.test(ref))
|
|
226
|
+
return null;
|
|
227
|
+
const refIsFullSha = /^[0-9a-f]{40}$/.test(ref);
|
|
228
|
+
const pathParts = segments.slice(4);
|
|
229
|
+
const path = pathParts.length > 0 ? pathParts.join("/") : "";
|
|
230
|
+
return {
|
|
231
|
+
owner,
|
|
232
|
+
repo,
|
|
233
|
+
ref,
|
|
234
|
+
refIsFullSha,
|
|
235
|
+
path,
|
|
236
|
+
type: action
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
function cacheKey(owner, repo, ref) {
|
|
240
|
+
return ref ? `${owner}/${repo}@${ref}` : `${owner}/${repo}`;
|
|
241
|
+
}
|
|
242
|
+
function readLinuxBootId() {
|
|
243
|
+
try {
|
|
244
|
+
const bootId = readFileSync("/proc/sys/kernel/random/boot_id", "utf-8").trim();
|
|
245
|
+
return bootId.length > 0 ? bootId : null;
|
|
246
|
+
} catch {
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
async function readLinuxBootIdAsync() {
|
|
251
|
+
try {
|
|
252
|
+
const bootId = (await readFileAsync("/proc/sys/kernel/random/boot_id", "utf-8")).trim();
|
|
253
|
+
return bootId.length > 0 ? bootId : null;
|
|
254
|
+
} catch {
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
function parseLinuxProcessInfo(stat) {
|
|
259
|
+
const closingParen = stat.lastIndexOf(") ");
|
|
260
|
+
if (closingParen < 0)
|
|
261
|
+
return null;
|
|
262
|
+
const fields = stat.slice(closingParen + 2).trim().split(/\s+/);
|
|
263
|
+
const state = fields[0];
|
|
264
|
+
const startTime = fields[19];
|
|
265
|
+
if (!state || !startTime || !/^\d+$/.test(startTime))
|
|
266
|
+
return null;
|
|
267
|
+
return { state, startTime, dead: false };
|
|
268
|
+
}
|
|
269
|
+
function readLinuxProcessInfo(pid) {
|
|
270
|
+
try {
|
|
271
|
+
return parseLinuxProcessInfo(readFileSync(`/proc/${pid}/stat`, "utf-8"));
|
|
272
|
+
} catch (err) {
|
|
273
|
+
if (err && typeof err === "object" && "code" in err && err.code === "ENOENT") {
|
|
274
|
+
return { state: "", startTime: null, dead: true };
|
|
275
|
+
}
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
async function readLinuxProcessInfoAsync(pid) {
|
|
280
|
+
try {
|
|
281
|
+
return parseLinuxProcessInfo(await readFileAsync(`/proc/${pid}/stat`, "utf-8"));
|
|
282
|
+
} catch (err) {
|
|
283
|
+
if (err && typeof err === "object" && "code" in err && err.code === "ENOENT") {
|
|
284
|
+
return { state: "", startTime: null, dead: true };
|
|
285
|
+
}
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
function currentCloneRuntimeOwner() {
|
|
290
|
+
const owner = {
|
|
291
|
+
version: CLONE_RUNTIME_OWNER_VERSION,
|
|
292
|
+
pid: process.pid,
|
|
293
|
+
platform: process.platform
|
|
294
|
+
};
|
|
295
|
+
if (process.platform === "linux") {
|
|
296
|
+
const bootId = readLinuxBootId();
|
|
297
|
+
const processInfo = readLinuxProcessInfo(process.pid);
|
|
298
|
+
if (bootId && processInfo?.startTime) {
|
|
299
|
+
owner.bootId = bootId;
|
|
300
|
+
owner.startTime = processInfo.startTime;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return owner;
|
|
304
|
+
}
|
|
305
|
+
function writeCloneRuntimeOwner(runtimePath) {
|
|
306
|
+
const ownerPath = join(runtimePath, CLONE_RUNTIME_OWNER_FILENAME);
|
|
307
|
+
writeFileSync(ownerPath, JSON.stringify(currentCloneRuntimeOwner()), {
|
|
308
|
+
encoding: "utf-8",
|
|
309
|
+
flag: "wx",
|
|
310
|
+
mode: 384
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
function parseCloneRuntimeOwner(text) {
|
|
314
|
+
let parsed;
|
|
315
|
+
try {
|
|
316
|
+
parsed = JSON.parse(text);
|
|
317
|
+
} catch {
|
|
318
|
+
return null;
|
|
319
|
+
}
|
|
320
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
321
|
+
return null;
|
|
322
|
+
const record = parsed;
|
|
323
|
+
if (record.version !== CLONE_RUNTIME_OWNER_VERSION || typeof record.pid !== "number" || !Number.isSafeInteger(record.pid) || record.pid <= 0 || typeof record.platform !== "string" || record.platform.length === 0 || record.platform.length > 32) {
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
const owner = {
|
|
327
|
+
version: CLONE_RUNTIME_OWNER_VERSION,
|
|
328
|
+
pid: record.pid,
|
|
329
|
+
platform: record.platform
|
|
330
|
+
};
|
|
331
|
+
if (record.bootId !== undefined) {
|
|
332
|
+
if (typeof record.bootId !== "string" || record.bootId.length === 0 || record.bootId.length > 256)
|
|
333
|
+
return null;
|
|
334
|
+
owner.bootId = record.bootId;
|
|
335
|
+
}
|
|
336
|
+
if (record.startTime !== undefined) {
|
|
337
|
+
if (typeof record.startTime !== "string" || !/^\d+$/.test(record.startTime))
|
|
338
|
+
return null;
|
|
339
|
+
owner.startTime = record.startTime;
|
|
340
|
+
}
|
|
341
|
+
return owner;
|
|
342
|
+
}
|
|
343
|
+
async function readCloneRuntimeOwner(runtimePath) {
|
|
344
|
+
const ownerPath = join(runtimePath, CLONE_RUNTIME_OWNER_FILENAME);
|
|
345
|
+
let file;
|
|
346
|
+
try {
|
|
347
|
+
const entry = await lstatAsync(ownerPath);
|
|
348
|
+
if (!entry.isFile() || entry.size > MAX_CLONE_RUNTIME_OWNER_BYTES)
|
|
349
|
+
return null;
|
|
350
|
+
const noFollow = fsConstants.O_NOFOLLOW ?? 0;
|
|
351
|
+
file = await openAsync(ownerPath, fsConstants.O_RDONLY | noFollow);
|
|
352
|
+
const opened = await file.stat();
|
|
353
|
+
if (!opened.isFile() || opened.size > MAX_CLONE_RUNTIME_OWNER_BYTES)
|
|
354
|
+
return null;
|
|
355
|
+
return parseCloneRuntimeOwner(await file.readFile("utf-8"));
|
|
356
|
+
} catch {
|
|
357
|
+
return null;
|
|
358
|
+
} finally {
|
|
359
|
+
if (file !== undefined) {
|
|
360
|
+
try {
|
|
361
|
+
await file.close();
|
|
362
|
+
} catch {}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
function processNonexistenceProven(pid) {
|
|
367
|
+
try {
|
|
368
|
+
process.kill(pid, 0);
|
|
369
|
+
return false;
|
|
370
|
+
} catch (err) {
|
|
371
|
+
return Boolean(err && typeof err === "object" && "code" in err && err.code === "ESRCH");
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
async function cloneRuntimeOwnerIsDead(owner, bootId) {
|
|
375
|
+
if (owner.platform !== process.platform)
|
|
376
|
+
return false;
|
|
377
|
+
if (process.platform !== "linux") {
|
|
378
|
+
return processNonexistenceProven(owner.pid);
|
|
379
|
+
}
|
|
380
|
+
if (!owner.bootId || !owner.startTime || !bootId)
|
|
381
|
+
return false;
|
|
382
|
+
const processInfo = await readLinuxProcessInfoAsync(owner.pid);
|
|
383
|
+
if (!processInfo)
|
|
384
|
+
return false;
|
|
385
|
+
if (processInfo.dead || processInfo.state === "Z" || processInfo.state === "X")
|
|
386
|
+
return true;
|
|
387
|
+
return processInfo.startTime !== owner.startTime || bootId !== owner.bootId;
|
|
388
|
+
}
|
|
389
|
+
async function removeCloneRuntimeAsync(parentPath, runtimePath) {
|
|
390
|
+
const normalizedParentPath = resolvePath(parentPath);
|
|
391
|
+
const normalizedRuntimePath = resolvePath(runtimePath);
|
|
392
|
+
if (dirname(normalizedRuntimePath) !== normalizedParentPath || !basename(normalizedRuntimePath).startsWith("runtime-"))
|
|
393
|
+
return;
|
|
394
|
+
try {
|
|
395
|
+
const realRuntimePath = await realpathAsync(normalizedRuntimePath);
|
|
396
|
+
if (dirname(realRuntimePath) !== normalizedParentPath || basename(realRuntimePath) !== basename(normalizedRuntimePath))
|
|
397
|
+
return;
|
|
398
|
+
const entry = await lstatAsync(normalizedRuntimePath);
|
|
399
|
+
if (entry.isSymbolicLink() || !entry.isDirectory())
|
|
400
|
+
return;
|
|
401
|
+
await rmAsync(normalizedRuntimePath, { recursive: true, force: true });
|
|
402
|
+
} catch {}
|
|
403
|
+
}
|
|
404
|
+
async function sweepStaleCloneRuntimes(parentPath) {
|
|
405
|
+
const normalizedParentPath = resolvePath(parentPath);
|
|
406
|
+
const bootId = process.platform === "linux" ? await readLinuxBootIdAsync() : null;
|
|
407
|
+
let directory;
|
|
408
|
+
try {
|
|
409
|
+
directory = await opendirAsync(normalizedParentPath);
|
|
410
|
+
} catch {
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
try {
|
|
414
|
+
for await (const entry of directory) {
|
|
415
|
+
if (!entry.name.startsWith("runtime-"))
|
|
416
|
+
continue;
|
|
417
|
+
try {
|
|
418
|
+
if (!entry.isDirectory() || entry.isSymbolicLink())
|
|
419
|
+
continue;
|
|
420
|
+
const runtimePath = resolvePath(normalizedParentPath, entry.name);
|
|
421
|
+
if (dirname(runtimePath) !== normalizedParentPath)
|
|
422
|
+
continue;
|
|
423
|
+
const runtimeEntry = await lstatAsync(runtimePath);
|
|
424
|
+
if (!runtimeEntry.isDirectory() || runtimeEntry.isSymbolicLink())
|
|
425
|
+
continue;
|
|
426
|
+
const realRuntimePath = await realpathAsync(runtimePath);
|
|
427
|
+
if (dirname(realRuntimePath) !== normalizedParentPath || basename(realRuntimePath) !== basename(runtimePath))
|
|
428
|
+
continue;
|
|
429
|
+
const owner = await readCloneRuntimeOwner(runtimePath);
|
|
430
|
+
if (!owner || !await cloneRuntimeOwnerIsDead(owner, bootId))
|
|
431
|
+
continue;
|
|
432
|
+
await removeCloneRuntimeAsync(normalizedParentPath, runtimePath);
|
|
433
|
+
} catch {}
|
|
434
|
+
}
|
|
435
|
+
} catch {} finally {
|
|
436
|
+
try {
|
|
437
|
+
await directory.close();
|
|
438
|
+
} catch {}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
function startStaleCloneRuntimeCleanup(parentPath) {
|
|
442
|
+
const normalizedParentPath = resolvePath(parentPath);
|
|
443
|
+
if (startedCloneRuntimeCleanups.has(normalizedParentPath))
|
|
444
|
+
return;
|
|
445
|
+
startedCloneRuntimeCleanups.add(normalizedParentPath);
|
|
446
|
+
sweepStaleCloneRuntimes(normalizedParentPath).catch(() => {});
|
|
447
|
+
}
|
|
448
|
+
function removeCloneRuntime(parentPath, runtimePath) {
|
|
449
|
+
const normalizedParentPath = resolvePath(parentPath);
|
|
450
|
+
const normalizedRuntimePath = resolvePath(runtimePath);
|
|
451
|
+
if (dirname(normalizedRuntimePath) !== normalizedParentPath || !basename(normalizedRuntimePath).startsWith("runtime-"))
|
|
452
|
+
return;
|
|
453
|
+
try {
|
|
454
|
+
const entry = lstatSync(normalizedRuntimePath);
|
|
455
|
+
if (entry.isSymbolicLink())
|
|
456
|
+
unlinkSync(normalizedRuntimePath);
|
|
457
|
+
else
|
|
458
|
+
rmSync(normalizedRuntimePath, { recursive: true, force: true });
|
|
459
|
+
} catch {}
|
|
460
|
+
}
|
|
461
|
+
function getCloneRuntimeRoot(config) {
|
|
462
|
+
if (cloneRuntime)
|
|
463
|
+
return cloneRuntime.rootPath;
|
|
464
|
+
let parentPath = null;
|
|
465
|
+
let runtimePath = null;
|
|
466
|
+
try {
|
|
467
|
+
const configuredPath = resolvePath(config.clonePath);
|
|
468
|
+
mkdirSync(configuredPath, { recursive: true });
|
|
469
|
+
parentPath = realpathSync(configuredPath);
|
|
470
|
+
startStaleCloneRuntimeCleanup(parentPath);
|
|
471
|
+
runtimePath = mkdtempSync(join(parentPath, "runtime-"));
|
|
472
|
+
chmodSync(runtimePath, 448);
|
|
473
|
+
const rootPath = realpathSync(runtimePath);
|
|
474
|
+
if (dirname(rootPath) !== parentPath) {
|
|
475
|
+
removeCloneRuntime(parentPath, runtimePath);
|
|
476
|
+
return null;
|
|
477
|
+
}
|
|
478
|
+
writeCloneRuntimeOwner(rootPath);
|
|
479
|
+
cloneRuntime = { parentPath, rootPath };
|
|
480
|
+
return rootPath;
|
|
481
|
+
} catch {
|
|
482
|
+
if (parentPath && runtimePath)
|
|
483
|
+
removeCloneRuntime(parentPath, runtimePath);
|
|
484
|
+
return null;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
function cloneDestination(config, owner, repo, ref) {
|
|
488
|
+
try {
|
|
489
|
+
const rootPath = getCloneRuntimeRoot(config);
|
|
490
|
+
if (!rootPath)
|
|
491
|
+
return null;
|
|
492
|
+
const digest = createHash("sha256").update(JSON.stringify([owner, repo, ref ?? null])).digest("hex");
|
|
493
|
+
const localPath = resolvePath(rootPath, digest);
|
|
494
|
+
if (dirname(localPath) !== rootPath)
|
|
495
|
+
return null;
|
|
496
|
+
return { rootPath, localPath };
|
|
497
|
+
} catch {
|
|
498
|
+
return null;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
function removeCloneDestination(destination) {
|
|
502
|
+
const rootPath = resolvePath(destination.rootPath);
|
|
503
|
+
const localPath = resolvePath(destination.localPath);
|
|
504
|
+
if (dirname(localPath) !== rootPath || !/^[0-9a-f]{64}$/.test(basename(localPath)))
|
|
505
|
+
return false;
|
|
506
|
+
try {
|
|
507
|
+
const entry = lstatSync(localPath);
|
|
508
|
+
if (entry.isSymbolicLink())
|
|
509
|
+
unlinkSync(localPath);
|
|
510
|
+
else
|
|
511
|
+
rmSync(localPath, { recursive: true, force: true });
|
|
512
|
+
return true;
|
|
513
|
+
} catch (err) {
|
|
514
|
+
if (err && typeof err === "object" && "code" in err && err.code === "ENOENT")
|
|
515
|
+
return true;
|
|
516
|
+
return false;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
const PROCESS_KILL_GRACE_MS = 3000;
|
|
520
|
+
function terminateProcessTree(child) {
|
|
521
|
+
const pid = child.pid;
|
|
522
|
+
if (!pid)
|
|
523
|
+
return;
|
|
524
|
+
if (process.platform === "win32") {
|
|
525
|
+
const killer = execFile("taskkill", ["/pid", String(pid), "/T", "/F"], { windowsHide: true }, (err) => {
|
|
526
|
+
if (err)
|
|
527
|
+
child.kill();
|
|
528
|
+
});
|
|
529
|
+
killer.unref();
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
try {
|
|
533
|
+
process.kill(-pid, "SIGTERM");
|
|
534
|
+
} catch {
|
|
535
|
+
child.kill();
|
|
536
|
+
}
|
|
537
|
+
const forceKill = setTimeout(() => {
|
|
538
|
+
try {
|
|
539
|
+
process.kill(-pid, "SIGKILL");
|
|
540
|
+
} catch {
|
|
541
|
+
child.kill("SIGKILL");
|
|
542
|
+
}
|
|
543
|
+
}, PROCESS_KILL_GRACE_MS);
|
|
544
|
+
forceKill.unref();
|
|
545
|
+
}
|
|
546
|
+
function execClone(args, destination, timeoutMs, signal) {
|
|
547
|
+
return new Promise((resolve) => {
|
|
548
|
+
const { localPath } = destination;
|
|
549
|
+
let settled = false;
|
|
550
|
+
let timeout;
|
|
551
|
+
let onAbort;
|
|
552
|
+
const finish = (success) => {
|
|
553
|
+
if (settled)
|
|
554
|
+
return;
|
|
555
|
+
settled = true;
|
|
556
|
+
if (timeout)
|
|
557
|
+
clearTimeout(timeout);
|
|
558
|
+
if (signal && onAbort)
|
|
559
|
+
signal.removeEventListener("abort", onAbort);
|
|
560
|
+
if (!success) {
|
|
561
|
+
removeCloneDestination(destination);
|
|
562
|
+
resolve(null);
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
resolve(localPath);
|
|
566
|
+
};
|
|
567
|
+
const child = spawn(args[0], args.slice(1), {
|
|
568
|
+
detached: process.platform !== "win32",
|
|
569
|
+
env: {
|
|
570
|
+
...process.env,
|
|
571
|
+
GIT_TERMINAL_PROMPT: "0",
|
|
572
|
+
GCM_INTERACTIVE: "Never",
|
|
573
|
+
GH_PROMPT_DISABLED: "1"
|
|
574
|
+
},
|
|
575
|
+
stdio: "ignore",
|
|
576
|
+
windowsHide: true
|
|
577
|
+
});
|
|
578
|
+
child.once("error", () => finish(false));
|
|
579
|
+
child.once("close", (code) => finish(code === 0));
|
|
580
|
+
timeout = setTimeout(() => terminateProcessTree(child), timeoutMs);
|
|
581
|
+
timeout.unref();
|
|
582
|
+
if (signal) {
|
|
583
|
+
onAbort = () => {
|
|
584
|
+
if (timeout)
|
|
585
|
+
clearTimeout(timeout);
|
|
586
|
+
terminateProcessTree(child);
|
|
587
|
+
};
|
|
588
|
+
if (signal.aborted)
|
|
589
|
+
onAbort();
|
|
590
|
+
else
|
|
591
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
async function cloneRepo(owner, repo, ref, config, destination, signal) {
|
|
596
|
+
const { localPath } = destination;
|
|
597
|
+
if (!removeCloneDestination(destination))
|
|
598
|
+
return null;
|
|
599
|
+
const timeoutMs = config.cloneTimeoutSeconds * 1000;
|
|
600
|
+
const hasGh = await checkGhAvailable();
|
|
601
|
+
if (hasGh) {
|
|
602
|
+
const args = ["gh", "repo", "clone", `${owner}/${repo}`, localPath, "--", "--depth", "1", "--single-branch"];
|
|
603
|
+
if (ref)
|
|
604
|
+
args.push("--branch", ref);
|
|
605
|
+
return execClone(args, destination, timeoutMs, signal);
|
|
606
|
+
}
|
|
607
|
+
showGhHint();
|
|
608
|
+
const gitUrl = `https://github.com/${owner}/${repo}.git`;
|
|
609
|
+
const args = ["git", "clone", "--depth", "1", "--single-branch"];
|
|
610
|
+
if (ref)
|
|
611
|
+
args.push("--branch", ref);
|
|
612
|
+
args.push(gitUrl, localPath);
|
|
613
|
+
return execClone(args, destination, timeoutMs, signal);
|
|
614
|
+
}
|
|
615
|
+
function isBinaryFile(filePath) {
|
|
616
|
+
const ext = extname(filePath).toLowerCase();
|
|
617
|
+
if (BINARY_EXTENSIONS.has(ext))
|
|
618
|
+
return true;
|
|
619
|
+
let fd;
|
|
620
|
+
try {
|
|
621
|
+
fd = openSync(filePath, "r");
|
|
622
|
+
} catch {
|
|
623
|
+
return false;
|
|
624
|
+
}
|
|
625
|
+
try {
|
|
626
|
+
const buf = Buffer.alloc(512);
|
|
627
|
+
const bytesRead = readSync(fd, buf, 0, 512, 0);
|
|
628
|
+
for (let i = 0;i < bytesRead; i++) {
|
|
629
|
+
if (buf[i] === 0)
|
|
630
|
+
return true;
|
|
631
|
+
}
|
|
632
|
+
} catch {
|
|
633
|
+
return false;
|
|
634
|
+
} finally {
|
|
635
|
+
closeSync(fd);
|
|
636
|
+
}
|
|
637
|
+
return false;
|
|
638
|
+
}
|
|
639
|
+
function formatFileSize(bytes) {
|
|
640
|
+
if (bytes < 1024)
|
|
641
|
+
return `${bytes} B`;
|
|
642
|
+
if (bytes < 1024 * 1024)
|
|
643
|
+
return `${(bytes / 1024).toFixed(1)} KB`;
|
|
644
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
645
|
+
}
|
|
646
|
+
function resolveWithinRepo(rootPath, relativePath) {
|
|
647
|
+
const normalizedRoot = resolvePath(rootPath);
|
|
648
|
+
const candidate = resolvePath(normalizedRoot, relativePath);
|
|
649
|
+
if (candidate !== normalizedRoot) {
|
|
650
|
+
const rootPrefix = normalizedRoot.endsWith(pathSep) ? normalizedRoot : normalizedRoot + pathSep;
|
|
651
|
+
if (!candidate.startsWith(rootPrefix))
|
|
652
|
+
return null;
|
|
653
|
+
}
|
|
654
|
+
if (!existsSync(candidate))
|
|
655
|
+
return candidate;
|
|
656
|
+
try {
|
|
657
|
+
const realRoot = realpathSync(normalizedRoot);
|
|
658
|
+
const realCandidate = realpathSync(candidate);
|
|
659
|
+
if (realCandidate === realRoot)
|
|
660
|
+
return candidate;
|
|
661
|
+
const realRootPrefix = realRoot.endsWith(pathSep) ? realRoot : realRoot + pathSep;
|
|
662
|
+
return realCandidate.startsWith(realRootPrefix) ? candidate : null;
|
|
663
|
+
} catch {
|
|
664
|
+
return null;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
function readTextFile(path) {
|
|
668
|
+
try {
|
|
669
|
+
return readFileSync(path, "utf-8");
|
|
670
|
+
} catch {
|
|
671
|
+
return null;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
function buildTree(rootPath) {
|
|
675
|
+
const entries = [];
|
|
676
|
+
function walk(dir, relPath) {
|
|
677
|
+
if (entries.length >= MAX_TREE_ENTRIES)
|
|
678
|
+
return;
|
|
679
|
+
let items;
|
|
680
|
+
try {
|
|
681
|
+
items = readdirSync(dir).sort();
|
|
682
|
+
} catch {
|
|
683
|
+
return;
|
|
684
|
+
}
|
|
685
|
+
for (const item of items) {
|
|
686
|
+
if (entries.length >= MAX_TREE_ENTRIES)
|
|
687
|
+
return;
|
|
688
|
+
if (item === ".git")
|
|
689
|
+
continue;
|
|
690
|
+
const rel = relPath ? `${relPath}/${item}` : item;
|
|
691
|
+
const safePath = resolveWithinRepo(rootPath, rel);
|
|
692
|
+
if (!safePath) {
|
|
693
|
+
entries.push(`${rel} [outside repo skipped]`);
|
|
694
|
+
continue;
|
|
695
|
+
}
|
|
696
|
+
let stat;
|
|
697
|
+
try {
|
|
698
|
+
stat = statSync(safePath);
|
|
699
|
+
} catch {
|
|
700
|
+
continue;
|
|
701
|
+
}
|
|
702
|
+
if (stat.isDirectory()) {
|
|
703
|
+
if (NOISE_DIRS.has(item)) {
|
|
704
|
+
entries.push(`${rel}/ [skipped]`);
|
|
705
|
+
continue;
|
|
706
|
+
}
|
|
707
|
+
entries.push(`${rel}/`);
|
|
708
|
+
walk(safePath, rel);
|
|
709
|
+
} else {
|
|
710
|
+
entries.push(rel);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
walk(rootPath, "");
|
|
715
|
+
if (entries.length >= MAX_TREE_ENTRIES) {
|
|
716
|
+
entries.push(`... (truncated at ${MAX_TREE_ENTRIES} entries)`);
|
|
717
|
+
}
|
|
718
|
+
return entries.join(`
|
|
719
|
+
`);
|
|
720
|
+
}
|
|
721
|
+
function buildDirListing(rootPath, subPath) {
|
|
722
|
+
const targetPath = resolveWithinRepo(rootPath, subPath);
|
|
723
|
+
if (!targetPath)
|
|
724
|
+
return "(path escapes repository root)";
|
|
725
|
+
const lines = [];
|
|
726
|
+
let items;
|
|
727
|
+
try {
|
|
728
|
+
items = readdirSync(targetPath).sort();
|
|
729
|
+
} catch {
|
|
730
|
+
return "(directory not readable)";
|
|
731
|
+
}
|
|
732
|
+
for (const item of items) {
|
|
733
|
+
if (item === ".git")
|
|
734
|
+
continue;
|
|
735
|
+
const rel = subPath ? `${subPath}/${item}` : item;
|
|
736
|
+
const safePath = resolveWithinRepo(rootPath, rel);
|
|
737
|
+
if (!safePath) {
|
|
738
|
+
lines.push(` ${item} (outside repo)`);
|
|
739
|
+
continue;
|
|
740
|
+
}
|
|
741
|
+
try {
|
|
742
|
+
const stat = statSync(safePath);
|
|
743
|
+
if (stat.isDirectory()) {
|
|
744
|
+
lines.push(` ${item}/`);
|
|
745
|
+
} else {
|
|
746
|
+
lines.push(` ${item} (${formatFileSize(stat.size)})`);
|
|
747
|
+
}
|
|
748
|
+
} catch {
|
|
749
|
+
lines.push(` ${item} (unreadable)`);
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
return lines.join(`
|
|
753
|
+
`);
|
|
754
|
+
}
|
|
755
|
+
function readReadme(localPath) {
|
|
756
|
+
const candidates = ["README.md", "readme.md", "README", "README.txt", "README.rst"];
|
|
757
|
+
for (const name of candidates) {
|
|
758
|
+
const readmePath = join(localPath, name);
|
|
759
|
+
if (existsSync(readmePath)) {
|
|
760
|
+
try {
|
|
761
|
+
const content = readFileSync(readmePath, "utf-8");
|
|
762
|
+
return content.length > 8192 ? content.slice(0, 8192) + `
|
|
763
|
+
|
|
764
|
+
[README truncated at 8K chars]` : content;
|
|
765
|
+
} catch {
|
|
766
|
+
continue;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
return null;
|
|
771
|
+
}
|
|
772
|
+
function generateContent(localPath, info) {
|
|
773
|
+
const lines = [];
|
|
774
|
+
lines.push(`Repository cloned to: ${localPath}`);
|
|
775
|
+
lines.push("");
|
|
776
|
+
if (info.type === "root") {
|
|
777
|
+
lines.push("## Structure");
|
|
778
|
+
lines.push(buildTree(localPath));
|
|
779
|
+
lines.push("");
|
|
780
|
+
const readme = readReadme(localPath);
|
|
781
|
+
if (readme) {
|
|
782
|
+
lines.push("## README.md");
|
|
783
|
+
lines.push(readme);
|
|
784
|
+
lines.push("");
|
|
785
|
+
}
|
|
786
|
+
lines.push("Use `read` and `bash` tools at the path above to explore further.");
|
|
787
|
+
return lines.join(`
|
|
788
|
+
`);
|
|
789
|
+
}
|
|
790
|
+
if (info.type === "tree") {
|
|
791
|
+
const dirPath = info.path || "";
|
|
792
|
+
const fullDirPath = resolveWithinRepo(localPath, dirPath);
|
|
793
|
+
if (!fullDirPath || !existsSync(fullDirPath)) {
|
|
794
|
+
lines.push(`Path \`${dirPath}\` not found in clone. Showing repository root instead.`);
|
|
795
|
+
lines.push("");
|
|
796
|
+
lines.push("## Structure");
|
|
797
|
+
lines.push(buildTree(localPath));
|
|
798
|
+
} else {
|
|
799
|
+
lines.push(`## ${dirPath || "/"}`);
|
|
800
|
+
lines.push(buildDirListing(localPath, dirPath));
|
|
801
|
+
}
|
|
802
|
+
lines.push("");
|
|
803
|
+
lines.push("Use `read` and `bash` tools at the path above to explore further.");
|
|
804
|
+
return lines.join(`
|
|
805
|
+
`);
|
|
806
|
+
}
|
|
807
|
+
if (info.type === "blob") {
|
|
808
|
+
const filePath = info.path || "";
|
|
809
|
+
const fullFilePath = resolveWithinRepo(localPath, filePath);
|
|
810
|
+
if (!fullFilePath || !existsSync(fullFilePath)) {
|
|
811
|
+
lines.push(`Path \`${filePath}\` not found in clone. Showing repository root instead.`);
|
|
812
|
+
lines.push("");
|
|
813
|
+
lines.push("## Structure");
|
|
814
|
+
lines.push(buildTree(localPath));
|
|
815
|
+
lines.push("");
|
|
816
|
+
lines.push("Use `read` and `bash` tools at the path above to explore further.");
|
|
817
|
+
return lines.join(`
|
|
818
|
+
`);
|
|
819
|
+
}
|
|
820
|
+
let stat;
|
|
821
|
+
try {
|
|
822
|
+
stat = statSync(fullFilePath);
|
|
823
|
+
} catch (err) {
|
|
824
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
825
|
+
lines.push(`Could not inspect \`${filePath}\`: ${message}`);
|
|
826
|
+
lines.push("");
|
|
827
|
+
lines.push("Use `read` and `bash` tools at the path above to explore further.");
|
|
828
|
+
return lines.join(`
|
|
829
|
+
`);
|
|
830
|
+
}
|
|
831
|
+
if (stat.isDirectory()) {
|
|
832
|
+
lines.push(`## ${filePath || "/"}`);
|
|
833
|
+
lines.push(buildDirListing(localPath, filePath));
|
|
834
|
+
lines.push("");
|
|
835
|
+
lines.push("Use `read` and `bash` tools at the path above to explore further.");
|
|
836
|
+
return lines.join(`
|
|
837
|
+
`);
|
|
838
|
+
}
|
|
839
|
+
if (isBinaryFile(fullFilePath)) {
|
|
840
|
+
const ext = extname(filePath).replace(".", "");
|
|
841
|
+
lines.push(`## ${filePath}`);
|
|
842
|
+
lines.push(`Binary file (${ext}, ${formatFileSize(stat.size)}). Use \`read\` or \`bash\` tools at the path above to inspect.`);
|
|
843
|
+
return lines.join(`
|
|
844
|
+
`);
|
|
845
|
+
}
|
|
846
|
+
const content = readTextFile(fullFilePath);
|
|
847
|
+
if (content === null) {
|
|
848
|
+
lines.push(`Could not read \`${filePath}\` as UTF-8 text.`);
|
|
849
|
+
lines.push("");
|
|
850
|
+
lines.push("Use `read` and `bash` tools at the path above to explore further.");
|
|
851
|
+
return lines.join(`
|
|
852
|
+
`);
|
|
853
|
+
}
|
|
854
|
+
lines.push(`## ${filePath}`);
|
|
855
|
+
if (content.length > MAX_INLINE_FILE_CHARS) {
|
|
856
|
+
lines.push(content.slice(0, MAX_INLINE_FILE_CHARS));
|
|
857
|
+
lines.push("");
|
|
858
|
+
lines.push(`[File truncated at 100K chars. Full file: ${fullFilePath}]`);
|
|
859
|
+
} else {
|
|
860
|
+
lines.push(content);
|
|
861
|
+
}
|
|
862
|
+
lines.push("");
|
|
863
|
+
lines.push("Use `read` and `bash` tools at the path above to explore further.");
|
|
864
|
+
return lines.join(`
|
|
865
|
+
`);
|
|
866
|
+
}
|
|
867
|
+
return lines.join(`
|
|
868
|
+
`);
|
|
869
|
+
}
|
|
870
|
+
async function awaitCachedClone(cached, url, owner, repo, info, signal) {
|
|
871
|
+
if (signal?.aborted)
|
|
872
|
+
return null;
|
|
873
|
+
const result = await cached.clonePromise;
|
|
874
|
+
if (signal?.aborted)
|
|
875
|
+
return null;
|
|
876
|
+
if (result) {
|
|
877
|
+
const content = generateContent(result, info);
|
|
878
|
+
const title = info.path ? `${owner}/${repo} - ${info.path}` : `${owner}/${repo}`;
|
|
879
|
+
return { url, title, content, error: null };
|
|
880
|
+
}
|
|
881
|
+
return fetchViaApi(url, owner, repo, info);
|
|
882
|
+
}
|
|
883
|
+
export async function extractGitHub(url, signal, forceClone) {
|
|
884
|
+
const info = parseGitHubUrl(url);
|
|
885
|
+
if (!info)
|
|
886
|
+
return null;
|
|
887
|
+
if (signal?.aborted)
|
|
888
|
+
return null;
|
|
889
|
+
const config = loadGitHubConfig();
|
|
890
|
+
if (!config.enabled)
|
|
891
|
+
return null;
|
|
892
|
+
const { owner, repo } = info;
|
|
893
|
+
const key = cacheKey(owner, repo, info.ref);
|
|
894
|
+
const cached = cloneCache.get(key);
|
|
895
|
+
if (cached)
|
|
896
|
+
return awaitCachedClone(cached, url, owner, repo, info, signal);
|
|
897
|
+
if (info.refIsFullSha) {
|
|
898
|
+
if (signal?.aborted)
|
|
899
|
+
return null;
|
|
900
|
+
const sizeNote = `Note: Commit SHA URLs use the GitHub API instead of cloning.`;
|
|
901
|
+
return fetchViaApi(url, owner, repo, info, sizeNote);
|
|
902
|
+
}
|
|
903
|
+
const activityId = activityMonitor.logStart({ type: "fetch", url: `github.com/${owner}/${repo}` });
|
|
904
|
+
if (!forceClone) {
|
|
905
|
+
const sizeKB = await checkRepoSize(owner, repo);
|
|
906
|
+
if (signal?.aborted) {
|
|
907
|
+
activityMonitor.logComplete(activityId, 0);
|
|
908
|
+
return null;
|
|
909
|
+
}
|
|
910
|
+
if (sizeKB !== null) {
|
|
911
|
+
const sizeMB = sizeKB / 1024;
|
|
912
|
+
if (sizeMB > config.maxRepoSizeMB) {
|
|
913
|
+
if (signal?.aborted) {
|
|
914
|
+
activityMonitor.logComplete(activityId, 0);
|
|
915
|
+
return null;
|
|
916
|
+
}
|
|
917
|
+
const sizeNote = `Note: Repository is ${Math.round(sizeMB)}MB (threshold: ${config.maxRepoSizeMB}MB). ` + `Showing API-fetched content instead of full clone. Ask the user if they'd like to clone the full repo -- ` + `if yes, call fetch_content again with the same URL and add forceClone: true to the params.`;
|
|
918
|
+
const apiView = await fetchViaApi(url, owner, repo, info, sizeNote);
|
|
919
|
+
if (apiView) {
|
|
920
|
+
activityMonitor.logComplete(activityId, 200);
|
|
921
|
+
return apiView;
|
|
922
|
+
}
|
|
923
|
+
activityMonitor.logError(activityId, "api fallback unavailable for oversized repository");
|
|
924
|
+
return null;
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
if (signal?.aborted) {
|
|
929
|
+
activityMonitor.logComplete(activityId, 0);
|
|
930
|
+
return null;
|
|
931
|
+
}
|
|
932
|
+
const cachedAfterSizeCheck = cloneCache.get(key);
|
|
933
|
+
if (cachedAfterSizeCheck) {
|
|
934
|
+
const cachedResult = await awaitCachedClone(cachedAfterSizeCheck, url, owner, repo, info, signal);
|
|
935
|
+
if (signal?.aborted) {
|
|
936
|
+
activityMonitor.logComplete(activityId, 0);
|
|
937
|
+
} else if (cachedResult) {
|
|
938
|
+
activityMonitor.logComplete(activityId, 200);
|
|
939
|
+
} else {
|
|
940
|
+
activityMonitor.logError(activityId, "clone failed");
|
|
941
|
+
}
|
|
942
|
+
return cachedResult;
|
|
943
|
+
}
|
|
944
|
+
const destination = cloneDestination(config, owner, repo, info.ref);
|
|
945
|
+
if (!destination) {
|
|
946
|
+
const apiFallback = await fetchViaApi(url, owner, repo, info);
|
|
947
|
+
if (apiFallback)
|
|
948
|
+
activityMonitor.logComplete(activityId, 200);
|
|
949
|
+
else
|
|
950
|
+
activityMonitor.logError(activityId, "invalid clone destination");
|
|
951
|
+
return apiFallback;
|
|
952
|
+
}
|
|
953
|
+
const clonePromise = cloneRepo(owner, repo, info.ref, config, destination, signal);
|
|
954
|
+
cloneCache.set(key, { destination, clonePromise });
|
|
955
|
+
const result = await clonePromise;
|
|
956
|
+
if (signal?.aborted) {
|
|
957
|
+
if (!result)
|
|
958
|
+
cloneCache.delete(key);
|
|
959
|
+
activityMonitor.logComplete(activityId, 0);
|
|
960
|
+
return null;
|
|
961
|
+
}
|
|
962
|
+
if (!result) {
|
|
963
|
+
cloneCache.delete(key);
|
|
964
|
+
if (signal?.aborted) {
|
|
965
|
+
activityMonitor.logComplete(activityId, 0);
|
|
966
|
+
return null;
|
|
967
|
+
}
|
|
968
|
+
const apiFallback = await fetchViaApi(url, owner, repo, info);
|
|
969
|
+
if (apiFallback) {
|
|
970
|
+
activityMonitor.logComplete(activityId, 200);
|
|
971
|
+
return apiFallback;
|
|
972
|
+
}
|
|
973
|
+
activityMonitor.logError(activityId, "clone and API fallback failed");
|
|
974
|
+
return null;
|
|
975
|
+
}
|
|
976
|
+
activityMonitor.logComplete(activityId, 200);
|
|
977
|
+
const content = generateContent(result, info);
|
|
978
|
+
const title = info.path ? `${owner}/${repo} - ${info.path}` : `${owner}/${repo}`;
|
|
979
|
+
return { url, title, content, error: null };
|
|
980
|
+
}
|
|
981
|
+
export function clearCloneCache() {
|
|
982
|
+
for (const entry of cloneCache.values()) {
|
|
983
|
+
removeCloneDestination(entry.destination);
|
|
984
|
+
}
|
|
985
|
+
cloneCache.clear();
|
|
986
|
+
if (cloneRuntime) {
|
|
987
|
+
removeCloneRuntime(cloneRuntime.parentPath, cloneRuntime.rootPath);
|
|
988
|
+
}
|
|
989
|
+
cloneRuntime = null;
|
|
990
|
+
cachedConfig = null;
|
|
991
|
+
}
|