@zero-gravity/zero-gravity 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -0
- package/LICENSE +21 -0
- package/README.md +50 -2
- package/THIRD-PARTY-NOTICES.md +100 -0
- package/dist/adv-context-SLTP32JQ.js +36 -0
- package/dist/advanced-EPLZB6DG.js +164 -0
- package/dist/agents-JYRQHH7R.js +181 -0
- package/dist/chat-AEHVCKXX.js +47 -0
- package/dist/chunk-2OH5KHWO.js +112 -0
- package/dist/chunk-2ZYSAYMJ.js +108 -0
- package/dist/chunk-4BZ4IXTO.js +161 -0
- package/dist/chunk-4ITJY25Z.js +144 -0
- package/dist/chunk-4JBLZQAQ.js +65 -0
- package/dist/chunk-6QLYRU3N.js +28 -0
- package/dist/chunk-7TACFVY2.js +57 -0
- package/dist/chunk-AY36KJTH.js +3023 -0
- package/dist/chunk-BJABQO7S.js +378 -0
- package/dist/chunk-C2GFK4JP.js +387 -0
- package/dist/chunk-CDC2K2KP.js +47 -0
- package/dist/chunk-DKCCXMSE.js +27 -0
- package/dist/chunk-FC3UOHOO.js +194 -0
- package/dist/chunk-FIASMDLX.js +216 -0
- package/dist/chunk-GMVDCQVW.js +88 -0
- package/dist/chunk-JQYZX56A.js +231 -0
- package/dist/chunk-JU5PGDOD.js +138 -0
- package/dist/chunk-JUZP5SXD.js +309 -0
- package/dist/chunk-KGA7VWQG.js +70 -0
- package/dist/chunk-L7VNR5LM.js +407 -0
- package/dist/chunk-LOSXCWHU.js +145 -0
- package/dist/chunk-NVC433AX.js +24 -0
- package/dist/chunk-QH27MDJM.js +248 -0
- package/dist/chunk-QUC5URWQ.js +708 -0
- package/dist/chunk-R2CPYTSP.js +31 -0
- package/dist/chunk-R5KQACGR.js +2833 -0
- package/dist/chunk-R6DONQKN.js +153 -0
- package/dist/chunk-RIX4S3J7.js +4747 -0
- package/dist/chunk-SZKAZ2VN.js +35 -0
- package/dist/chunk-T6DDNE3W.js +1338 -0
- package/dist/chunk-U4URD6TU.js +4605 -0
- package/dist/chunk-U5UIUB6L.js +51 -0
- package/dist/chunk-UMC77TR3.js +79 -0
- package/dist/chunk-UQURXFAE.js +80 -0
- package/dist/chunk-VBG7T7XY.js +1703 -0
- package/dist/chunk-VCCMJIAG.js +326 -0
- package/dist/chunk-VS375NYD.js +91 -0
- package/dist/chunk-WANZOLI5.js +1073 -0
- package/dist/chunk-WUF3TVAE.js +51 -0
- package/dist/chunk-XCWLFZSM.js +149 -0
- package/dist/chunk-YJEUXO5I.js +76 -0
- package/dist/chunk-YR6RCC6Y.js +894 -0
- package/dist/chunk-ZQFAX275.js +35 -0
- package/dist/config-ELG4FQ3K.js +30 -0
- package/dist/config-IEWRCG7R.js +50 -0
- package/dist/doctor-ZZC2D3RW.js +357 -0
- package/dist/ext-context-RIMZMEPB.js +37 -0
- package/dist/index.js +20 -0
- package/dist/init-UB3E47KM.js +22 -0
- package/dist/integrations-HW4VTLN4.js +183 -0
- package/dist/main-RAWWT22W.js +115 -0
- package/dist/mcp-SWD4SO35.js +232 -0
- package/dist/mcp-context-PM5FY665.js +28 -0
- package/dist/memory-QQ54GRCN.js +128 -0
- package/dist/memory-session-R6DGT6CS.js +14 -0
- package/dist/memory-summary-4P26B2ZY.js +18 -0
- package/dist/plugins-VUGTEUNV.js +166 -0
- package/dist/providers-O3WOVP5Q.js +417 -0
- package/dist/report-DDIZ6BLF.js +28 -0
- package/dist/sec-context-VQHW6TOZ.js +15 -0
- package/dist/security-SAYW4LU4.js +95 -0
- package/dist/sessions-CL6CRNQL.js +160 -0
- package/dist/skills-7KDGTTKX.js +126 -0
- package/dist/version-6E6A23B3.js +13 -0
- package/package.json +70 -4
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
SUMMARY_SYSTEM,
|
|
4
|
+
summarise,
|
|
5
|
+
transcriptForSummary
|
|
6
|
+
} from "./chunk-VS375NYD.js";
|
|
7
|
+
import {
|
|
8
|
+
runTurn
|
|
9
|
+
} from "./chunk-JUZP5SXD.js";
|
|
10
|
+
import {
|
|
11
|
+
MemoryError,
|
|
12
|
+
redactSensitive
|
|
13
|
+
} from "./chunk-XCWLFZSM.js";
|
|
14
|
+
|
|
15
|
+
// src/chat/memory-session.ts
|
|
16
|
+
import { randomUUID } from "crypto";
|
|
17
|
+
function createSummaryGenerator(backend, timeoutMs) {
|
|
18
|
+
return async (input, signal) => {
|
|
19
|
+
const b = backend();
|
|
20
|
+
if (b === null) return { status: "failed", message: "no AI provider is connected" };
|
|
21
|
+
const body = [input.previousSummary ? `Earlier summary of this session:
|
|
22
|
+
<untrusted source="earlier-summary">
|
|
23
|
+
${redactSensitive(input.previousSummary)}
|
|
24
|
+
</untrusted>
|
|
25
|
+
` : "", `Conversation:
|
|
26
|
+
<untrusted source="conversation">
|
|
27
|
+
${transcriptForSummary(input.messages)}
|
|
28
|
+
</untrusted>`, "Write the summary now."].join("\n");
|
|
29
|
+
const messages = [
|
|
30
|
+
{ role: "system", content: SUMMARY_SYSTEM },
|
|
31
|
+
{ role: "user", content: body }
|
|
32
|
+
];
|
|
33
|
+
const r = await runTurn(b, messages, { signal, stream: false, idleTimeoutMs: timeoutMs });
|
|
34
|
+
if (r.status === "cancelled") return { status: "cancelled" };
|
|
35
|
+
if (r.status === "failed") return { status: "failed", message: r.error.message.replace(/\.$/, "") };
|
|
36
|
+
return { status: "ok", text: r.text };
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
var MemoryController = class {
|
|
40
|
+
memory;
|
|
41
|
+
sessions;
|
|
42
|
+
#d;
|
|
43
|
+
#queue = Promise.resolve();
|
|
44
|
+
#block = null;
|
|
45
|
+
#blockLoaded = false;
|
|
46
|
+
#warned = /* @__PURE__ */ new Set();
|
|
47
|
+
#resumedFrom = null;
|
|
48
|
+
/** True once any message of the current session was saved. */
|
|
49
|
+
#saved = false;
|
|
50
|
+
constructor(deps) {
|
|
51
|
+
this.#d = deps;
|
|
52
|
+
this.memory = deps.memory;
|
|
53
|
+
this.sessions = deps.sessions;
|
|
54
|
+
deps.session.onMessage((m) => this.#enqueue(m));
|
|
55
|
+
}
|
|
56
|
+
get memorySettings() {
|
|
57
|
+
return this.#d.memorySettings();
|
|
58
|
+
}
|
|
59
|
+
get sessionSettings() {
|
|
60
|
+
return this.#d.sessionSettings();
|
|
61
|
+
}
|
|
62
|
+
get sessionId() {
|
|
63
|
+
return this.#d.session.id;
|
|
64
|
+
}
|
|
65
|
+
get dataDir() {
|
|
66
|
+
return this.#d.dataDir;
|
|
67
|
+
}
|
|
68
|
+
get saving() {
|
|
69
|
+
return this.sessionSettings.autoSave && this.sessions.project !== null;
|
|
70
|
+
}
|
|
71
|
+
get resumedFrom() {
|
|
72
|
+
return this.#resumedFrom;
|
|
73
|
+
}
|
|
74
|
+
get hasSaved() {
|
|
75
|
+
return this.#saved;
|
|
76
|
+
}
|
|
77
|
+
#enqueue(m) {
|
|
78
|
+
if (!this.saving) return;
|
|
79
|
+
const incoming = { seq: m.seq, role: m.role, content: m.content, timestamp: m.timestamp };
|
|
80
|
+
const id = this.#d.session.id;
|
|
81
|
+
const view = this.#d.runtime.view;
|
|
82
|
+
this.#queue = this.#queue.then(async () => {
|
|
83
|
+
try {
|
|
84
|
+
await this.sessions.append(id, [incoming], { provider: view.providerId, model: view.model });
|
|
85
|
+
this.#saved = true;
|
|
86
|
+
} catch (err) {
|
|
87
|
+
const text = err instanceof MemoryError ? err.message : "A message could not be saved.";
|
|
88
|
+
if (!this.#warned.has(text)) {
|
|
89
|
+
this.#warned.add(text);
|
|
90
|
+
this.#d.onProblem(`${text} The conversation continues; it is just not being saved.`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
async flush() {
|
|
96
|
+
await this.#queue;
|
|
97
|
+
}
|
|
98
|
+
// ---- memory text for the system prompt -----------------------------------------------------
|
|
99
|
+
invalidate() {
|
|
100
|
+
this.#blockLoaded = false;
|
|
101
|
+
}
|
|
102
|
+
/** The system prompt plus the saved notes (when memory is on). Cached until memory changes. */
|
|
103
|
+
async systemPrompt(base) {
|
|
104
|
+
if (!this.#blockLoaded) {
|
|
105
|
+
try {
|
|
106
|
+
this.#block = await this.memory.promptBlock();
|
|
107
|
+
} catch {
|
|
108
|
+
this.#block = null;
|
|
109
|
+
}
|
|
110
|
+
this.#blockLoaded = true;
|
|
111
|
+
}
|
|
112
|
+
if (!this.#block) return base;
|
|
113
|
+
return [base?.trim(), this.#block].filter((x) => !!x).join("\n\n");
|
|
114
|
+
}
|
|
115
|
+
async setEnabled(on) {
|
|
116
|
+
this.memorySettings.enabled = on;
|
|
117
|
+
this.invalidate();
|
|
118
|
+
if (!this.#d.saveSetting) return { persisted: false };
|
|
119
|
+
await this.#d.saveSetting("memory.enabled", on);
|
|
120
|
+
return { persisted: true };
|
|
121
|
+
}
|
|
122
|
+
// ---- finishing, new and resumed sessions ---------------------------------------------------
|
|
123
|
+
/** Finish the current session: mark it ended, summarise it, and (if asked) remember the summary. Never throws. */
|
|
124
|
+
async finalize(signal) {
|
|
125
|
+
await this.flush();
|
|
126
|
+
const id = this.#d.session.id;
|
|
127
|
+
if (!this.#saved) return { summary: null, note: null, remembered: false };
|
|
128
|
+
let result = null;
|
|
129
|
+
let note = null;
|
|
130
|
+
let remembered = false;
|
|
131
|
+
try {
|
|
132
|
+
await this.sessions.markEnded(id);
|
|
133
|
+
if (this.sessionSettings.autoSummarize) {
|
|
134
|
+
const { messages } = await this.sessions.readMessages(id);
|
|
135
|
+
const prev = await this.sessions.readSummary(id);
|
|
136
|
+
const backend = this.#d.runtime.backend;
|
|
137
|
+
const s = await summarise(messages, { generator: backend ? createSummaryGenerator(() => this.#d.runtime.backend, this.#d.timeoutMs()) : null, previousSummary: prev, signal });
|
|
138
|
+
await this.sessions.setSummary(id, s.text);
|
|
139
|
+
result = s.method;
|
|
140
|
+
note = s.method === "fallback" && s.reason ? `A plain summary was saved (${s.reason}).` : null;
|
|
141
|
+
const ms = this.memorySettings;
|
|
142
|
+
if (ms.enabled && ms.autoSave && ms.projectMemory && this.memory.project) {
|
|
143
|
+
try {
|
|
144
|
+
await this.memory.add("project", { text: `Session ${id.slice(0, 8)}: ${s.text.replace(/^## /gm, "").replace(/\n+/g, " ")}`.slice(0, ms.maxEntryChars), kind: "summary", source: "session", sessionId: id, redactInstead: true });
|
|
145
|
+
remembered = true;
|
|
146
|
+
this.invalidate();
|
|
147
|
+
} catch (err) {
|
|
148
|
+
if (!(err instanceof MemoryError && err.code === "duplicate")) note = `${note ?? ""} The summary was not added to memory: ${err instanceof MemoryError ? err.message : "error"}`.trim();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
} catch (err) {
|
|
153
|
+
note = `The session summary could not be saved: ${err instanceof MemoryError ? err.message : "unexpected problem"}`;
|
|
154
|
+
}
|
|
155
|
+
return { summary: result, note, remembered };
|
|
156
|
+
}
|
|
157
|
+
/** Start a fresh conversation with a new id (the old one is finished first). */
|
|
158
|
+
async startNew(signal) {
|
|
159
|
+
const oldId = this.#d.session.id;
|
|
160
|
+
const finalized = await this.finalize(signal);
|
|
161
|
+
this.#d.session.restore({ id: randomUUID(), messages: [] });
|
|
162
|
+
this.#saved = false;
|
|
163
|
+
this.#resumedFrom = null;
|
|
164
|
+
return { oldId, finalized };
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Load a saved session into the live conversation. Checks the project first. Messages go through the normal
|
|
168
|
+
* context controls: when the saved conversation is long and has a summary, the oldest part is archived behind
|
|
169
|
+
* that summary (like /compact); nothing is sent anywhere until you write your next message.
|
|
170
|
+
*/
|
|
171
|
+
async resume(idOrPrefix, signal) {
|
|
172
|
+
const loaded = await this.sessions.load(idOrPrefix);
|
|
173
|
+
this.sessions.assertResumable(loaded.meta);
|
|
174
|
+
if (loaded.meta.id === this.#d.session.id) throw new MemoryError("invalid", "That is the session you are already in.");
|
|
175
|
+
await this.finalize(signal);
|
|
176
|
+
const keep = this.#d.runtime.config.context.compaction.keepRecentMessages;
|
|
177
|
+
const msgs = loaded.messages;
|
|
178
|
+
const evenKeep = Math.max(2, keep - keep % 2);
|
|
179
|
+
let archiveCount = 0;
|
|
180
|
+
if (loaded.summary && msgs.length > evenKeep + 2) {
|
|
181
|
+
archiveCount = msgs.length - evenKeep;
|
|
182
|
+
if (archiveCount % 2 !== 0) archiveCount -= 1;
|
|
183
|
+
}
|
|
184
|
+
this.#d.session.restore({
|
|
185
|
+
id: loaded.meta.id,
|
|
186
|
+
messages: msgs.map((m) => ({ seq: m.seq, role: m.role, content: m.content, timestamp: new Date(m.at) })),
|
|
187
|
+
...archiveCount >= 2 && loaded.summary ? { compaction: { count: archiveCount, note: `Summary of the earlier part of this resumed session (saved by zg, not verified):
|
|
188
|
+
${loaded.summary}`, method: "excerpt" } } : {}
|
|
189
|
+
});
|
|
190
|
+
this.#saved = loaded.meta.messageCount > 0;
|
|
191
|
+
this.#resumedFrom = loaded.meta.id;
|
|
192
|
+
this.invalidate();
|
|
193
|
+
return { meta: loaded.meta, restored: msgs.length, archived: archiveCount, summary: loaded.summary, skippedLines: loaded.skippedLines, providerNote: this.#restoreProvider(loaded.meta), truncated: loaded.meta.truncated };
|
|
194
|
+
}
|
|
195
|
+
/** Re-select the saved provider/model only if it is available now; otherwise say clearly what is in use instead. */
|
|
196
|
+
#restoreProvider(meta) {
|
|
197
|
+
const rt = this.#d.runtime;
|
|
198
|
+
const now = rt.view;
|
|
199
|
+
if (!meta.provider) return "The saved session has no provider information; the current provider is unchanged.";
|
|
200
|
+
if (now.providerId === meta.provider && (meta.model === null || now.model === meta.model)) return `Provider and model are the same as before (${now.providerName}${now.model ? `, ${now.model}` : ""}).`;
|
|
201
|
+
const status = rt.providerStatuses().find((s) => s.id === meta.provider);
|
|
202
|
+
const was = `${meta.provider}${meta.model ? ` / ${meta.model}` : ""}`;
|
|
203
|
+
if (!status || status.readiness !== "ready") return `The session used ${was}, which is not available now (${status ? status.detail : "unknown provider"}). The current provider (${now.providerName}) is unchanged: this conversation will be sent there with your next message.`;
|
|
204
|
+
try {
|
|
205
|
+
const v = rt.select(meta.provider, meta.model);
|
|
206
|
+
return `Switched back to ${v.providerName}${v.model ? ` (${v.model})` : ""}, as in the saved session (this run only; saved settings are unchanged).`;
|
|
207
|
+
} catch {
|
|
208
|
+
return `The session used ${was}, but it could not be selected now. The current provider (${now.providerName}) is unchanged.`;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
export {
|
|
214
|
+
createSummaryGenerator,
|
|
215
|
+
MemoryController
|
|
216
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// package.json
|
|
4
|
+
var package_default = {
|
|
5
|
+
name: "@zero-gravity/zero-gravity",
|
|
6
|
+
version: "0.1.0",
|
|
7
|
+
description: "ZERO GRAVITY (zg): an independent AI coding terminal. Multi-provider chat, agents, file editing, commands, Git, MCP, plugins and a permission system.",
|
|
8
|
+
homepage: "https://github.com/Pratik-Verma-002/Zero-Gravity#readme",
|
|
9
|
+
repository: {
|
|
10
|
+
type: "git",
|
|
11
|
+
url: "git+https://github.com/Pratik-Verma-002/Zero-Gravity.git",
|
|
12
|
+
directory: "cli"
|
|
13
|
+
},
|
|
14
|
+
bugs: {
|
|
15
|
+
url: "https://github.com/Pratik-Verma-002/Zero-Gravity/issues"
|
|
16
|
+
},
|
|
17
|
+
type: "module",
|
|
18
|
+
bin: {
|
|
19
|
+
zg: "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
files: [
|
|
22
|
+
"dist",
|
|
23
|
+
"README.md",
|
|
24
|
+
"LICENSE",
|
|
25
|
+
"CHANGELOG.md",
|
|
26
|
+
"THIRD-PARTY-NOTICES.md"
|
|
27
|
+
],
|
|
28
|
+
engines: {
|
|
29
|
+
node: ">=22"
|
|
30
|
+
},
|
|
31
|
+
scripts: {
|
|
32
|
+
build: "tsup",
|
|
33
|
+
dev: "tsx src/cli/index.ts",
|
|
34
|
+
test: "vitest run",
|
|
35
|
+
typecheck: "tsc --noEmit",
|
|
36
|
+
lint: "eslint .",
|
|
37
|
+
"license:check": 'npx --yes license-checker-rseidelsohn --production --onlyAllow "MIT;ISC;BSD-2-Clause;BSD-3-Clause;Apache-2.0;0BSD;CC0-1.0;Unlicense;BlueOak-1.0.0"',
|
|
38
|
+
prepublishOnly: "npm run typecheck && npm run lint && npm run test && npm run build && node scripts/release-check.mjs --pack",
|
|
39
|
+
"release:check": "node scripts/release-check.mjs",
|
|
40
|
+
"pack:check": "npm pack --dry-run --json > /dev/null && node scripts/release-check.mjs --pack",
|
|
41
|
+
"audit:gate": "node scripts/audit-gate.mjs"
|
|
42
|
+
},
|
|
43
|
+
dependencies: {
|
|
44
|
+
"@modelcontextprotocol/sdk": "^1.32.2",
|
|
45
|
+
chalk: "^5.6.2",
|
|
46
|
+
commander: "^13.1.0",
|
|
47
|
+
zod: "^3.25.76"
|
|
48
|
+
},
|
|
49
|
+
devDependencies: {
|
|
50
|
+
"@eslint/js": "^9.30.0",
|
|
51
|
+
"@types/node": "^22.15.0",
|
|
52
|
+
eslint: "^9.30.0",
|
|
53
|
+
tsup: "^8.5.0",
|
|
54
|
+
tsx: "^4.20.0",
|
|
55
|
+
typescript: "^5.9.2",
|
|
56
|
+
"typescript-eslint": "^8.38.0",
|
|
57
|
+
vitest: "^3.2.4"
|
|
58
|
+
},
|
|
59
|
+
keywords: [
|
|
60
|
+
"cli",
|
|
61
|
+
"ai",
|
|
62
|
+
"coding",
|
|
63
|
+
"terminal",
|
|
64
|
+
"agent",
|
|
65
|
+
"mcp",
|
|
66
|
+
"llm",
|
|
67
|
+
"developer-tools",
|
|
68
|
+
"zero-gravity",
|
|
69
|
+
"zg"
|
|
70
|
+
],
|
|
71
|
+
license: "MIT",
|
|
72
|
+
publishConfig: {
|
|
73
|
+
access: "public"
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// src/core/version.ts
|
|
78
|
+
var VERSION = package_default.version;
|
|
79
|
+
var PRODUCT_NAME = "ZERO GRAVITY";
|
|
80
|
+
var BIN_NAME = "zg";
|
|
81
|
+
var MIN_NODE_MAJOR = 22;
|
|
82
|
+
|
|
83
|
+
export {
|
|
84
|
+
VERSION,
|
|
85
|
+
PRODUCT_NAME,
|
|
86
|
+
BIN_NAME,
|
|
87
|
+
MIN_NODE_MAJOR
|
|
88
|
+
};
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
sensitiveReason
|
|
4
|
+
} from "./chunk-ZQFAX275.js";
|
|
5
|
+
|
|
6
|
+
// src/filesystem/project-scanner.ts
|
|
7
|
+
import { promises as fsp } from "fs";
|
|
8
|
+
import path from "path";
|
|
9
|
+
|
|
10
|
+
// src/filesystem/ignore-manager.ts
|
|
11
|
+
var ALWAYS_IGNORED_DIRS = [".git", "node_modules"];
|
|
12
|
+
var DEFAULT_IGNORED_DIRS = ["dist", "build", "coverage", ".next", ".nuxt", ".cache", ".turbo", ".gradle", "__pycache__", ".venv", "venv", ".pytest_cache", ".mypy_cache", ".idea"];
|
|
13
|
+
function globToRegex(glob, anchored) {
|
|
14
|
+
let re = "";
|
|
15
|
+
for (let i = 0; i < glob.length; i++) {
|
|
16
|
+
const c = glob[i];
|
|
17
|
+
if (c === "*") {
|
|
18
|
+
if (glob[i + 1] === "*") {
|
|
19
|
+
const slashAfter = glob[i + 2] === "/";
|
|
20
|
+
re += slashAfter ? "(?:.*/)?" : ".*";
|
|
21
|
+
i += slashAfter ? 2 : 1;
|
|
22
|
+
} else re += "[^/]*";
|
|
23
|
+
} else if (c === "?") re += "[^/]";
|
|
24
|
+
else if (c === "[") {
|
|
25
|
+
const end = glob.indexOf("]", i + 2);
|
|
26
|
+
if (end === -1) re += "\\[";
|
|
27
|
+
else {
|
|
28
|
+
const body = glob.slice(i + 1, end).replace(/^!/, "^").replace(/\\/g, "\\\\");
|
|
29
|
+
re += `[${body}]`;
|
|
30
|
+
i = end;
|
|
31
|
+
}
|
|
32
|
+
} else if (c === "\\" && i + 1 < glob.length) {
|
|
33
|
+
re += glob[i + 1].replace(/[.*+?^${}()|[\]\\/]/g, "\\$&");
|
|
34
|
+
i += 1;
|
|
35
|
+
} else re += c.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
36
|
+
}
|
|
37
|
+
return new RegExp(`^${anchored ? "" : "(?:.*/)?"}${re}$`, "i");
|
|
38
|
+
}
|
|
39
|
+
var IgnoreRules = class {
|
|
40
|
+
#rules = [];
|
|
41
|
+
constructor(defaults = true) {
|
|
42
|
+
if (defaults) for (const d of DEFAULT_IGNORED_DIRS) this.addPattern("", `${d}/`);
|
|
43
|
+
}
|
|
44
|
+
addPattern(base, line) {
|
|
45
|
+
let p = line.replace(/\r$/, "");
|
|
46
|
+
if (p.trim() === "" || p.startsWith("#")) return;
|
|
47
|
+
p = p.replace(/(?<!\\)\s+$/, "");
|
|
48
|
+
const negate = p.startsWith("!");
|
|
49
|
+
if (negate) p = p.slice(1);
|
|
50
|
+
const dirOnly = p.endsWith("/");
|
|
51
|
+
if (dirOnly) p = p.slice(0, -1);
|
|
52
|
+
if (p === "") return;
|
|
53
|
+
const anchored = p.startsWith("/") || p.includes("/");
|
|
54
|
+
if (p.startsWith("/")) p = p.slice(1);
|
|
55
|
+
try {
|
|
56
|
+
this.#rules.push({ base, negate, dirOnly, re: globToRegex(p, anchored) });
|
|
57
|
+
} catch {
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/** Add the lines of an ignore file that lives in directory `base` ("" for the project root). */
|
|
61
|
+
addFile(base, text) {
|
|
62
|
+
for (const line of text.split("\n").slice(0, 5e3)) this.addPattern(base, line);
|
|
63
|
+
}
|
|
64
|
+
/** Is this workspace-relative path ignored? Ancestors are checked too, so a file inside an ignored directory is ignored. */
|
|
65
|
+
ignored(rel, isDir) {
|
|
66
|
+
const parts = rel.split("/").filter(Boolean);
|
|
67
|
+
for (let i = 0; i < parts.length; i++) {
|
|
68
|
+
const last = i === parts.length - 1;
|
|
69
|
+
const sub = parts.slice(0, i + 1).join("/");
|
|
70
|
+
const dir = last ? isDir : true;
|
|
71
|
+
if (dir && ALWAYS_IGNORED_DIRS.includes(parts[i].toLowerCase())) return true;
|
|
72
|
+
if (this.#matches(sub, dir)) return true;
|
|
73
|
+
}
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
#matches(rel, isDir) {
|
|
77
|
+
let result = false;
|
|
78
|
+
for (const r of this.#rules) {
|
|
79
|
+
if (r.dirOnly && !isDir) continue;
|
|
80
|
+
let local = rel;
|
|
81
|
+
if (r.base !== "") {
|
|
82
|
+
if (!rel.startsWith(`${r.base}/`)) continue;
|
|
83
|
+
local = rel.slice(r.base.length + 1);
|
|
84
|
+
}
|
|
85
|
+
if (r.re.test(local)) result = !r.negate;
|
|
86
|
+
}
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// src/filesystem/project-scanner.ts
|
|
92
|
+
var IGNORE_FILE_LIMIT = 1e5;
|
|
93
|
+
async function readIgnoreFile(abs) {
|
|
94
|
+
try {
|
|
95
|
+
const st = await fsp.lstat(abs);
|
|
96
|
+
if (!st.isFile() || st.size > IGNORE_FILE_LIMIT) return null;
|
|
97
|
+
return await fsp.readFile(abs, "utf8");
|
|
98
|
+
} catch {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async function loadIgnoreRules(root, extra) {
|
|
103
|
+
const rules = new IgnoreRules(true);
|
|
104
|
+
for (const d of extra.dirs) rules.addPattern("", `${d.replace(/^\/+|\/+$/g, "")}/`);
|
|
105
|
+
for (const p of extra.patterns) rules.addPattern("", p);
|
|
106
|
+
for (const name of [".gitignore", ".zgignore"]) {
|
|
107
|
+
const text = await readIgnoreFile(path.join(root, name));
|
|
108
|
+
if (text !== null) rules.addFile("", text);
|
|
109
|
+
}
|
|
110
|
+
return rules;
|
|
111
|
+
}
|
|
112
|
+
async function walk(v, rules, opts) {
|
|
113
|
+
const entries = [];
|
|
114
|
+
let truncated = false;
|
|
115
|
+
const startRel = opts.startRel ?? "";
|
|
116
|
+
const startAbs = startRel === "" ? v.root : (await v.resolveExisting(startRel, { allowRoot: true, allowSensitive: true })).abs;
|
|
117
|
+
async function visit(dirAbs, dirRel, depth) {
|
|
118
|
+
if (truncated) return;
|
|
119
|
+
if (opts.signal?.aborted) {
|
|
120
|
+
truncated = true;
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (dirRel !== "") {
|
|
124
|
+
const nested = await readIgnoreFile(path.join(dirAbs, ".gitignore"));
|
|
125
|
+
if (nested !== null) rules.addFile(dirRel, nested);
|
|
126
|
+
}
|
|
127
|
+
let items;
|
|
128
|
+
try {
|
|
129
|
+
items = await fsp.readdir(dirAbs, { withFileTypes: true });
|
|
130
|
+
} catch {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
items.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
134
|
+
for (const item of items) {
|
|
135
|
+
if (truncated) return;
|
|
136
|
+
if (opts.signal?.aborted) {
|
|
137
|
+
truncated = true;
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const rel = dirRel === "" ? item.name : `${dirRel}/${item.name}`;
|
|
141
|
+
const isDir = item.isDirectory();
|
|
142
|
+
if (rules.ignored(rel, isDir || item.isSymbolicLink())) continue;
|
|
143
|
+
if (item.isSymbolicLink()) {
|
|
144
|
+
entries.push({ rel, kind: "symlink", protected: sensitiveReason(rel) !== null });
|
|
145
|
+
} else if (isDir) {
|
|
146
|
+
entries.push({ rel, kind: "dir", protected: false });
|
|
147
|
+
if (depth < opts.maxDepth) await visit(path.join(dirAbs, item.name), rel, depth + 1);
|
|
148
|
+
} else if (item.isFile()) {
|
|
149
|
+
let size;
|
|
150
|
+
try {
|
|
151
|
+
size = (await fsp.lstat(path.join(dirAbs, item.name))).size;
|
|
152
|
+
} catch {
|
|
153
|
+
}
|
|
154
|
+
entries.push({ rel, kind: "file", ...size !== void 0 ? { size } : {}, protected: sensitiveReason(rel) !== null });
|
|
155
|
+
} else continue;
|
|
156
|
+
if (entries.length >= opts.maxEntries) {
|
|
157
|
+
truncated = true;
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
await visit(startAbs, startRel, 1);
|
|
163
|
+
return { entries, truncated };
|
|
164
|
+
}
|
|
165
|
+
var MARKERS = [
|
|
166
|
+
{ file: "package.json", type: "Node.js" },
|
|
167
|
+
{ file: "tsconfig.json", type: "TypeScript" },
|
|
168
|
+
{ file: "pyproject.toml", type: "Python" },
|
|
169
|
+
{ file: "requirements.txt", type: "Python" },
|
|
170
|
+
{ file: "setup.py", type: "Python" },
|
|
171
|
+
{ file: "Cargo.toml", type: "Rust" },
|
|
172
|
+
{ file: "go.mod", type: "Go" },
|
|
173
|
+
{ file: "pom.xml", type: "Java (Maven)" },
|
|
174
|
+
{ file: "settings.gradle", type: "Gradle" },
|
|
175
|
+
{ file: "settings.gradle.kts", type: "Gradle" },
|
|
176
|
+
{ file: "build.gradle", type: "Gradle" },
|
|
177
|
+
{ file: "build.gradle.kts", type: "Gradle" },
|
|
178
|
+
{ file: "Gemfile", type: "Ruby" },
|
|
179
|
+
{ file: "composer.json", type: "PHP" }
|
|
180
|
+
];
|
|
181
|
+
var SOURCE_DIRS = ["src", "lib", "app", "apps", "packages", "components", "pages", "tests", "test", "spec", "docs", "public", "scripts", "cmd", "internal"];
|
|
182
|
+
var CONFIG_FILES = ["package.json", "tsconfig.json", "pyproject.toml", "requirements.txt", "Cargo.toml", "go.mod", "pom.xml", "settings.gradle", "settings.gradle.kts", "build.gradle", "build.gradle.kts", "gradle.properties", ".gitignore", ".zgignore", ".editorconfig", "vite.config.ts", "vite.config.js", "next.config.js", "next.config.mjs", "tailwind.config.ts", "tailwind.config.js", "eslint.config.js", ".eslintrc.json", ".prettierrc", "jest.config.js", "vitest.config.ts", "Dockerfile", "Makefile", "README.md"];
|
|
183
|
+
async function exists(p, dir) {
|
|
184
|
+
try {
|
|
185
|
+
const st = await fsp.lstat(p);
|
|
186
|
+
return dir === void 0 ? true : dir ? st.isDirectory() : st.isFile();
|
|
187
|
+
} catch {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
async function detectProject(root) {
|
|
192
|
+
const types = /* @__PURE__ */ new Set();
|
|
193
|
+
const markers = [];
|
|
194
|
+
for (const m of MARKERS) {
|
|
195
|
+
if (await exists(path.join(root, m.file), false)) {
|
|
196
|
+
types.add(m.type);
|
|
197
|
+
markers.push(m.file);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
const android = await exists(path.join(root, "app", "src", "main", "AndroidManifest.xml"), false) || await exists(path.join(root, "app", "build.gradle"), false) || await exists(path.join(root, "app", "build.gradle.kts"), false);
|
|
201
|
+
if (android && types.has("Gradle")) {
|
|
202
|
+
types.delete("Gradle");
|
|
203
|
+
types.add("Android (Gradle)");
|
|
204
|
+
}
|
|
205
|
+
const sourceDirs = [];
|
|
206
|
+
for (const d of SOURCE_DIRS) if (await exists(path.join(root, d), true)) sourceDirs.push(d);
|
|
207
|
+
const configFiles = [];
|
|
208
|
+
for (const f of CONFIG_FILES) if (await exists(path.join(root, f), false)) configFiles.push(f);
|
|
209
|
+
const isGitRepo = await exists(path.join(root, ".git"));
|
|
210
|
+
return { root, types: [...types], markers, sourceDirs, configFiles, isGitRepo, recognized: markers.length > 0 || isGitRepo };
|
|
211
|
+
}
|
|
212
|
+
var ROOT_MARKERS = [".git", ...MARKERS.map((m) => m.file)];
|
|
213
|
+
async function findProjectRoot(start, homedir) {
|
|
214
|
+
let cur = path.resolve(start);
|
|
215
|
+
const home = path.resolve(homedir);
|
|
216
|
+
for (; ; ) {
|
|
217
|
+
if (cur === home || path.dirname(cur) === cur) break;
|
|
218
|
+
for (const m of ROOT_MARKERS) if (await exists(path.join(cur, m))) return { root: cur, detected: true };
|
|
219
|
+
const parent = path.dirname(cur);
|
|
220
|
+
if (parent === cur) break;
|
|
221
|
+
cur = parent;
|
|
222
|
+
}
|
|
223
|
+
return { root: path.resolve(start), detected: false };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export {
|
|
227
|
+
loadIgnoreRules,
|
|
228
|
+
walk,
|
|
229
|
+
detectProject,
|
|
230
|
+
findProjectRoot
|
|
231
|
+
};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
MIN_NODE_MAJOR
|
|
4
|
+
} from "./chunk-GMVDCQVW.js";
|
|
5
|
+
|
|
6
|
+
// src/platform/detect.ts
|
|
7
|
+
import { readFileSync } from "fs";
|
|
8
|
+
import os from "os";
|
|
9
|
+
function parseOsRelease(text) {
|
|
10
|
+
const fields = {};
|
|
11
|
+
for (const line of text.split(/\r?\n/)) {
|
|
12
|
+
const match = /^([A-Z_]+)=(.*)$/.exec(line.trim());
|
|
13
|
+
if (!match || match[1] === void 0 || match[2] === void 0) continue;
|
|
14
|
+
fields[match[1]] = match[2].replace(/^["']|["']$/g, "");
|
|
15
|
+
}
|
|
16
|
+
const id = fields["ID"];
|
|
17
|
+
if (!id) return null;
|
|
18
|
+
return { id, name: fields["PRETTY_NAME"] ?? fields["NAME"] ?? id };
|
|
19
|
+
}
|
|
20
|
+
function detectTermux(input) {
|
|
21
|
+
const { env, platform } = input;
|
|
22
|
+
if (env["TERMUX_VERSION"]) return true;
|
|
23
|
+
if (env["PREFIX"]?.includes("com.termux")) return true;
|
|
24
|
+
return platform === "android" && (env["HOME"]?.includes("com.termux") ?? false);
|
|
25
|
+
}
|
|
26
|
+
function detectAndroid(input, isTermux) {
|
|
27
|
+
if (isTermux || input.platform === "android") return true;
|
|
28
|
+
return input.platform === "linux" && Boolean(input.env["ANDROID_ROOT"] && input.env["ANDROID_DATA"]);
|
|
29
|
+
}
|
|
30
|
+
function osFamily(platform, isAndroid) {
|
|
31
|
+
if (isAndroid) return "android";
|
|
32
|
+
if (platform === "linux") return "linux";
|
|
33
|
+
if (platform === "darwin") return "macos";
|
|
34
|
+
if (platform === "win32") return "windows";
|
|
35
|
+
return "other";
|
|
36
|
+
}
|
|
37
|
+
function archOf(raw) {
|
|
38
|
+
if (raw === "x64") return "x64";
|
|
39
|
+
if (raw === "arm64") return "arm64";
|
|
40
|
+
return "other";
|
|
41
|
+
}
|
|
42
|
+
function detectShell(family, env) {
|
|
43
|
+
if (family === "windows") {
|
|
44
|
+
if (env["PROMPT"]) return "cmd";
|
|
45
|
+
if (env["PSModulePath"]) return "powershell";
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
const shell = env["SHELL"];
|
|
49
|
+
if (!shell) return null;
|
|
50
|
+
const parts = shell.split("/");
|
|
51
|
+
return parts[parts.length - 1] || null;
|
|
52
|
+
}
|
|
53
|
+
function detectTerminal(family, input) {
|
|
54
|
+
const { env, isTTY } = input;
|
|
55
|
+
let colorLevel = 0;
|
|
56
|
+
if (env["NO_COLOR"] !== void 0 && env["NO_COLOR"] !== "") {
|
|
57
|
+
colorLevel = 0;
|
|
58
|
+
} else if (!isTTY && !env["FORCE_COLOR"]) {
|
|
59
|
+
colorLevel = 0;
|
|
60
|
+
} else if (env["TERM"] === "dumb") {
|
|
61
|
+
colorLevel = 0;
|
|
62
|
+
} else if (/^(truecolor|24bit)$/i.test(env["COLORTERM"] ?? "")) {
|
|
63
|
+
colorLevel = 3;
|
|
64
|
+
} else if (/256/.test(env["TERM"] ?? "")) {
|
|
65
|
+
colorLevel = 2;
|
|
66
|
+
} else {
|
|
67
|
+
colorLevel = 1;
|
|
68
|
+
}
|
|
69
|
+
const locale = `${env["LC_ALL"] ?? ""}${env["LC_CTYPE"] ?? ""}${env["LANG"] ?? ""}`;
|
|
70
|
+
let unicode;
|
|
71
|
+
if (family === "windows") {
|
|
72
|
+
unicode = Boolean(env["WT_SESSION"] || env["ConEmuANSI"] || env["TERM_PROGRAM"] === "vscode");
|
|
73
|
+
} else if (family === "android") {
|
|
74
|
+
unicode = true;
|
|
75
|
+
} else {
|
|
76
|
+
unicode = /utf-?8/i.test(locale);
|
|
77
|
+
}
|
|
78
|
+
if (env["ZG_ASCII"] !== void 0 && env["ZG_ASCII"] !== "" && env["ZG_ASCII"] !== "0") unicode = false;
|
|
79
|
+
return { isTTY, colorLevel, unicode, columns: input.columns, rows: input.rows };
|
|
80
|
+
}
|
|
81
|
+
function detectPlatform(input) {
|
|
82
|
+
const isTermux = detectTermux(input);
|
|
83
|
+
const isAndroid = detectAndroid(input, isTermux);
|
|
84
|
+
const family = osFamily(input.platform, isAndroid);
|
|
85
|
+
const major = Number.parseInt(input.nodeVersion.replace(/^v/, "").split(".")[0] ?? "0", 10) || 0;
|
|
86
|
+
return {
|
|
87
|
+
os: family,
|
|
88
|
+
rawPlatform: input.platform,
|
|
89
|
+
isAndroid,
|
|
90
|
+
isTermux,
|
|
91
|
+
arch: archOf(input.arch),
|
|
92
|
+
rawArch: input.arch,
|
|
93
|
+
kernelRelease: input.release,
|
|
94
|
+
distro: input.osReleaseText ? parseOsRelease(input.osReleaseText) : null,
|
|
95
|
+
node: { version: input.nodeVersion, major, supported: major >= MIN_NODE_MAJOR },
|
|
96
|
+
shell: detectShell(family, input.env),
|
|
97
|
+
terminal: detectTerminal(family, input)
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function readOsRelease() {
|
|
101
|
+
for (const file of ["/etc/os-release", "/usr/lib/os-release"]) {
|
|
102
|
+
try {
|
|
103
|
+
return readFileSync(file, "utf8");
|
|
104
|
+
} catch {
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
function collectHostInput() {
|
|
110
|
+
const platform = process.platform;
|
|
111
|
+
return {
|
|
112
|
+
platform,
|
|
113
|
+
arch: process.arch,
|
|
114
|
+
release: os.release(),
|
|
115
|
+
env: process.env,
|
|
116
|
+
nodeVersion: process.version,
|
|
117
|
+
osReleaseText: platform === "linux" || platform === "android" ? readOsRelease() : null,
|
|
118
|
+
isTTY: Boolean(process.stdout.isTTY),
|
|
119
|
+
columns: process.stdout.columns ?? null,
|
|
120
|
+
rows: process.stdout.rows ?? null
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function detectHost() {
|
|
124
|
+
return detectPlatform(collectHostInput());
|
|
125
|
+
}
|
|
126
|
+
function describePlatform(info) {
|
|
127
|
+
if (info.isTermux) return "Android (Termux)";
|
|
128
|
+
if (info.isAndroid) return "Android";
|
|
129
|
+
if (info.os === "linux") return info.distro?.name ?? "Linux";
|
|
130
|
+
if (info.os === "macos") return "macOS";
|
|
131
|
+
if (info.os === "windows") return "Windows";
|
|
132
|
+
return info.rawPlatform;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export {
|
|
136
|
+
detectHost,
|
|
137
|
+
describePlatform
|
|
138
|
+
};
|