@vite-hub/workspace 0.0.1 → 0.0.3
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/README.md +225 -17
- package/dist/{ai-Bk3LLnit.js → ai-Ce3PtWFW.js} +160 -29
- package/dist/ai-nmHdzjCM.d.ts +162 -0
- package/dist/ai.d.ts +2 -85
- package/dist/ai.js +1 -1
- package/dist/asset-registry-CaG9b7tf.js +19 -0
- package/dist/cloudflare.d.ts +18 -0
- package/dist/cloudflare.js +2 -0
- package/dist/collections/client.d.ts +39 -0
- package/dist/collections/client.js +137 -0
- package/dist/collections.d.ts +59 -0
- package/dist/collections.js +159 -0
- package/dist/config-3I1BnsdO.d.ts +28 -0
- package/dist/config-CDoyls2X.js +30 -0
- package/dist/config-CZLO0m9h.js +7811 -0
- package/dist/file-CHfsZpso.js +23 -0
- package/dist/{files-DbsIjBya.js → files-QRV55OCx.js} +6 -5
- package/dist/github-CfA5vpJ5.js +189 -0
- package/dist/glob-D0vD_wRY.js +17 -0
- package/dist/host-CAnbugpI.js +370 -0
- package/dist/hosted-C1-tTTTf.js +581 -0
- package/dist/{providers/vercel/blob-store.js → hosted-vercel-blob-CWBILDVo.js} +100 -19
- package/dist/hosted-vercel-blob.d.ts +15 -0
- package/dist/hosted-vercel-blob.js +2 -0
- package/dist/hosted.d.ts +18 -0
- package/dist/hosted.js +2 -0
- package/dist/index.d.ts +129 -102
- package/dist/index.js +477 -599
- package/dist/lifecycle-BYoa9hVb.js +176 -0
- package/dist/loader.d.ts +1 -1
- package/dist/loader.js +1 -1
- package/dist/mountx.d.ts +10 -0
- package/dist/mountx.js +143 -0
- package/dist/nitro.d.ts +2 -31
- package/dist/nitro.js +2 -2
- package/dist/nuxt.d.ts +26 -0
- package/dist/nuxt.js +36 -0
- package/dist/path-CNI6wKIU.js +115 -0
- package/dist/{provider-BZuKAV4e.js → provider-C3_elpEh.js} +142 -37
- package/dist/providers/github/store.d.ts +6 -0
- package/dist/providers/github/store.js +2 -0
- package/dist/publish.d.ts +3 -2
- package/dist/publish.js +48 -142
- package/dist/{registry-D4Qzq26I.d.ts → registry-BIQVCBWI.d.ts} +3 -2
- package/dist/registry-CqyDTW9Y.js +74 -0
- package/dist/request-execution-u9mgUXKX.js +80 -0
- package/dist/runtime/assets.d.ts +2 -1
- package/dist/runtime/assets.js +5 -5
- package/dist/runtime/empty-assets-registry.d.ts +1 -1
- package/dist/runtime/empty-registry.d.ts +1 -1
- package/dist/runtime/state.d.ts +4 -21
- package/dist/runtime/state.js +7 -4
- package/dist/runtime/workspace.d.ts +2 -0
- package/dist/runtime/workspace.js +2 -0
- package/dist/runtime-options-BsPSv5O7.js +13 -0
- package/dist/runtime.d.ts +38 -0
- package/dist/runtime.js +626 -0
- package/dist/{search-DtaEyjpO.js → search-C4REyHj9.js} +2 -2
- package/dist/server.d.ts +65 -0
- package/dist/server.js +355 -0
- package/dist/shared-jfogBrd5.js +273 -0
- package/dist/source-metadata.d.ts +2 -0
- package/dist/source-metadata.js +2 -0
- package/dist/state-TTqa3x3Y.d.ts +25 -0
- package/dist/store-Bs1SLh55.js +414 -0
- package/dist/sync-DiuDmSGk.js +269 -0
- package/dist/target-D4Bqb1eH.js +11 -0
- package/dist/test.d.ts +1 -1
- package/dist/test.js +1 -1
- package/dist/types-60Yn149G.d.ts +612 -0
- package/dist/use-CUFSnFCS.d.ts +86 -0
- package/dist/use-CsV3oIxH.js +336 -0
- package/dist/vite-tbfDxtlu.js +2191 -0
- package/dist/vite.d.ts +33 -5
- package/dist/vite.js +2 -131
- package/dist/workspace-66SKyiec.d.ts +6 -0
- package/dist/workspace-C4k92Ow8.js +1460 -0
- package/package.json +143 -62
- package/dist/asset-registry-CMuQbyvl.js +0 -14
- package/dist/cloudflare-env-DL94Uvpz.js +0 -12
- package/dist/config-CvS-7sre.js +0 -49
- package/dist/errors-BUL8SoFS.js +0 -21
- package/dist/lifecycle-uD82JeGh.js +0 -92
- package/dist/module-8LZUMEXC.js +0 -922
- package/dist/path-BVv9OXhg.js +0 -63
- package/dist/providers/cloudflare/artifacts-store.d.ts +0 -6
- package/dist/providers/cloudflare/artifacts-store.js +0 -384
- package/dist/providers/vercel/blob-store.d.ts +0 -6
- package/dist/registry-Cnr0NoKv.js +0 -932
- package/dist/runtime/nitro-plugin.d.ts +0 -6
- package/dist/runtime/nitro-plugin.js +0 -14
- package/dist/sandbox-DtGXCWRq.js +0 -198
- package/dist/types-CwNcWTdt.d.ts +0 -378
- package/dist/utils-BtAm_K5P.js +0 -51
- /package/dist/{chunk-DrSxFLj_.js → chunk-8H4AJuhK.js} +0 -0
package/dist/index.js
CHANGED
|
@@ -1,656 +1,534 @@
|
|
|
1
|
-
import { t as __exportAll } from "./chunk-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
1
|
+
import { t as __exportAll } from "./chunk-8H4AJuhK.js";
|
|
2
|
+
import { o as normalizeSafeWorkspacePath, p as workspaceError, r as contentToBytes } from "./path-CNI6wKIU.js";
|
|
3
|
+
import { t as createWorkspaceTools } from "./ai-Ce3PtWFW.js";
|
|
4
|
+
import { d as fetch, g as markLiveWorkspaceSource, u as registerMcpResourcesSourceLoader } from "./config-CZLO0m9h.js";
|
|
5
|
+
import { g as isMissingWorkspacePathError, t as createWorkspace, u as resolveWorkspaceAutoCommit } from "./workspace-C4k92Ow8.js";
|
|
6
|
+
import { r as resolveRegisteredWorkspaceDefinition } from "./registry-CqyDTW9Y.js";
|
|
7
|
+
import { t as useWorkspace } from "./use-CsV3oIxH.js";
|
|
8
|
+
import { t as withWorkspaceRuntimeOptions } from "./runtime-options-BsPSv5O7.js";
|
|
9
|
+
import { t as file } from "./file-CHfsZpso.js";
|
|
10
|
+
import { t as github } from "./github-CfA5vpJ5.js";
|
|
11
|
+
import { t as glob } from "./glob-D0vD_wRY.js";
|
|
9
12
|
import { posix } from "node:path";
|
|
10
|
-
import {
|
|
13
|
+
import { lookup } from "mrmime";
|
|
14
|
+
import { markdown as markdown$1, mcpResources as mcpResources$1 } from "@vite-hub/source";
|
|
15
|
+
import { gzipSync } from "node:zlib";
|
|
11
16
|
//#region src/core/define.ts
|
|
17
|
+
const workspaceDefinitionKeys = new Set([
|
|
18
|
+
"bindings",
|
|
19
|
+
"commit",
|
|
20
|
+
"hooks",
|
|
21
|
+
"loaders",
|
|
22
|
+
"plugins",
|
|
23
|
+
"publish",
|
|
24
|
+
"rootDir",
|
|
25
|
+
"rules",
|
|
26
|
+
"sourceRootDir",
|
|
27
|
+
"sources",
|
|
28
|
+
"store"
|
|
29
|
+
]);
|
|
30
|
+
function assertWorkspaceDefinitionKeys(definition) {
|
|
31
|
+
const unsupported = Object.keys(definition).filter((key) => !workspaceDefinitionKeys.has(key));
|
|
32
|
+
if (!unsupported.length) return;
|
|
33
|
+
throw new TypeError(`[vitehub] defineWorkspace does not support option${unsupported.length === 1 ? "" : "s"}: ${unsupported.join(", ")}.`);
|
|
34
|
+
}
|
|
12
35
|
function defineWorkspace(definition) {
|
|
13
36
|
if (!definition || typeof definition !== "object") throw new TypeError("[vitehub] defineWorkspace requires a workspace definition.");
|
|
14
37
|
if ("name" in definition) throw new TypeError("[vitehub] Workspace names are inferred from definition filenames.");
|
|
38
|
+
assertWorkspaceDefinitionKeys(definition);
|
|
15
39
|
return definition;
|
|
16
40
|
}
|
|
17
41
|
//#endregion
|
|
18
42
|
//#region src/sources/custom.ts
|
|
19
43
|
function custom(source) {
|
|
20
|
-
return source;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const mount = typeof options.mount === "object" && options.mount && !("path" in options.mount) ? {
|
|
27
|
-
...options.mount,
|
|
28
|
-
path: ""
|
|
29
|
-
} : options.mount ?? "";
|
|
30
|
-
return {
|
|
31
|
-
...file(options),
|
|
32
|
-
cache: options.cache,
|
|
33
|
-
materialize: options.materialize,
|
|
34
|
-
mount,
|
|
35
|
-
validate: options.validate
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
//#endregion
|
|
39
|
-
//#region ../internal/src/http-request.ts
|
|
40
|
-
async function executeHttpRequest(definition, options = {}) {
|
|
41
|
-
const normalized = normalizeHttpRequest(definition);
|
|
42
|
-
const responseType = options.responseType || "json";
|
|
43
|
-
const response = await fetchWithRetry(normalized);
|
|
44
|
-
const decoded = await decodeResponse(response, responseType);
|
|
45
|
-
return {
|
|
46
|
-
data: options.schema ? await parseStandardSchema(options.schema, decoded, "HTTP response") : decoded,
|
|
47
|
-
mediaType: response.headers.get("content-type") || void 0,
|
|
48
|
-
status: response.status,
|
|
49
|
-
summary: redactedHttpRequestSummary(normalized, responseType)
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
async function fetchWithRetry(definition) {
|
|
53
|
-
const attempts = definition.method === "GET" || definition.method === "HEAD" ? 2 : 1;
|
|
54
|
-
let lastError;
|
|
55
|
-
for (let attempt = 1; attempt <= attempts; attempt += 1) try {
|
|
56
|
-
return await fetchOnce(definition);
|
|
57
|
-
} catch (error) {
|
|
58
|
-
lastError = error;
|
|
59
|
-
if (attempt === attempts) break;
|
|
60
|
-
}
|
|
61
|
-
throw lastError;
|
|
62
|
-
}
|
|
63
|
-
async function fetchOnce(definition) {
|
|
64
|
-
const headers = new Headers(definition.headers);
|
|
65
|
-
const body = serializeRequestBody(definition.body, headers);
|
|
66
|
-
const controller = definition.timeout ? new AbortController() : void 0;
|
|
67
|
-
const timeout = controller ? setTimeout(() => controller.abort(), definition.timeout) : void 0;
|
|
68
|
-
try {
|
|
69
|
-
const response = await fetch(urlWithQuery(definition).toString(), {
|
|
70
|
-
body,
|
|
71
|
-
headers,
|
|
72
|
-
method: definition.method,
|
|
73
|
-
signal: controller?.signal
|
|
74
|
-
});
|
|
75
|
-
if (!response.ok) throw new Error(`[vitehub] HTTP request failed with status ${response.status}.`);
|
|
76
|
-
return response;
|
|
77
|
-
} finally {
|
|
78
|
-
if (timeout) clearTimeout(timeout);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
function serializeRequestBody(body, headers) {
|
|
82
|
-
if (typeof body === "undefined") return void 0;
|
|
83
|
-
if (typeof body === "string" || body instanceof Uint8Array || body instanceof ArrayBuffer || ArrayBuffer.isView(body) || typeof Blob !== "undefined" && body instanceof Blob || typeof FormData !== "undefined" && body instanceof FormData || typeof URLSearchParams !== "undefined" && body instanceof URLSearchParams || typeof ReadableStream !== "undefined" && body instanceof ReadableStream) return body;
|
|
84
|
-
if (!headers.has("content-type")) headers.set("content-type", "application/json");
|
|
85
|
-
return JSON.stringify(body);
|
|
86
|
-
}
|
|
87
|
-
function urlWithQuery(definition) {
|
|
88
|
-
const url = new URL(definition.url);
|
|
89
|
-
if (!definition.query) return url;
|
|
90
|
-
for (const [key, value] of Object.entries(definition.query)) {
|
|
91
|
-
if (typeof value === "undefined") continue;
|
|
92
|
-
if (Array.isArray(value)) {
|
|
93
|
-
for (const item of value) if (typeof item !== "undefined") url.searchParams.append(key, queryValue(item));
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
url.searchParams.set(key, queryValue(value));
|
|
97
|
-
}
|
|
98
|
-
return url;
|
|
99
|
-
}
|
|
100
|
-
function queryValue(value) {
|
|
101
|
-
if (value === null) return "";
|
|
102
|
-
if (typeof value === "string") return value;
|
|
103
|
-
if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") return String(value);
|
|
104
|
-
return JSON.stringify(value);
|
|
105
|
-
}
|
|
106
|
-
async function decodeResponse(response, responseType) {
|
|
107
|
-
if (responseType === "text") return await response.text();
|
|
108
|
-
if (responseType === "arrayBuffer") return await response.arrayBuffer();
|
|
109
|
-
if (responseType === "blob") return await response.blob();
|
|
110
|
-
const text = await response.text();
|
|
111
|
-
return text ? JSON.parse(text) : void 0;
|
|
112
|
-
}
|
|
113
|
-
function normalizeHttpRequest(definition) {
|
|
114
|
-
const method = (definition.method || "GET").toUpperCase();
|
|
115
|
-
if (method !== "GET" && method !== "HEAD" && method !== "POST") throw new TypeError(`[vitehub] HTTP request method "${method}" is not supported. Use GET, HEAD, or POST.`);
|
|
116
|
-
return {
|
|
117
|
-
...definition,
|
|
118
|
-
method,
|
|
119
|
-
url: definition.url instanceof URL ? definition.url : new URL(definition.url)
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
async function parseStandardSchema(schema, value, label) {
|
|
123
|
-
const result = await schema["~standard"].validate(value);
|
|
124
|
-
if ("issues" in result && result.issues && result.issues.length > 0) throw new Error(`[vitehub] Invalid ${label}: ${formatIssues(result.issues)}`);
|
|
125
|
-
if (!("value" in result)) throw new Error(`[vitehub] Invalid ${label}: ${formatIssues(result.issues)}`);
|
|
126
|
-
return result.value;
|
|
127
|
-
}
|
|
128
|
-
function redactedHttpRequestSummary(definition, responseType) {
|
|
44
|
+
if (!("files" in source) || !Array.isArray(source.files)) return source;
|
|
45
|
+
const { files: inputFiles, ...options } = source;
|
|
46
|
+
const files = inputFiles.map((file) => ({
|
|
47
|
+
...file,
|
|
48
|
+
path: normalizeSafeWorkspacePath(file.path)
|
|
49
|
+
}));
|
|
129
50
|
return {
|
|
130
|
-
|
|
131
|
-
hasQuery: Boolean(definition.query && Object.keys(definition.query).length),
|
|
132
|
-
headers: definition.headers && Object.keys(definition.headers).length ? "redacted" : "none",
|
|
133
|
-
method: definition.method,
|
|
134
|
-
responseType,
|
|
135
|
-
url: `${definition.url.origin}${definition.url.pathname}`
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
function formatIssues(issues) {
|
|
139
|
-
if (Array.isArray(issues)) return issues.map((issue) => typeof issue === "string" ? issue : JSON.stringify(issue)).join("; ");
|
|
140
|
-
return typeof issues === "string" ? issues : JSON.stringify(issues);
|
|
141
|
-
}
|
|
142
|
-
//#endregion
|
|
143
|
-
//#region src/sources/fetch.ts
|
|
144
|
-
function fetch$1(options) {
|
|
145
|
-
const responseType = normalizePublicResponseType(options.responseType || "json");
|
|
146
|
-
const workspacePath = normalizeSafeWorkspacePath(options.path || deriveWorkspacePath(options.url, responseType), { allowEmpty: false });
|
|
147
|
-
const mountPath = posix.dirname(workspacePath) === "." ? "" : posix.dirname(workspacePath);
|
|
148
|
-
const key = mountPath ? workspacePath.slice(mountPath.length + 1) : workspacePath;
|
|
149
|
-
return markLiveWorkspaceSource({
|
|
150
|
-
cache: options.cache ?? false,
|
|
151
|
-
fingerprint: {
|
|
152
|
-
method: options.method,
|
|
153
|
-
path: workspacePath,
|
|
154
|
-
responseType,
|
|
155
|
-
url: String(options.url)
|
|
156
|
-
},
|
|
157
|
-
materialize: "lazy",
|
|
158
|
-
mount: mountPath,
|
|
51
|
+
...options,
|
|
159
52
|
async getKeys() {
|
|
160
|
-
return
|
|
53
|
+
return files.map((file) => file.path);
|
|
161
54
|
},
|
|
162
|
-
async getItem(
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
const
|
|
166
|
-
...request,
|
|
167
|
-
method: request?.method ?? options.method,
|
|
168
|
-
url: options.url
|
|
169
|
-
}, {
|
|
170
|
-
responseType,
|
|
171
|
-
schema: options.schema
|
|
172
|
-
});
|
|
55
|
+
async getItem(key, context) {
|
|
56
|
+
const file = files.find((file) => file.path === key);
|
|
57
|
+
if (!file) throw workspaceError(`[vitehub] Custom Workspace Source file does not exist: ${key}.`);
|
|
58
|
+
const { content, ...item } = file;
|
|
173
59
|
return {
|
|
60
|
+
...item,
|
|
61
|
+
content: typeof content === "function" ? await content(context) : content,
|
|
174
62
|
key,
|
|
175
|
-
|
|
176
|
-
content: serializeFetchSourceContent(options.transform ? await options.transform(result.data) : result.data, responseType),
|
|
177
|
-
mediaType: result.mediaType ?? mediaTypeFor(responseType),
|
|
178
|
-
metadata: {
|
|
179
|
-
request: result.summary,
|
|
180
|
-
status: result.status
|
|
181
|
-
}
|
|
182
|
-
};
|
|
183
|
-
},
|
|
184
|
-
async getMeta(sourcePath) {
|
|
185
|
-
if (sourcePath !== key) return void 0;
|
|
186
|
-
return {
|
|
187
|
-
mediaType: mediaTypeFor(responseType),
|
|
188
|
-
request: {
|
|
189
|
-
method: options.method || "GET",
|
|
190
|
-
responseType,
|
|
191
|
-
url: redactedUrl(options.url)
|
|
192
|
-
}
|
|
63
|
+
mediaType: item.mediaType || lookup(key) || void 0
|
|
193
64
|
};
|
|
194
65
|
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
function normalizePublicResponseType(responseType) {
|
|
198
|
-
if (responseType !== "json" && responseType !== "text") throw new TypeError(`[vitehub] source.fetch() responseType "${responseType}" is not supported in v1. Use json or text.`);
|
|
199
|
-
return responseType;
|
|
200
|
-
}
|
|
201
|
-
function deriveWorkspacePath(url, responseType) {
|
|
202
|
-
const parsed = url instanceof URL ? url : new URL(url);
|
|
203
|
-
if (parsed.search) throw new Error("[vitehub] source.fetch() requires an explicit path when the URL includes query parameters.");
|
|
204
|
-
let path = normalizeSafeWorkspacePath(decodeURI(parsed.pathname).replace(/^\/+/, ""), { allowEmpty: false });
|
|
205
|
-
if (!posix.extname(path)) path = `${path}.${responseType === "json" ? "json" : "txt"}`;
|
|
206
|
-
return path;
|
|
207
|
-
}
|
|
208
|
-
function serializeFetchSourceContent(value, responseType) {
|
|
209
|
-
if (value instanceof Uint8Array) return value;
|
|
210
|
-
if (responseType === "json") return JSON.stringify(value, null, 2) ?? "null";
|
|
211
|
-
if (typeof value === "string") return value;
|
|
212
|
-
return typeof value === "undefined" ? "" : String(value);
|
|
213
|
-
}
|
|
214
|
-
function mediaTypeFor(responseType) {
|
|
215
|
-
return responseType === "json" ? "application/json" : "text/plain";
|
|
216
|
-
}
|
|
217
|
-
function redactedUrl(url) {
|
|
218
|
-
const parsed = url instanceof URL ? url : new URL(url);
|
|
219
|
-
return `${parsed.origin}${parsed.pathname}`;
|
|
66
|
+
};
|
|
220
67
|
}
|
|
221
68
|
//#endregion
|
|
222
|
-
//#region src/
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
return
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
}
|
|
230
|
-
async function loadViteEnv(rootDir) {
|
|
231
|
-
if (viteLoadEnv === null) return;
|
|
232
|
-
if (!viteLoadEnv) try {
|
|
233
|
-
viteLoadEnv = (await import(
|
|
234
|
-
/* @vite-ignore */
|
|
235
|
-
"vite"
|
|
236
|
-
)).loadEnv;
|
|
237
|
-
} catch {
|
|
238
|
-
viteLoadEnv = null;
|
|
239
|
-
return;
|
|
240
|
-
}
|
|
241
|
-
return viteLoadEnv("", rootDir, "");
|
|
69
|
+
//#region src/sources/markdown.ts
|
|
70
|
+
function markdown(options) {
|
|
71
|
+
const key = normalizeSafeWorkspacePath(options.workspacePath || options.path || "");
|
|
72
|
+
return {
|
|
73
|
+
...withWorkspaceRuntimeOptions(markdown$1(options), options),
|
|
74
|
+
probeKeys: options.probeKeys || [key]
|
|
75
|
+
};
|
|
242
76
|
}
|
|
243
77
|
//#endregion
|
|
244
|
-
//#region src/sources/
|
|
245
|
-
function
|
|
246
|
-
const
|
|
78
|
+
//#region src/sources/mcp-resources.ts
|
|
79
|
+
function mcpResources(options) {
|
|
80
|
+
const livePaths = {};
|
|
81
|
+
const baseSource = mcpResources$1({
|
|
247
82
|
...options,
|
|
248
|
-
|
|
249
|
-
};
|
|
250
|
-
const baseSource = github({
|
|
251
|
-
...resolvedOptions,
|
|
252
|
-
auth: createGitHubAuthResolver(resolvedOptions.auth)
|
|
83
|
+
cache: options.cache
|
|
253
84
|
});
|
|
254
|
-
|
|
255
|
-
async function getSourceForRoot(rootDir) {
|
|
256
|
-
const envFileToken = await resolveWorkspaceEnv(rootDir, "GITHUB_TOKEN");
|
|
257
|
-
const cacheKey = `${rootDir}\0${envFileToken ?? ""}`;
|
|
258
|
-
const cachedSource = sourceByRootAndToken.get(cacheKey);
|
|
259
|
-
if (cachedSource) return cachedSource;
|
|
260
|
-
const source = github({
|
|
261
|
-
...resolvedOptions,
|
|
262
|
-
auth: createGitHubAuthResolver(resolvedOptions.auth, envFileToken)
|
|
263
|
-
});
|
|
264
|
-
sourceByRootAndToken.set(cacheKey, source);
|
|
265
|
-
return source;
|
|
266
|
-
}
|
|
267
|
-
return {
|
|
85
|
+
return markLiveWorkspaceSource(withWorkspaceRuntimeOptions({
|
|
268
86
|
...baseSource,
|
|
269
|
-
cache: resolvedOptions.cache,
|
|
270
|
-
fingerprint: {
|
|
271
|
-
exclude: resolvedOptions.exclude,
|
|
272
|
-
include: resolvedOptions.include,
|
|
273
|
-
ref: resolvedOptions.ref,
|
|
274
|
-
repo: resolvedOptions.repo,
|
|
275
|
-
root: resolvedOptions.root
|
|
276
|
-
},
|
|
277
|
-
materialize: resolvedOptions.materialize,
|
|
278
|
-
mount: resolvedOptions.mount,
|
|
279
|
-
validate: resolvedOptions.validate,
|
|
280
87
|
async prepare(ctx) {
|
|
281
|
-
await
|
|
282
|
-
|
|
283
|
-
async getKeys(ctx) {
|
|
284
|
-
return await (await getSourceForRoot(ctx.rootDir)).getKeys(ctx);
|
|
285
|
-
},
|
|
286
|
-
async getItem(key, ctx) {
|
|
287
|
-
return await (await getSourceForRoot(ctx.rootDir)).getItem(key, ctx);
|
|
288
|
-
},
|
|
289
|
-
async getItems(ctx) {
|
|
290
|
-
const source = await getSourceForRoot(ctx.rootDir);
|
|
291
|
-
return await source.getItems?.(ctx) ?? await Promise.all((await source.getKeys(ctx)).map((key) => source.getItem(key, ctx)));
|
|
292
|
-
},
|
|
293
|
-
async getMeta(key, ctx) {
|
|
294
|
-
return await (await getSourceForRoot(ctx.rootDir)).getMeta?.(key, ctx);
|
|
295
|
-
},
|
|
296
|
-
async search(query, ctx) {
|
|
297
|
-
return await (await getSourceForRoot(ctx.rootDir)).search?.(query, ctx) ?? [];
|
|
88
|
+
await baseSource.prepare?.(ctx);
|
|
89
|
+
resetLivePaths(livePaths, resolveMountPath(options.mount, ctx), await baseSource.getKeys(ctx));
|
|
298
90
|
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
function glob$1(options) {
|
|
315
|
-
return {
|
|
316
|
-
...glob({
|
|
317
|
-
...options,
|
|
318
|
-
keyCache: options.keyCache ?? !isLazySource(options)
|
|
319
|
-
}),
|
|
320
|
-
cache: options.cache,
|
|
321
|
-
materialize: options.materialize,
|
|
322
|
-
mount: options.mount,
|
|
323
|
-
validate: options.validate
|
|
324
|
-
};
|
|
325
|
-
}
|
|
326
|
-
function isLazySource(options) {
|
|
327
|
-
if (options.materialize === "lazy") return true;
|
|
328
|
-
return typeof options.mount === "object" && options.mount?.materialize === "lazy";
|
|
329
|
-
}
|
|
330
|
-
//#endregion
|
|
331
|
-
//#region src/sources/markdown.ts
|
|
332
|
-
function markdown$1(options) {
|
|
333
|
-
return {
|
|
334
|
-
...markdown(options),
|
|
335
|
-
cache: options.cache,
|
|
336
|
-
materialize: options.materialize,
|
|
337
|
-
mount: options.mount,
|
|
338
|
-
validate: options.validate
|
|
339
|
-
};
|
|
91
|
+
}, {
|
|
92
|
+
...options,
|
|
93
|
+
materialize: options.materialize || (options.sync ? "none" : "lazy")
|
|
94
|
+
}), livePaths);
|
|
95
|
+
}
|
|
96
|
+
registerMcpResourcesSourceLoader((input) => mcpResources(input));
|
|
97
|
+
function resolveMountPath(mount, ctx) {
|
|
98
|
+
return normalizeSafeWorkspacePath((typeof mount === "string" ? mount : mount?.path) ?? ctx.mountPath ?? ctx.source ?? "", { allowEmpty: true });
|
|
99
|
+
}
|
|
100
|
+
function resetLivePaths(paths, mountPath, keys) {
|
|
101
|
+
for (const path of Object.keys(paths)) delete paths[path];
|
|
102
|
+
for (const key of keys) {
|
|
103
|
+
const path = normalizeSafeWorkspacePath(`${mountPath}/${key}`.replace(/\/+/g, "/"), { allowEmpty: false });
|
|
104
|
+
paths[path] = key;
|
|
105
|
+
}
|
|
340
106
|
}
|
|
341
107
|
//#endregion
|
|
342
108
|
//#region src/sources/index.ts
|
|
343
109
|
var sources_exports = /* @__PURE__ */ __exportAll({
|
|
344
110
|
custom: () => custom,
|
|
345
|
-
fetch: () => fetch
|
|
346
|
-
file: () => file
|
|
347
|
-
github: () => github
|
|
348
|
-
glob: () => glob
|
|
349
|
-
markdown: () => markdown
|
|
111
|
+
fetch: () => fetch,
|
|
112
|
+
file: () => file,
|
|
113
|
+
github: () => github,
|
|
114
|
+
glob: () => glob,
|
|
115
|
+
markdown: () => markdown,
|
|
116
|
+
mcpResources: () => mcpResources
|
|
350
117
|
});
|
|
351
118
|
//#endregion
|
|
352
|
-
//#region src/
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
function
|
|
357
|
-
return
|
|
358
|
-
}
|
|
359
|
-
function
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
function
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
});
|
|
409
|
-
},
|
|
410
|
-
async stat(path) {
|
|
411
|
-
return await (await resolveSyncedWorkspace()).stat(normalizePath(path));
|
|
412
|
-
},
|
|
413
|
-
async exists(path) {
|
|
414
|
-
return await (await resolveSyncedWorkspace()).exists(normalizePath(path));
|
|
415
|
-
},
|
|
416
|
-
async mkdir(path, options) {
|
|
417
|
-
await (await resolveSyncedWorkspace()).mkdir(normalizePath(path), options);
|
|
418
|
-
},
|
|
419
|
-
async rm(path, options) {
|
|
420
|
-
await (await resolveSyncedWorkspace()).rm(normalizePath(path), options);
|
|
421
|
-
},
|
|
422
|
-
async materializeSources(options) {
|
|
423
|
-
return await (await resolveSyncedWorkspace()).materializeSources?.(options) ?? {
|
|
424
|
-
bytes: 0,
|
|
425
|
-
directories: 0,
|
|
426
|
-
durationMs: 0,
|
|
427
|
-
files: 0,
|
|
428
|
-
path: options?.path || "",
|
|
429
|
-
sources: []
|
|
430
|
-
};
|
|
431
|
-
},
|
|
432
|
-
async snapshot(options) {
|
|
433
|
-
return await (await resolveSyncedWorkspace()).snapshot(options);
|
|
434
|
-
},
|
|
435
|
-
async diff(options) {
|
|
436
|
-
return await (await resolveSyncedWorkspace()).diff(options);
|
|
437
|
-
},
|
|
438
|
-
async startSession(options) {
|
|
439
|
-
return await (await resolveSyncedWorkspace()).startSession(options);
|
|
440
|
-
},
|
|
441
|
-
mount(options) {
|
|
442
|
-
return {
|
|
443
|
-
workspace,
|
|
444
|
-
mode: options?.mode || "read-only",
|
|
445
|
-
target: options?.target || "/workspace",
|
|
446
|
-
async diff() {
|
|
447
|
-
return await (await resolveSyncedWorkspace()).mount(options).diff();
|
|
448
|
-
},
|
|
449
|
-
async commit(commitOptions) {
|
|
450
|
-
await (await resolveSyncedWorkspace()).mount(options).commit(commitOptions);
|
|
451
|
-
},
|
|
452
|
-
async export() {
|
|
453
|
-
return await (await resolveSyncedWorkspace()).mount(options).export();
|
|
454
|
-
}
|
|
455
|
-
};
|
|
456
|
-
}
|
|
457
|
-
};
|
|
458
|
-
return workspace;
|
|
459
|
-
}
|
|
460
|
-
function createWritableFs(workspace) {
|
|
461
|
-
return {
|
|
462
|
-
readFile: async (path, options) => await workspace.readFile(normalizePath(path), options),
|
|
463
|
-
writeFile: async (path, content, options) => await workspace.writeFile(normalizePath(path), content, options),
|
|
464
|
-
appendFile: async (path, content) => await appendWorkspaceFile(workspace, normalizePath(path), content),
|
|
465
|
-
stat: async (path) => await workspace.stat(normalizePath(path)),
|
|
466
|
-
exists: async (path) => await workspace.exists(normalizePath(path)),
|
|
467
|
-
list: async (path, options) => await workspace.list(path ? normalizeListPath(path) : "", options),
|
|
468
|
-
glob: async (pattern, options) => await workspace.glob(normalizePattern(pattern), options),
|
|
469
|
-
search: async (query) => await workspace.search({
|
|
470
|
-
...query,
|
|
471
|
-
cwd: query.cwd ? normalizeListPath(query.cwd) : query.cwd,
|
|
472
|
-
paths: query.paths?.map((path) => normalizeListPath(path))
|
|
473
|
-
}),
|
|
474
|
-
mkdir: async (path, options) => await workspace.mkdir(normalizePath(path), options),
|
|
475
|
-
rm: async (path, options) => await workspace.rm(normalizePath(path), options),
|
|
476
|
-
movePath: async (from, to, options) => {
|
|
477
|
-
const source = normalizePath(from);
|
|
478
|
-
await copyWorkspacePath(workspace, source, normalizePath(to), options?.overwrite);
|
|
479
|
-
await workspace.rm(source, {
|
|
480
|
-
recursive: true,
|
|
481
|
-
force: true
|
|
482
|
-
});
|
|
483
|
-
},
|
|
484
|
-
copyPath: async (from, to, options) => await copyWorkspacePath(workspace, normalizePath(from), normalizePath(to), options?.overwrite)
|
|
485
|
-
};
|
|
486
|
-
}
|
|
487
|
-
function useOptionalWorkspaceAssets(name) {
|
|
119
|
+
//#region src/session/harness.ts
|
|
120
|
+
const archivePath = ".vitehub-workspace.tar.gz";
|
|
121
|
+
const tarBlockSize = 512;
|
|
122
|
+
const workspaceReadConcurrency = 16;
|
|
123
|
+
function isWritableWorkspaceFacade(workspace) {
|
|
124
|
+
return "startSession" in workspace && typeof workspace.startSession === "function";
|
|
125
|
+
}
|
|
126
|
+
function workspaceSourceMaterializer(workspace) {
|
|
127
|
+
if ("materializeSources" in workspace && typeof workspace.materializeSources === "function") return (options) => workspace.materializeSources(options);
|
|
128
|
+
const materializeSources = workspace.fs.materializeSources;
|
|
129
|
+
if (typeof materializeSources === "function") return (options) => materializeSources(options);
|
|
130
|
+
}
|
|
131
|
+
function sandboxPath(root, path) {
|
|
132
|
+
return posix.join(root, path);
|
|
133
|
+
}
|
|
134
|
+
function shellQuote(value) {
|
|
135
|
+
return `'${value.replace(/'/g, "'\\''")}'`;
|
|
136
|
+
}
|
|
137
|
+
function isGeneratedSourceDescriptorPath(path) {
|
|
138
|
+
return path === ".vitehub" || path === ".vitehub/sources" || /^\.vitehub\/sources\/[^/]+\.json$/.test(path);
|
|
139
|
+
}
|
|
140
|
+
function normalizeHarnessWorkspacePath(path = "") {
|
|
141
|
+
const descriptorPath = normalizeSafeWorkspacePath(path, {
|
|
142
|
+
allowEmpty: true,
|
|
143
|
+
allowReserved: true
|
|
144
|
+
});
|
|
145
|
+
if (isGeneratedSourceDescriptorPath(descriptorPath)) return descriptorPath;
|
|
146
|
+
return normalizeSafeWorkspacePath(path, { allowEmpty: true });
|
|
147
|
+
}
|
|
148
|
+
function normalizeSessionPaths(paths) {
|
|
149
|
+
if (paths === void 0) return void 0;
|
|
150
|
+
const normalized = [...new Set((paths || []).map(normalizeHarnessWorkspacePath))];
|
|
151
|
+
if (normalized.includes("")) return void 0;
|
|
152
|
+
return normalized.sort((left, right) => left.length - right.length || left.localeCompare(right));
|
|
153
|
+
}
|
|
154
|
+
function isIgnoredWriteBackPath(path, ignoreWriteBackPaths) {
|
|
155
|
+
for (const ignoredPath of ignoreWriteBackPaths) if (path === ignoredPath || path.startsWith(`${ignoredPath}/`)) return true;
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
function isIgnoredWriteBackParent(path, ignoreWriteBackPaths) {
|
|
159
|
+
for (const ignoredPath of ignoreWriteBackPaths) if (ignoredPath.startsWith(`${path}/`)) return true;
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
async function runSandbox(sandbox, options) {
|
|
163
|
+
const result = await sandbox.run(options);
|
|
164
|
+
if (result.exitCode !== 0) throw new Error(`[vitehub] Failed to prepare Harness Workspace Session: ${result.stderr || "sandbox command failed"}`);
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
async function withPrepareProgress(onProgress, event, fn) {
|
|
168
|
+
const startedAt = Date.now();
|
|
169
|
+
await onProgress?.({
|
|
170
|
+
data: event.data,
|
|
171
|
+
id: event.id,
|
|
172
|
+
label: event.label,
|
|
173
|
+
status: "started"
|
|
174
|
+
});
|
|
488
175
|
try {
|
|
489
|
-
|
|
176
|
+
const result = await fn();
|
|
177
|
+
await onProgress?.({
|
|
178
|
+
data: event.data,
|
|
179
|
+
durationMs: Date.now() - startedAt,
|
|
180
|
+
id: event.id,
|
|
181
|
+
label: event.label,
|
|
182
|
+
status: "completed"
|
|
183
|
+
});
|
|
184
|
+
return result;
|
|
490
185
|
} catch (error) {
|
|
491
|
-
|
|
186
|
+
await onProgress?.({
|
|
187
|
+
data: {
|
|
188
|
+
...event.data,
|
|
189
|
+
error: error instanceof Error ? error.message : String(error)
|
|
190
|
+
},
|
|
191
|
+
durationMs: Date.now() - startedAt,
|
|
192
|
+
error: error instanceof Error ? error.message : String(error),
|
|
193
|
+
id: event.id,
|
|
194
|
+
label: event.label,
|
|
195
|
+
status: "failed"
|
|
196
|
+
});
|
|
492
197
|
throw error;
|
|
493
198
|
}
|
|
494
199
|
}
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
200
|
+
function workspaceFiles(entries) {
|
|
201
|
+
return entries.filter((entry) => entry.type === "file").sort((left, right) => left.path.localeCompare(right.path));
|
|
202
|
+
}
|
|
203
|
+
function workspaceDirectories(entries) {
|
|
204
|
+
return entries.filter((entry) => entry.type === "directory").map((entry) => entry.path).sort((left, right) => left.localeCompare(right));
|
|
205
|
+
}
|
|
206
|
+
function addParentDirectories(directories, path) {
|
|
207
|
+
let directory = posix.dirname(path);
|
|
208
|
+
while (directory && directory !== ".") {
|
|
209
|
+
directories.add(directory);
|
|
210
|
+
directory = posix.dirname(directory);
|
|
501
211
|
}
|
|
502
212
|
}
|
|
503
|
-
function
|
|
504
|
-
|
|
213
|
+
async function forEachConcurrent(items, limit, fn) {
|
|
214
|
+
let index = 0;
|
|
215
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, async () => {
|
|
216
|
+
while (index < items.length) {
|
|
217
|
+
const item = items[index++];
|
|
218
|
+
if (item !== void 0) await fn(item);
|
|
219
|
+
}
|
|
220
|
+
}));
|
|
505
221
|
}
|
|
506
|
-
function
|
|
222
|
+
async function workspaceEntries(workspace, paths) {
|
|
223
|
+
if (paths && !paths.length) return [];
|
|
224
|
+
if (!paths) return await workspace.fs.list("", { recursive: true });
|
|
507
225
|
const entries = /* @__PURE__ */ new Map();
|
|
508
|
-
for (const
|
|
509
|
-
|
|
226
|
+
for (const path of paths) {
|
|
227
|
+
const stat = await workspace.fs.stat(path).catch(() => void 0);
|
|
228
|
+
if (!stat) continue;
|
|
229
|
+
entries.set(stat.path, stat);
|
|
230
|
+
if (stat.type === "directory") for (const entry of await workspace.fs.list(path, { recursive: true })) entries.set(entry.path, entry);
|
|
231
|
+
}
|
|
510
232
|
return [...entries.values()].sort((left, right) => left.path.localeCompare(right.path));
|
|
511
233
|
}
|
|
512
|
-
function
|
|
513
|
-
const
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
234
|
+
async function materializeWorkspaceSourcesForSession(workspace, paths, options = {}) {
|
|
235
|
+
const materialize = workspaceSourceMaterializer(workspace);
|
|
236
|
+
if (!materialize) return;
|
|
237
|
+
if (paths && !paths.length) return;
|
|
238
|
+
await Promise.all((paths || [""]).map(async (path) => {
|
|
239
|
+
await materialize({
|
|
240
|
+
...options,
|
|
241
|
+
path
|
|
242
|
+
}).catch((error) => {
|
|
243
|
+
if (path && isMissingWorkspacePathError(error)) return;
|
|
244
|
+
throw error;
|
|
245
|
+
});
|
|
246
|
+
}));
|
|
247
|
+
}
|
|
248
|
+
function bytesEqual(left, right) {
|
|
249
|
+
if (!left || left.byteLength !== right.byteLength) return false;
|
|
250
|
+
for (let index = 0; index < left.byteLength; index++) if (left[index] !== right[index]) return false;
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
function pathsFromFindOutput(stdout) {
|
|
254
|
+
return stdout.split("\n").map((path) => path.trim()).filter(Boolean).map((path) => path.replace(/^\.\//, "")).filter((path) => path && path !== ".").sort((left, right) => left.localeCompare(right));
|
|
255
|
+
}
|
|
256
|
+
async function resetSandboxWorkDir(sandbox, sessionWorkDir, abortSignal) {
|
|
257
|
+
const workDir = posix.normalize(sessionWorkDir);
|
|
258
|
+
const parent = posix.dirname(workDir);
|
|
259
|
+
const name = posix.basename(workDir);
|
|
260
|
+
await sandbox.writeBinaryFile({
|
|
261
|
+
abortSignal,
|
|
262
|
+
content: new Uint8Array(),
|
|
263
|
+
path: sandboxPath(parent, ".vitehub-reset")
|
|
264
|
+
});
|
|
265
|
+
await runSandbox(sandbox, {
|
|
266
|
+
abortSignal,
|
|
267
|
+
command: `rm -rf -- ${shellQuote(name)} && mkdir -p -- ${shellQuote(name)} && rm -f -- ${shellQuote(".vitehub-reset")}`,
|
|
268
|
+
workingDirectory: parent
|
|
519
269
|
});
|
|
520
270
|
}
|
|
521
|
-
function
|
|
522
|
-
const
|
|
523
|
-
return
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
271
|
+
function paddedTarContent(content) {
|
|
272
|
+
const padding = content.byteLength % tarBlockSize ? tarBlockSize - content.byteLength % tarBlockSize : 0;
|
|
273
|
+
if (!padding) return Buffer.from(content);
|
|
274
|
+
return Buffer.concat([Buffer.from(content), Buffer.alloc(padding)]);
|
|
275
|
+
}
|
|
276
|
+
function splitTarPath(path) {
|
|
277
|
+
if (Buffer.byteLength(path) <= 100) return { name: path };
|
|
278
|
+
const parts = path.split("/");
|
|
279
|
+
for (let index = 1; index < parts.length; index++) {
|
|
280
|
+
const prefix = parts.slice(0, index).join("/");
|
|
281
|
+
const name = parts.slice(index).join("/");
|
|
282
|
+
if (Buffer.byteLength(prefix) <= 155 && Buffer.byteLength(name) <= 100) return {
|
|
283
|
+
name,
|
|
284
|
+
prefix
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
throw new Error(`[vitehub] Harness Workspace Session path is too long for tar transfer: ${path}`);
|
|
288
|
+
}
|
|
289
|
+
function writeTarString(header, offset, length, value) {
|
|
290
|
+
const bytes = Buffer.from(value);
|
|
291
|
+
bytes.copy(header, offset, 0, Math.min(bytes.byteLength, length));
|
|
292
|
+
}
|
|
293
|
+
function writeTarOctal(header, offset, length, value) {
|
|
294
|
+
const text = value.toString(8).padStart(length - 1, "0").slice(-(length - 1));
|
|
295
|
+
header.write(text, offset, length - 1, "ascii");
|
|
296
|
+
header[offset + length - 1] = 0;
|
|
297
|
+
}
|
|
298
|
+
function tarHeader(path, options) {
|
|
299
|
+
const header = Buffer.alloc(tarBlockSize);
|
|
300
|
+
const { name, prefix } = splitTarPath(path);
|
|
301
|
+
if (options.linkName && Buffer.byteLength(options.linkName) > 100) throw new Error(`[vitehub] Harness Workspace Session symlink target is too long for tar transfer: ${path}`);
|
|
302
|
+
writeTarString(header, 0, 100, name);
|
|
303
|
+
writeTarOctal(header, 100, 8, options.mode);
|
|
304
|
+
writeTarOctal(header, 108, 8, 0);
|
|
305
|
+
writeTarOctal(header, 116, 8, 0);
|
|
306
|
+
writeTarOctal(header, 124, 12, options.size);
|
|
307
|
+
writeTarOctal(header, 136, 12, 0);
|
|
308
|
+
header.fill(32, 148, 156);
|
|
309
|
+
header[156] = options.type === "directory" ? 53 : options.type === "symlink" ? 50 : 48;
|
|
310
|
+
if (options.linkName) writeTarString(header, 157, 100, options.linkName);
|
|
311
|
+
writeTarString(header, 257, 6, "ustar");
|
|
312
|
+
writeTarString(header, 263, 2, "00");
|
|
313
|
+
if (prefix) writeTarString(header, 345, 155, prefix);
|
|
314
|
+
let checksum = 0;
|
|
315
|
+
for (const byte of header) checksum += byte;
|
|
316
|
+
header.write(checksum.toString(8).padStart(6, "0"), 148, 6, "ascii");
|
|
317
|
+
header[154] = 0;
|
|
318
|
+
header[155] = 32;
|
|
319
|
+
return header;
|
|
320
|
+
}
|
|
321
|
+
function createWorkspaceTarGz(directories, files) {
|
|
322
|
+
const blocks = [];
|
|
323
|
+
for (const directory of [...directories].sort((left, right) => left.localeCompare(right))) {
|
|
324
|
+
const path = directory.endsWith("/") ? directory : `${directory}/`;
|
|
325
|
+
blocks.push(tarHeader(path, {
|
|
326
|
+
mode: 493,
|
|
327
|
+
size: 0,
|
|
328
|
+
type: "directory"
|
|
329
|
+
}));
|
|
330
|
+
}
|
|
331
|
+
for (const [path, file] of [...files.entries()].sort(([left], [right]) => left.localeCompare(right))) {
|
|
332
|
+
if (file.symlinkTarget) {
|
|
333
|
+
blocks.push(tarHeader(path, {
|
|
334
|
+
linkName: file.symlinkTarget,
|
|
335
|
+
mode: 511,
|
|
336
|
+
size: 0,
|
|
337
|
+
type: "symlink"
|
|
338
|
+
}));
|
|
339
|
+
continue;
|
|
577
340
|
}
|
|
578
|
-
|
|
341
|
+
blocks.push(tarHeader(path, {
|
|
342
|
+
mode: 420,
|
|
343
|
+
size: file.content.byteLength,
|
|
344
|
+
type: "file"
|
|
345
|
+
}));
|
|
346
|
+
blocks.push(paddedTarContent(file.content));
|
|
347
|
+
}
|
|
348
|
+
blocks.push(Buffer.alloc(tarBlockSize * 2));
|
|
349
|
+
return gzipSync(Buffer.concat(blocks));
|
|
350
|
+
}
|
|
351
|
+
async function extractWorkspaceArchive(sandbox, sessionWorkDir, initialTree, directories, abortSignal) {
|
|
352
|
+
if (!initialTree.files.size && !directories.size) return;
|
|
353
|
+
const path = sandboxPath(sessionWorkDir, archivePath);
|
|
354
|
+
await sandbox.writeBinaryFile({
|
|
355
|
+
abortSignal,
|
|
356
|
+
content: createWorkspaceTarGz(directories, initialTree.files),
|
|
357
|
+
path
|
|
358
|
+
});
|
|
359
|
+
await runSandbox(sandbox, {
|
|
360
|
+
abortSignal,
|
|
361
|
+
command: `tar -xzf ${shellQuote(archivePath)} && rm ${shellQuote(archivePath)}`,
|
|
362
|
+
workingDirectory: sessionWorkDir
|
|
363
|
+
});
|
|
579
364
|
}
|
|
580
|
-
function
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
};
|
|
365
|
+
async function initializeSandboxGitBaseline(sandbox, sessionWorkDir, abortSignal) {
|
|
366
|
+
await runSandbox(sandbox, {
|
|
367
|
+
abortSignal,
|
|
368
|
+
command: "if command -v git >/dev/null 2>&1; then git init -q && git config user.email vitehub@example.invalid && git config user.name ViteHub && git add -A -f && git commit --allow-empty --no-gpg-sign --no-verify -qm workspace-baseline || true; fi",
|
|
369
|
+
workingDirectory: sessionWorkDir
|
|
370
|
+
});
|
|
587
371
|
}
|
|
588
|
-
function
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
372
|
+
async function listSandboxPaths(sandbox, sessionWorkDir, type, abortSignal) {
|
|
373
|
+
const result = await runSandbox(sandbox, {
|
|
374
|
+
abortSignal,
|
|
375
|
+
command: `find . -type ${type} -print`,
|
|
376
|
+
workingDirectory: sessionWorkDir
|
|
377
|
+
});
|
|
378
|
+
return new Set(pathsFromFindOutput(result.stdout));
|
|
379
|
+
}
|
|
380
|
+
async function readSandboxSymlinkTarget(sandbox, sessionWorkDir, path, abortSignal) {
|
|
381
|
+
return (await runSandbox(sandbox, {
|
|
382
|
+
abortSignal,
|
|
383
|
+
command: `readlink -- ${shellQuote(path)}`,
|
|
384
|
+
workingDirectory: sessionWorkDir
|
|
385
|
+
})).stdout.replace(/\n$/, "");
|
|
386
|
+
}
|
|
387
|
+
async function copyWorkspaceToSandbox(workspace, sandbox, sessionWorkDir, abortSignal, paths, onMaterializeProgress, onProgress) {
|
|
388
|
+
await withPrepareProgress(onProgress, {
|
|
389
|
+
data: { paths: paths ?? null },
|
|
390
|
+
id: "workspace.prepare.materialize",
|
|
391
|
+
label: "Materializing workspace sources"
|
|
392
|
+
}, async () => {
|
|
393
|
+
await materializeWorkspaceSourcesForSession(workspace, paths, {
|
|
394
|
+
...abortSignal ? { abortSignal } : {},
|
|
395
|
+
...onMaterializeProgress ? { onProgress: onMaterializeProgress } : {}
|
|
396
|
+
});
|
|
397
|
+
});
|
|
398
|
+
const entries = await withPrepareProgress(onProgress, {
|
|
399
|
+
data: { paths: paths ?? null },
|
|
400
|
+
id: "workspace.prepare.entries",
|
|
401
|
+
label: "Resolving workspace entries"
|
|
402
|
+
}, async () => await workspaceEntries(workspace, paths));
|
|
403
|
+
const directoriesToCreate = new Set(workspaceDirectories(entries));
|
|
404
|
+
const initialTree = {
|
|
405
|
+
archiveDirectories: directoriesToCreate,
|
|
406
|
+
directories: new Set(workspaceDirectories(entries)),
|
|
407
|
+
files: /* @__PURE__ */ new Map()
|
|
599
408
|
};
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
maxOutputLength: opts?.maxOutputLength,
|
|
615
|
-
operations: toWriteOperations(opts),
|
|
616
|
-
timeout: opts?.timeout
|
|
409
|
+
const files = workspaceFiles(entries);
|
|
410
|
+
for (const entry of files) addParentDirectories(directoriesToCreate, entry.path);
|
|
411
|
+
await withPrepareProgress(onProgress, {
|
|
412
|
+
data: { files: files.length },
|
|
413
|
+
id: "workspace.prepare.read-files",
|
|
414
|
+
label: "Reading workspace files"
|
|
415
|
+
}, async () => {
|
|
416
|
+
await forEachConcurrent(files, workspaceReadConcurrency, async (entry) => {
|
|
417
|
+
const bytes = contentToBytes(await workspace.fs.readFile(entry.path, { encoding: "binary" }));
|
|
418
|
+
initialTree.files.set(entry.path, {
|
|
419
|
+
content: bytes,
|
|
420
|
+
mediaType: entry.mediaType,
|
|
421
|
+
symlinkTarget: entry.metadata?.gitMode === "120000" ? typeof entry.metadata.symlinkTarget === "string" ? entry.metadata.symlinkTarget : new TextDecoder().decode(bytes) : void 0
|
|
422
|
+
});
|
|
617
423
|
});
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
424
|
+
});
|
|
425
|
+
await withPrepareProgress(onProgress, {
|
|
426
|
+
id: "workspace.prepare.reset-sandbox",
|
|
427
|
+
label: "Resetting sandbox workspace"
|
|
428
|
+
}, async () => {
|
|
429
|
+
await resetSandboxWorkDir(sandbox, sessionWorkDir, abortSignal);
|
|
430
|
+
});
|
|
431
|
+
await withPrepareProgress(onProgress, {
|
|
432
|
+
data: {
|
|
433
|
+
directories: directoriesToCreate.size,
|
|
434
|
+
files: initialTree.files.size
|
|
435
|
+
},
|
|
436
|
+
id: "workspace.prepare.extract-archive",
|
|
437
|
+
label: "Extracting workspace archive"
|
|
438
|
+
}, async () => {
|
|
439
|
+
await extractWorkspaceArchive(sandbox, sessionWorkDir, initialTree, directoriesToCreate, abortSignal);
|
|
440
|
+
});
|
|
441
|
+
await withPrepareProgress(onProgress, {
|
|
442
|
+
id: "workspace.prepare.git-status",
|
|
443
|
+
label: "Preparing workspace status"
|
|
444
|
+
}, async () => {
|
|
445
|
+
await initializeSandboxGitBaseline(sandbox, sessionWorkDir, abortSignal);
|
|
446
|
+
});
|
|
447
|
+
return initialTree;
|
|
448
|
+
}
|
|
449
|
+
async function copySandboxChangesToWorkspace(session, definition, sandbox, sessionWorkDir, initialTree, abortSignal, ignoreWriteBackPaths, commit, onWriteBack) {
|
|
450
|
+
if (!sandbox.readBinaryFile) throw new Error("[vitehub] Harness Workspace Session write mode requires sandbox.readBinaryFile.");
|
|
451
|
+
const ignoredWriteBackPaths = new Set([...ignoreWriteBackPaths, ".git"]);
|
|
452
|
+
const sandboxDirectories = await listSandboxPaths(sandbox, sessionWorkDir, "d", abortSignal);
|
|
453
|
+
const sandboxFiles = await listSandboxPaths(sandbox, sessionWorkDir, "f", abortSignal);
|
|
454
|
+
const sandboxSymlinks = await listSandboxPaths(sandbox, sessionWorkDir, "l", abortSignal);
|
|
455
|
+
const sandboxFileEntries = new Set([...sandboxFiles, ...sandboxSymlinks]);
|
|
456
|
+
for (const path of initialTree.files.keys()) {
|
|
457
|
+
if (isIgnoredWriteBackPath(path, ignoredWriteBackPaths)) continue;
|
|
458
|
+
if (!sandboxFileEntries.has(path)) await session.rm(path, { force: true });
|
|
459
|
+
}
|
|
460
|
+
for (const path of [...initialTree.directories].sort((left, right) => right.length - left.length)) {
|
|
461
|
+
if (isIgnoredWriteBackPath(path, ignoredWriteBackPaths)) continue;
|
|
462
|
+
if (isIgnoredWriteBackParent(path, ignoredWriteBackPaths)) continue;
|
|
463
|
+
if (!sandboxDirectories.has(path)) await session.rm(path, {
|
|
464
|
+
force: true,
|
|
465
|
+
recursive: true
|
|
625
466
|
});
|
|
626
|
-
const tools = createDefaultToolSet(createTools);
|
|
627
|
-
tools.inspect = createReadTools;
|
|
628
|
-
tools.write = createTools;
|
|
629
|
-
tools.none = emptyTools;
|
|
630
|
-
return {
|
|
631
|
-
diff: async (options) => await workspace.diff(options),
|
|
632
|
-
fs: createWritableFs(workspace),
|
|
633
|
-
snapshot: async (options) => await workspace.snapshot(options),
|
|
634
|
-
startSession: async (options) => await workspace.startSession(options),
|
|
635
|
-
tools
|
|
636
|
-
};
|
|
637
467
|
}
|
|
638
|
-
const
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
468
|
+
for (const path of sandboxDirectories) {
|
|
469
|
+
if (isIgnoredWriteBackPath(path, ignoredWriteBackPaths)) continue;
|
|
470
|
+
if (!initialTree.directories.has(path) && isIgnoredWriteBackParent(path, ignoredWriteBackPaths)) continue;
|
|
471
|
+
if (initialTree.archiveDirectories.has(path)) continue;
|
|
472
|
+
await session.mkdir(path, { recursive: true });
|
|
473
|
+
}
|
|
474
|
+
for (const path of sandboxFiles) {
|
|
475
|
+
if (isIgnoredWriteBackPath(path, ignoredWriteBackPaths)) continue;
|
|
476
|
+
const initial = initialTree.files.get(path);
|
|
477
|
+
const content = await sandbox.readBinaryFile({
|
|
478
|
+
abortSignal,
|
|
479
|
+
path: sandboxPath(sessionWorkDir, path)
|
|
480
|
+
});
|
|
481
|
+
if (!content || !initial?.symlinkTarget && bytesEqual(initial?.content, content)) continue;
|
|
482
|
+
await session.writeFile(path, content, { mediaType: initial?.mediaType || lookup(path) || void 0 });
|
|
483
|
+
}
|
|
484
|
+
for (const path of sandboxSymlinks) {
|
|
485
|
+
if (isIgnoredWriteBackPath(path, ignoredWriteBackPaths)) continue;
|
|
486
|
+
const initial = initialTree.files.get(path);
|
|
487
|
+
const target = await readSandboxSymlinkTarget(sandbox, sessionWorkDir, path, abortSignal);
|
|
488
|
+
if (initial?.symlinkTarget === target) continue;
|
|
489
|
+
await session.writeFile(path, contentToBytes(target), { metadata: { gitMode: "120000" } });
|
|
490
|
+
}
|
|
491
|
+
const diff = await session.diff();
|
|
492
|
+
if (!diff.entries.length) return;
|
|
493
|
+
if (commit) {
|
|
494
|
+
const commitOptions = commit(diff);
|
|
495
|
+
if (!commitOptions) return;
|
|
496
|
+
await session.commit({ message: commitOptions.message || "harness-workspace-session" });
|
|
497
|
+
await onWriteBack?.(diff);
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
const autoCommit = definition ? resolveWorkspaceAutoCommit(definition, diff) : void 0;
|
|
501
|
+
if (definition && !autoCommit) return;
|
|
502
|
+
await session.commit({ message: autoCommit?.message || "harness-workspace-session" });
|
|
503
|
+
await onWriteBack?.(diff);
|
|
504
|
+
}
|
|
505
|
+
async function prepareHarnessWorkspaceSession(workspace, options) {
|
|
506
|
+
const paths = normalizeSessionPaths(options.paths);
|
|
507
|
+
const ignoreWriteBackPaths = new Set((options.ignoreWriteBackPaths || []).map(normalizeHarnessWorkspacePath));
|
|
508
|
+
const initialTree = await copyWorkspaceToSandbox(workspace, options.session, options.sessionWorkDir, options.abortSignal, paths, options.onMaterializeProgress, options.onProgress);
|
|
509
|
+
const sessionOptions = paths ? { paths } : void 0;
|
|
510
|
+
const workspaceSession = isWritableWorkspaceFacade(workspace) && (paths === void 0 || paths.length) ? await withPrepareProgress(options.onProgress, {
|
|
511
|
+
data: { paths: paths ?? null },
|
|
512
|
+
id: "workspace.prepare.start-session",
|
|
513
|
+
label: "Starting workspace write session"
|
|
514
|
+
}, async () => await workspace.startSession(sessionOptions)) : void 0;
|
|
650
515
|
return {
|
|
651
|
-
|
|
652
|
-
|
|
516
|
+
async close(error) {
|
|
517
|
+
try {
|
|
518
|
+
if (!error && workspaceSession) await copySandboxChangesToWorkspace(workspaceSession, options.definition, options.session, options.sessionWorkDir, initialTree, options.abortSignal, ignoreWriteBackPaths, options.commit, options.onWriteBack);
|
|
519
|
+
} finally {
|
|
520
|
+
await workspaceSession?.close();
|
|
521
|
+
}
|
|
522
|
+
},
|
|
523
|
+
async refreshGitBaseline() {
|
|
524
|
+
await withPrepareProgress(options.onProgress, {
|
|
525
|
+
id: "workspace.prepare.git-status",
|
|
526
|
+
label: "Preparing workspace status"
|
|
527
|
+
}, async () => {
|
|
528
|
+
await initializeSandboxGitBaseline(options.session, options.sessionWorkDir, options.abortSignal);
|
|
529
|
+
});
|
|
530
|
+
}
|
|
653
531
|
};
|
|
654
532
|
}
|
|
655
533
|
//#endregion
|
|
656
|
-
export { createWorkspaceTools, defineWorkspace, resolveWorkspaceAutoCommit, sources_exports as source, useWorkspace };
|
|
534
|
+
export { createWorkspace, createWorkspaceTools, custom, defineWorkspace, fetch, file, github, glob, markdown, mcpResources, prepareHarnessWorkspaceSession, resolveRegisteredWorkspaceDefinition, resolveWorkspaceAutoCommit, sources_exports as source, useWorkspace };
|