@evandu/super-pi 0.1.222 → 0.1.224
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/dist/client/.super-pi-client-sources.json +12 -8
- package/dist/client/assets/{CodeMirrorEditor-IWcAz6oz.js → CodeMirrorEditor-CophJ9Eb.js} +2 -2
- package/dist/client/assets/{dist-D73UQVEK.js → dist-BGVYvXYo.js} +1 -1
- package/dist/client/assets/{dist-Bx-zvsek.js → dist-BYjXg164.js} +1 -1
- package/dist/client/assets/{dist-DglikBo1.js → dist-BxcN7lnR.js} +1 -1
- package/dist/client/assets/{dist-Cd2jBngi.js → dist-C0s45hG8.js} +1 -1
- package/dist/client/assets/{dist-DZFnjuKR.js → dist-C3BgT3PU.js} +1 -1
- package/dist/client/assets/{dist-B4YZqIKX.js → dist-C3amppza.js} +1 -1
- package/dist/client/assets/{dist-ChJliyoT.js → dist-C8FqYuNv.js} +1 -1
- package/dist/client/assets/{dist-Brmqe6G6.js → dist-CEbbDGoy.js} +1 -1
- package/dist/client/assets/{dist-DgRXONWV.js → dist-CFMeFXLU.js} +1 -1
- package/dist/client/assets/{dist-KddcFuL2.js → dist-CMPtWQjx.js} +1 -1
- package/dist/client/assets/{dist-DR-NR2sl.js → dist-Ch4dVwk4.js} +1 -1
- package/dist/client/assets/{dist-B2WzMUqb.js → dist-Clk6Knpv.js} +1 -1
- package/dist/client/assets/{dist-CRfBzL2s.js → dist-CxeSxC9m.js} +1 -1
- package/dist/client/assets/{dist-B41_P-GB.js → dist-D-s1GEbP.js} +1 -1
- package/dist/client/assets/{dist-fzY9qpWd.js → dist-D7On6h3N.js} +1 -1
- package/dist/client/assets/{dist-C-CsTWFq.js → dist-DBdR1Wph.js} +1 -1
- package/dist/client/assets/{dist-ByhQMTZz.js → dist-TcwefdPn.js} +1 -1
- package/dist/client/assets/{dist-BPhtg6ss.js → dist-ZuNGtw_8.js} +1 -1
- package/dist/client/assets/{dist-DftBUe3J.js → dist-i8fMtdTP.js} +1 -1
- package/dist/client/assets/{dist-CLfjxR6r.js → dist-nSNA_ze3.js} +1 -1
- package/dist/client/assets/{dist-0tDquiIc.js → dist-pVo8vQ15.js} +1 -1
- package/dist/client/assets/{dist-JFHBsRN8.js → dist-ukIdoKjd.js} +1 -1
- package/dist/client/assets/index-CoJT13t0.js +105 -0
- package/dist/client/assets/index-DK_mME9e.css +1 -0
- package/dist/client/index.html +2 -2
- package/dist/server/application/conversation-service.d.ts +15 -0
- package/dist/server/application/conversation-service.js +28 -0
- package/dist/server/application/project-service.d.ts +7 -1
- package/dist/server/application/project-service.js +29 -9
- package/dist/server/application/run-service.js +44 -17
- package/dist/server/file-index.d.ts +13 -1
- package/dist/server/file-index.js +108 -12
- package/dist/server/file-name-index.d.ts +8 -0
- package/dist/server/file-name-index.js +58 -11
- package/dist/server/files.js +4 -0
- package/dist/server/http-server.js +143 -17
- package/dist/server/session-cwd.d.ts +2 -0
- package/dist/server/session-cwd.js +16 -1
- package/dist/server/store.js +7 -0
- package/dist/server/types.d.ts +21 -0
- package/dist/server/workspace-file-indexes.d.ts +2 -0
- package/dist/server/workspace-file-indexes.js +61 -0
- package/dist/server/workspace-file-operations.d.ts +36 -0
- package/dist/server/workspace-file-operations.js +281 -0
- package/dist/server/workspace-file-response.d.ts +3 -0
- package/dist/server/workspace-file-response.js +79 -0
- package/dist/shared/archive-decisions.d.ts +37 -0
- package/dist/shared/archive-decisions.js +86 -0
- package/dist/shared/project-overview.d.ts +35 -0
- package/dist/shared/project-overview.js +39 -1
- package/dist/shared/workspace-file-preview.d.ts +4 -0
- package/dist/shared/workspace-file-preview.js +28 -0
- package/package.json +4 -1
- package/dist/client/assets/index-DbtNmSyS.js +0 -103
- package/dist/client/assets/index-OmaD3l6r.css +0 -1
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { lstatSync, mkdirSync, renameSync, rmdirSync, unlinkSync, linkSync } from "node:fs";
|
|
3
|
+
import { lstat, mkdir, mkdtemp, readFile, readdir, realpath, rename, rm, rmdir, stat, writeFile } from "node:fs/promises";
|
|
4
|
+
import { basename, dirname, join } from "node:path";
|
|
5
|
+
import { contains, fileMeta, FilesError, isTextName, resolveInsideRoot, toRelative } from "./files.js";
|
|
6
|
+
const TRASH_DIRECTORY = ".super-pi/file-trash";
|
|
7
|
+
const MAX_INITIAL_CONTENT = 32 * 1024 * 1024;
|
|
8
|
+
const trashIdFormat = /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/;
|
|
9
|
+
const writeQueues = new Map();
|
|
10
|
+
export async function serializeWorkspaceWrite(workspace, work) {
|
|
11
|
+
const key = await realpath(workspace);
|
|
12
|
+
const previous = writeQueues.get(key) ?? Promise.resolve();
|
|
13
|
+
const next = previous.catch(() => { }).then(work);
|
|
14
|
+
writeQueues.set(key, next);
|
|
15
|
+
try {
|
|
16
|
+
return await next;
|
|
17
|
+
}
|
|
18
|
+
finally {
|
|
19
|
+
if (writeQueues.get(key) === next)
|
|
20
|
+
writeQueues.delete(key);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function stringField(body, key) {
|
|
24
|
+
const value = body[key];
|
|
25
|
+
if (typeof value !== "string")
|
|
26
|
+
throw new FilesError(400, `需要 ${key}。`, "bad_request");
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
function isMissing(error) {
|
|
30
|
+
const code = error.code;
|
|
31
|
+
return code === "ENOENT" || code === "ENOTDIR";
|
|
32
|
+
}
|
|
33
|
+
export function explorerInternalPath(path) {
|
|
34
|
+
const normalized = path.toLowerCase();
|
|
35
|
+
return normalized === TRASH_DIRECTORY || normalized.startsWith(`${TRASH_DIRECTORY}/`);
|
|
36
|
+
}
|
|
37
|
+
function validateName(name) {
|
|
38
|
+
if (!name || name !== name.trim() || name.endsWith(".") || /[<>:"/\\|?*\u0000-\u001f]/.test(name)
|
|
39
|
+
|| /^(con|prn|aux|nul|conin\$|conout\$|com[1-9¹²³]|lpt[1-9¹²³])(?:\.|$)/i.test(name)) {
|
|
40
|
+
throw new FilesError(400, "文件名包含不受支持的字符或保留名称。", "invalid_name");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export async function explorerPath(workspace, root, path, allowRoot = false) {
|
|
44
|
+
const target = await resolveInsideRoot(root, path);
|
|
45
|
+
const rel = toRelative(workspace, target);
|
|
46
|
+
if ((!allowRoot && root === target) || explorerInternalPath(rel)) {
|
|
47
|
+
throw new FilesError(403, "不能操作工作目录本身或应用的回收目录。", "protected_path");
|
|
48
|
+
}
|
|
49
|
+
const canonicalRoot = await realpath(workspace);
|
|
50
|
+
for (let ancestor = target; contains(workspace, ancestor); ancestor = dirname(ancestor)) {
|
|
51
|
+
const existing = await realpath(ancestor).catch((error) => { if (!isMissing(error))
|
|
52
|
+
throw error; });
|
|
53
|
+
if (!existing)
|
|
54
|
+
continue;
|
|
55
|
+
if (explorerInternalPath(toRelative(canonicalRoot, existing)))
|
|
56
|
+
throw new FilesError(403, "不能直接访问应用的回收目录。", "protected_path");
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
return target;
|
|
60
|
+
}
|
|
61
|
+
async function targetAt(workspace, root, parent, name) {
|
|
62
|
+
validateName(name);
|
|
63
|
+
const directory = await explorerPath(workspace, root, parent, true);
|
|
64
|
+
if (!(await stat(directory).catch(() => undefined))?.isDirectory()) {
|
|
65
|
+
throw new FilesError(404, "目标文件夹已不存在。", "missing_directory");
|
|
66
|
+
}
|
|
67
|
+
return explorerPath(workspace, root, toRelative(root, join(directory, name)));
|
|
68
|
+
}
|
|
69
|
+
async function entryInfo(path) {
|
|
70
|
+
const info = await lstat(path).catch((error) => {
|
|
71
|
+
if (isMissing(error))
|
|
72
|
+
throw new FilesError(404, "文件或目录已不存在。", "missing");
|
|
73
|
+
throw error;
|
|
74
|
+
});
|
|
75
|
+
if (!info.isFile() && !info.isDirectory())
|
|
76
|
+
throw new FilesError(415, "此操作只支持普通文件和目录。", "unsupported_entry");
|
|
77
|
+
return info;
|
|
78
|
+
}
|
|
79
|
+
function moveWithoutReplacing(source, destination, directory) {
|
|
80
|
+
try {
|
|
81
|
+
lstatSync(destination);
|
|
82
|
+
throw new FilesError(409, "目标名称已存在。", "already_exists");
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
if (!isMissing(error))
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
if (directory) {
|
|
89
|
+
if (process.platform === "win32")
|
|
90
|
+
renameSync(source, destination);
|
|
91
|
+
else {
|
|
92
|
+
mkdirSync(destination);
|
|
93
|
+
try {
|
|
94
|
+
renameSync(source, destination);
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
try {
|
|
98
|
+
rmdirSync(destination);
|
|
99
|
+
}
|
|
100
|
+
catch { /* The original error describes the failed move. */ }
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
linkSync(source, destination);
|
|
107
|
+
try {
|
|
108
|
+
unlinkSync(source);
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
unlinkSync(destination);
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async function trashRoot(workspace, create) {
|
|
116
|
+
const path = await resolveInsideRoot(workspace, TRASH_DIRECTORY);
|
|
117
|
+
const parent = dirname(path);
|
|
118
|
+
if (create) {
|
|
119
|
+
await mkdir(parent).catch((error) => { if (error.code !== "EEXIST")
|
|
120
|
+
throw error; });
|
|
121
|
+
if (!(await lstat(parent)).isDirectory())
|
|
122
|
+
throw new FilesError(403, "无法使用回收目录。", "protected_path");
|
|
123
|
+
await mkdir(path).catch((error) => { if (error.code !== "EEXIST")
|
|
124
|
+
throw error; });
|
|
125
|
+
}
|
|
126
|
+
for (const entry of [parent, path]) {
|
|
127
|
+
const info = await lstat(entry).catch((error) => { if (!isMissing(error))
|
|
128
|
+
throw error; });
|
|
129
|
+
if (info && !info.isDirectory())
|
|
130
|
+
throw new FilesError(403, "无法使用回收目录。", "protected_path");
|
|
131
|
+
}
|
|
132
|
+
return path;
|
|
133
|
+
}
|
|
134
|
+
async function readTrashRecord(workspace, directory, id) {
|
|
135
|
+
if (!trashIdFormat.test(id))
|
|
136
|
+
throw new FilesError(400, "无效的回收项。", "bad_request");
|
|
137
|
+
const entry = join(directory, id);
|
|
138
|
+
const metadata = await resolveInsideRoot(workspace, toRelative(workspace, join(entry, "record.json")));
|
|
139
|
+
if (!(await lstat(entry)).isDirectory() || !(await lstat(metadata)).isFile()) {
|
|
140
|
+
throw new FilesError(404, "回收项已不存在。", "missing");
|
|
141
|
+
}
|
|
142
|
+
const record = JSON.parse(await readFile(metadata, "utf8"));
|
|
143
|
+
if (typeof record.path !== "string" || typeof record.deletedAt !== "string" || typeof record.directory !== "boolean") {
|
|
144
|
+
throw new FilesError(400, "回收记录无效。", "bad_request");
|
|
145
|
+
}
|
|
146
|
+
return record;
|
|
147
|
+
}
|
|
148
|
+
export async function listWorkspaceTrash(workspace, root) {
|
|
149
|
+
const directory = await trashRoot(workspace, false);
|
|
150
|
+
const ids = await readdir(directory).catch((error) => { if (isMissing(error))
|
|
151
|
+
return []; throw error; });
|
|
152
|
+
const records = await Promise.all(ids.filter(id => trashIdFormat.test(id)).map(async (id) => {
|
|
153
|
+
try {
|
|
154
|
+
const record = await readTrashRecord(workspace, directory, id);
|
|
155
|
+
const target = await resolveInsideRoot(workspace, record.path);
|
|
156
|
+
if (target === root || !contains(root, target))
|
|
157
|
+
return;
|
|
158
|
+
await entryInfo(join(directory, id, "item"));
|
|
159
|
+
return { id, ...record, path: toRelative(root, target) };
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
}));
|
|
165
|
+
return records.filter(record => record !== undefined).sort((a, b) => b.deletedAt.localeCompare(a.deletedAt));
|
|
166
|
+
}
|
|
167
|
+
export async function mutateWorkspaceTree(workspace, root, body) {
|
|
168
|
+
if (body.action === "restore") {
|
|
169
|
+
const id = stringField(body, "id");
|
|
170
|
+
const directory = await trashRoot(workspace, false);
|
|
171
|
+
const record = await readTrashRecord(workspace, directory, id);
|
|
172
|
+
const rooted = await resolveInsideRoot(workspace, record.path);
|
|
173
|
+
if (!contains(root, rooted))
|
|
174
|
+
throw new FilesError(403, "回收项属于另一个目录。", "outside_workspace");
|
|
175
|
+
const path = await explorerPath(workspace, root, toRelative(root, rooted));
|
|
176
|
+
const source = join(directory, id, "item");
|
|
177
|
+
const info = await entryInfo(source);
|
|
178
|
+
moveWithoutReplacing(source, path, info.isDirectory());
|
|
179
|
+
await rm(join(directory, id, "record.json"), { force: true }).catch(() => { });
|
|
180
|
+
await rmdir(join(directory, id)).catch(() => { });
|
|
181
|
+
return { path: toRelative(root, path), directory: info.isDirectory() };
|
|
182
|
+
}
|
|
183
|
+
if (body.action === "create-file" || body.action === "create-directory") {
|
|
184
|
+
const path = await targetAt(workspace, root, stringField(body, "parent"), stringField(body, "name"));
|
|
185
|
+
const directory = body.action === "create-directory";
|
|
186
|
+
if (directory)
|
|
187
|
+
await mkdir(path);
|
|
188
|
+
else
|
|
189
|
+
await writeFile(path, "", { flag: "wx" });
|
|
190
|
+
return { path: toRelative(root, path), directory, ...(directory ? {} : { text: isTextName(path) }) };
|
|
191
|
+
}
|
|
192
|
+
if (body.action !== "move" && body.action !== "trash")
|
|
193
|
+
throw new FilesError(400, "未知的文件操作。", "bad_request");
|
|
194
|
+
const source = await explorerPath(workspace, root, stringField(body, "path"));
|
|
195
|
+
const info = await entryInfo(source);
|
|
196
|
+
const directory = info.isDirectory();
|
|
197
|
+
const previousPath = toRelative(root, source);
|
|
198
|
+
if (body.action === "trash") {
|
|
199
|
+
// Keep the item and its recovery metadata on the same filesystem.
|
|
200
|
+
if (contains(source, join(workspace, TRASH_DIRECTORY)))
|
|
201
|
+
throw new FilesError(403, "该目录包含回收空间。", "protected_path");
|
|
202
|
+
const trash = await trashRoot(workspace, true);
|
|
203
|
+
const id = randomUUID();
|
|
204
|
+
const entry = join(trash, id);
|
|
205
|
+
await mkdir(entry);
|
|
206
|
+
try {
|
|
207
|
+
const record = { path: toRelative(workspace, source), deletedAt: new Date().toISOString(), directory };
|
|
208
|
+
await writeFile(join(entry, "record.json"), JSON.stringify(record), { flag: "wx" });
|
|
209
|
+
await rename(source, join(entry, "item"));
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
await rm(join(entry, "record.json"), { force: true }).catch(() => { });
|
|
213
|
+
await rmdir(entry).catch(() => { });
|
|
214
|
+
throw error;
|
|
215
|
+
}
|
|
216
|
+
return { path: previousPath, removed: true, directory, trashId: id };
|
|
217
|
+
}
|
|
218
|
+
const destination = await targetAt(workspace, root, stringField(body, "parent"), stringField(body, "name"));
|
|
219
|
+
if (source === destination)
|
|
220
|
+
return { path: previousPath, directory };
|
|
221
|
+
if (contains(source, join(workspace, TRASH_DIRECTORY)))
|
|
222
|
+
throw new FilesError(403, "该目录包含回收空间。", "protected_path");
|
|
223
|
+
if (process.platform === "win32" && source.toLowerCase() === destination.toLowerCase()) {
|
|
224
|
+
const stage = await mkdtemp(join(dirname(source), ".super-pi-rename-"));
|
|
225
|
+
const parked = join(stage, basename(source));
|
|
226
|
+
try {
|
|
227
|
+
renameSync(source, parked);
|
|
228
|
+
try {
|
|
229
|
+
moveWithoutReplacing(parked, destination, directory);
|
|
230
|
+
}
|
|
231
|
+
catch (error) {
|
|
232
|
+
moveWithoutReplacing(parked, source, directory);
|
|
233
|
+
throw error;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
finally {
|
|
237
|
+
await rmdir(stage).catch(() => { });
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
if (directory && contains(source, destination))
|
|
242
|
+
throw new FilesError(400, "不能把目录移到其子目录中。", "invalid_destination");
|
|
243
|
+
moveWithoutReplacing(source, destination, directory);
|
|
244
|
+
}
|
|
245
|
+
return { path: toRelative(root, destination), previousPath, directory, ...(directory ? {} : { text: isTextName(destination) }) };
|
|
246
|
+
}
|
|
247
|
+
export async function inspectWorkspaceUpload(workspace, root, parent, name) {
|
|
248
|
+
const path = await targetAt(workspace, root, parent, name);
|
|
249
|
+
const info = await lstat(path).catch((error) => { if (!isMissing(error))
|
|
250
|
+
throw error; });
|
|
251
|
+
if (!info)
|
|
252
|
+
return { exists: false };
|
|
253
|
+
if (!info.isFile())
|
|
254
|
+
return { exists: true, replaceable: false };
|
|
255
|
+
return { exists: true, replaceable: true, meta: await fileMeta(path) };
|
|
256
|
+
}
|
|
257
|
+
export async function uploadWorkspaceFile(workspace, root, parent, name, bytes, baseEtag) {
|
|
258
|
+
if (bytes.length > MAX_INITIAL_CONTENT)
|
|
259
|
+
throw new FilesError(413, "单个文件不能超过 32 MB。", "too_large");
|
|
260
|
+
const destination = await targetAt(workspace, root, parent, name);
|
|
261
|
+
const stage = await mkdtemp(join(dirname(destination), ".super-pi-upload-"));
|
|
262
|
+
const stagedFile = join(stage, "item");
|
|
263
|
+
try {
|
|
264
|
+
await writeFile(stagedFile, bytes, { flag: "wx" });
|
|
265
|
+
if (baseEtag) {
|
|
266
|
+
const current = await inspectWorkspaceUpload(workspace, root, parent, name);
|
|
267
|
+
if (!current.replaceable || current.meta?.etag !== baseEtag)
|
|
268
|
+
throw new FilesError(409, "目标文件在上传前已发生变化。", "stale");
|
|
269
|
+
renameSync(stagedFile, destination);
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
moveWithoutReplacing(stagedFile, destination, false);
|
|
273
|
+
}
|
|
274
|
+
return { path: toRelative(root, destination) };
|
|
275
|
+
}
|
|
276
|
+
finally {
|
|
277
|
+
await rm(stagedFile, { force: true }).catch(() => { });
|
|
278
|
+
await rmdir(stage).catch(() => { });
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
export { MAX_INITIAL_CONTENT as MAX_WORKSPACE_UPLOAD_BYTES };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
export declare function readWorkspaceDocumentPreview(path: string): Promise<import("./documents.js").ExtractResult>;
|
|
3
|
+
export declare function respondWithWorkspaceFile(request: IncomingMessage, response: ServerResponse, path: string, download: boolean): Promise<void>;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { constants } from "node:fs";
|
|
2
|
+
import { open } from "node:fs/promises";
|
|
3
|
+
import { basename } from "node:path";
|
|
4
|
+
import { pipeline } from "node:stream/promises";
|
|
5
|
+
import { workspaceFileMime } from "../shared/workspace-file-preview.js";
|
|
6
|
+
import { FilesError } from "./files.js";
|
|
7
|
+
import { extractTextFromBytes } from "./documents.js";
|
|
8
|
+
const MAX_DOCUMENT_PREVIEW_BYTES = 16 * 1024 * 1024;
|
|
9
|
+
function openWorkspaceAsset(path) {
|
|
10
|
+
return open(path, constants.O_RDONLY | constants.O_NONBLOCK).catch((error) => {
|
|
11
|
+
if (error.code === "ENOENT" || error.code === "ENOTDIR")
|
|
12
|
+
throw new FilesError(404, "文件已不存在。", "missing");
|
|
13
|
+
throw error;
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
export async function readWorkspaceDocumentPreview(path) {
|
|
17
|
+
if (!/\.docx$/i.test(path))
|
|
18
|
+
throw new FilesError(415, "该文件没有文字预览。", "unsupported_entry");
|
|
19
|
+
const handle = await openWorkspaceAsset(path);
|
|
20
|
+
try {
|
|
21
|
+
const info = await handle.stat();
|
|
22
|
+
if (!info.isFile())
|
|
23
|
+
throw new FilesError(400, "这个路径不是普通文件。", "not_a_file");
|
|
24
|
+
if (info.size > MAX_DOCUMENT_PREVIEW_BYTES)
|
|
25
|
+
throw new FilesError(413, "文件超过文字预览上限(16 MB)。", "too_large");
|
|
26
|
+
const { buffer, bytesRead } = await handle.read({ buffer: Buffer.alloc(info.size), length: info.size });
|
|
27
|
+
if (bytesRead !== info.size)
|
|
28
|
+
throw new FilesError(409, "文档已变化,请重试。", "stale");
|
|
29
|
+
return await extractTextFromBytes(buffer, "docx");
|
|
30
|
+
}
|
|
31
|
+
finally {
|
|
32
|
+
await handle.close();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function parseRange(header, size) {
|
|
36
|
+
const match = /^bytes=(\d*)-(\d*)$/.exec(header);
|
|
37
|
+
if (!match || (!match[1] && !match[2]) || size <= 0)
|
|
38
|
+
return;
|
|
39
|
+
const start = match[1] ? Number(match[1]) : Math.max(0, size - Number(match[2]));
|
|
40
|
+
const end = match[1] && match[2] ? Math.min(size - 1, Number(match[2])) : size - 1;
|
|
41
|
+
if (Number.isSafeInteger(start) && Number.isSafeInteger(end) && start <= end)
|
|
42
|
+
return { start, end };
|
|
43
|
+
}
|
|
44
|
+
export async function respondWithWorkspaceFile(request, response, path, download) {
|
|
45
|
+
const mime = workspaceFileMime(path);
|
|
46
|
+
if (!download && !mime)
|
|
47
|
+
throw new FilesError(415, "该文件可下载。", "unsupported_entry");
|
|
48
|
+
const handle = await openWorkspaceAsset(path);
|
|
49
|
+
try {
|
|
50
|
+
const info = await handle.stat();
|
|
51
|
+
if (!info.isFile())
|
|
52
|
+
throw new FilesError(400, "这个路径不是普通文件。", "not_a_file");
|
|
53
|
+
const range = request.headers.range ? parseRange(request.headers.range, info.size) : undefined;
|
|
54
|
+
const filename = encodeURIComponent(basename(path)).replace(/['()*]/g, character => `%${character.charCodeAt(0).toString(16)}`);
|
|
55
|
+
const headers = {
|
|
56
|
+
"content-type": download ? "application/octet-stream" : mime,
|
|
57
|
+
"content-disposition": `${download ? "attachment" : "inline"}; filename*=UTF-8''${filename}`,
|
|
58
|
+
"x-content-type-options": "nosniff",
|
|
59
|
+
"cross-origin-resource-policy": "same-origin",
|
|
60
|
+
"cache-control": "no-store",
|
|
61
|
+
"accept-ranges": "bytes",
|
|
62
|
+
};
|
|
63
|
+
if (request.headers.range && !range) {
|
|
64
|
+
response.writeHead(416, { ...headers, "content-range": `bytes */${info.size}` }).end();
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (range)
|
|
68
|
+
headers["content-range"] = `bytes ${range.start}-${range.end}/${info.size}`;
|
|
69
|
+
headers["content-length"] = range ? range.end - range.start + 1 : info.size;
|
|
70
|
+
response.writeHead(range ? 206 : 200, headers);
|
|
71
|
+
if (request.method === "HEAD" || info.size === 0)
|
|
72
|
+
response.end();
|
|
73
|
+
else
|
|
74
|
+
await pipeline(handle.createReadStream(range ?? { start: 0, end: info.size - 1 }), response).catch(() => response.destroy());
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
await handle.close().catch(() => { });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type OverviewProject } from "./project-overview.js";
|
|
2
|
+
/**
|
|
3
|
+
* THE WORDS THE RECORD CARRIES WHEN AN ARCHIVE THROWS THE DECISION AWAY.
|
|
4
|
+
*
|
|
5
|
+
* 作废也是保留:例外**留在** `p.escalations` 里,原因、决定、决定时间都在,可查可导出;
|
|
6
|
+
* 改的只是**状态**。所以这句话要写得像人话,也要说清是谁干的 —— 是用户自己归档了会话,
|
|
7
|
+
* 决策才随之作废的。
|
|
8
|
+
*/
|
|
9
|
+
export declare const ARCHIVED_DECISION_REASON = "\u7528\u6237\u5F52\u6863\u4E86\u8D1F\u8D23\u4EBA\u4F1A\u8BDD\uFF0C\u672A\u62CD\u677F\u7684\u51B3\u7B56\u968F\u4E4B\u4F5C\u5E9F\u3002";
|
|
10
|
+
/**
|
|
11
|
+
* 每个会话背着几条未拍板的决策 —— 通过它负责的项目算。
|
|
12
|
+
*
|
|
13
|
+
* 一个项目可以有多条未决的用户级例外,它们都记在这个项目的**负责人会话**名下:那个会话是
|
|
14
|
+
* 项目的领导,决策是向它要来的,也是它这一摊的事。会话被归档,这个项目就没人管了。
|
|
15
|
+
*/
|
|
16
|
+
export declare function pendingUserDecisionsByOwner(projects: OverviewProject[]): Record<string, number>;
|
|
17
|
+
/**
|
|
18
|
+
* 这批会话被归档时,有几条未拍板的决策会跟着作废。
|
|
19
|
+
*
|
|
20
|
+
* 树里每一个会话都算:负责人会话在树里,它负责的项目上的未决决策就归零。
|
|
21
|
+
*/
|
|
22
|
+
export declare function pendingUserDecisionsForTree(counts: Record<string, number> | undefined, conversationIds: string[]): number;
|
|
23
|
+
/**
|
|
24
|
+
* THE DISMISSAL: archiving a leader session throws away the decisions that were waiting on it.
|
|
25
|
+
*
|
|
26
|
+
* 判据只有一条:**负责人会话在这次归档的会话树里** ⇒ 那个项目从此没人管 ⇒ 它欠用户的
|
|
27
|
+
* 未决决策作废。三件事不做:
|
|
28
|
+
*
|
|
29
|
+
* - **别的项目的例外不动。** 别人家的项目跟这次归档没关系。
|
|
30
|
+
* - **supervisor 级的不作废。** 那是团队内部的事,用户从来没被问过。
|
|
31
|
+
* - **记录不删。** 例外留在 `p.escalations` 里,`status` 变成 `dismissed`,`decision` 写下
|
|
32
|
+
* 为什么作废,`resolvedAt` 记下什么时候。作废是定性,不是消失。
|
|
33
|
+
*
|
|
34
|
+
* 返回的是同样长、同样的顺序的一份列表:**没被动过的项目是同一个对象**,动了的项目是新对象。
|
|
35
|
+
* 调用方靠这个比较就知道哪些要落盘。
|
|
36
|
+
*/
|
|
37
|
+
export declare function dismissArchivedOwnerDecisions<T extends OverviewProject>(projects: T[], archivedConversationIds: Iterable<string>, stamp: string, reason?: string): T[];
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// 归档与未拍板的决策 —— 两边共用的判据。
|
|
2
|
+
//
|
|
3
|
+
// WHY THIS FILE EXISTS.
|
|
4
|
+
//
|
|
5
|
+
// 归档会牵动三件事,而它们数的是同一批例外:
|
|
6
|
+
//
|
|
7
|
+
// 1. 归档前告诉用户「还有 N 个未拍板的决策,归档后它们会作废」(客户端的确认框);
|
|
8
|
+
// 2. 归档后把那些决策作废(服务端;记录留下,状态改掉);
|
|
9
|
+
// 3. 决策作废之后,那个项目归到历史还是留在当前(服务端判视图,客户端照着判)。
|
|
10
|
+
//
|
|
11
|
+
// 三件事要是各写一遍判据,就会各写错一遍 —— 提醒说 3 条、作废掉 2 条、视图又按第 4 条算。
|
|
12
|
+
// 所以判据只写一次,两端都调它。
|
|
13
|
+
//
|
|
14
|
+
// 一条判据贯穿全篇:**未拍板的决策 = 用户的决定 = 项目上 status 为 open 的 boss 级例外**
|
|
15
|
+
// (没有 audience 的旧记录也算,那是当年的默认受众)。supervisor 级的是团队内部的事,不是
|
|
16
|
+
// 用户的待办,归档不碰它,视图也不为它留位置。
|
|
17
|
+
import { isOpenUserDecision, userProjectDecisions } from "./project-overview.js";
|
|
18
|
+
/**
|
|
19
|
+
* THE WORDS THE RECORD CARRIES WHEN AN ARCHIVE THROWS THE DECISION AWAY.
|
|
20
|
+
*
|
|
21
|
+
* 作废也是保留:例外**留在** `p.escalations` 里,原因、决定、决定时间都在,可查可导出;
|
|
22
|
+
* 改的只是**状态**。所以这句话要写得像人话,也要说清是谁干的 —— 是用户自己归档了会话,
|
|
23
|
+
* 决策才随之作废的。
|
|
24
|
+
*/
|
|
25
|
+
export const ARCHIVED_DECISION_REASON = "用户归档了负责人会话,未拍板的决策随之作废。";
|
|
26
|
+
/**
|
|
27
|
+
* 每个会话背着几条未拍板的决策 —— 通过它负责的项目算。
|
|
28
|
+
*
|
|
29
|
+
* 一个项目可以有多条未决的用户级例外,它们都记在这个项目的**负责人会话**名下:那个会话是
|
|
30
|
+
* 项目的领导,决策是向它要来的,也是它这一摊的事。会话被归档,这个项目就没人管了。
|
|
31
|
+
*/
|
|
32
|
+
export function pendingUserDecisionsByOwner(projects) {
|
|
33
|
+
const counts = {};
|
|
34
|
+
for (const project of projects) {
|
|
35
|
+
const owner = project.ownerConversationId;
|
|
36
|
+
if (!owner)
|
|
37
|
+
continue;
|
|
38
|
+
const open = userProjectDecisions(project).length;
|
|
39
|
+
if (!open)
|
|
40
|
+
continue;
|
|
41
|
+
counts[owner] = (counts[owner] ?? 0) + open;
|
|
42
|
+
}
|
|
43
|
+
return counts;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 这批会话被归档时,有几条未拍板的决策会跟着作废。
|
|
47
|
+
*
|
|
48
|
+
* 树里每一个会话都算:负责人会话在树里,它负责的项目上的未决决策就归零。
|
|
49
|
+
*/
|
|
50
|
+
export function pendingUserDecisionsForTree(counts, conversationIds) {
|
|
51
|
+
if (!counts)
|
|
52
|
+
return 0;
|
|
53
|
+
let total = 0;
|
|
54
|
+
for (const id of conversationIds)
|
|
55
|
+
total += counts[id] ?? 0;
|
|
56
|
+
return total;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* THE DISMISSAL: archiving a leader session throws away the decisions that were waiting on it.
|
|
60
|
+
*
|
|
61
|
+
* 判据只有一条:**负责人会话在这次归档的会话树里** ⇒ 那个项目从此没人管 ⇒ 它欠用户的
|
|
62
|
+
* 未决决策作废。三件事不做:
|
|
63
|
+
*
|
|
64
|
+
* - **别的项目的例外不动。** 别人家的项目跟这次归档没关系。
|
|
65
|
+
* - **supervisor 级的不作废。** 那是团队内部的事,用户从来没被问过。
|
|
66
|
+
* - **记录不删。** 例外留在 `p.escalations` 里,`status` 变成 `dismissed`,`decision` 写下
|
|
67
|
+
* 为什么作废,`resolvedAt` 记下什么时候。作废是定性,不是消失。
|
|
68
|
+
*
|
|
69
|
+
* 返回的是同样长、同样的顺序的一份列表:**没被动过的项目是同一个对象**,动了的项目是新对象。
|
|
70
|
+
* 调用方靠这个比较就知道哪些要落盘。
|
|
71
|
+
*/
|
|
72
|
+
export function dismissArchivedOwnerDecisions(projects, archivedConversationIds, stamp, reason = ARCHIVED_DECISION_REASON) {
|
|
73
|
+
const archived = new Set(archivedConversationIds);
|
|
74
|
+
return projects.map(project => {
|
|
75
|
+
const owner = project.ownerConversationId;
|
|
76
|
+
if (!owner || !archived.has(owner))
|
|
77
|
+
return project;
|
|
78
|
+
const escalations = project.escalations ?? [];
|
|
79
|
+
if (!escalations.some(isOpenUserDecision))
|
|
80
|
+
return project;
|
|
81
|
+
const dismissed = escalations.map(item => isOpenUserDecision(item)
|
|
82
|
+
? { ...item, status: "dismissed", decision: reason, resolvedAt: stamp }
|
|
83
|
+
: item);
|
|
84
|
+
return { ...project, escalations: dismissed };
|
|
85
|
+
});
|
|
86
|
+
}
|
|
@@ -18,6 +18,11 @@ export type OverviewDecision = {
|
|
|
18
18
|
severity?: string;
|
|
19
19
|
requestedByConversationId?: string;
|
|
20
20
|
createdAt?: string;
|
|
21
|
+
/** What the case was answered with - the reader's own words, or the words the archive wrote on
|
|
22
|
+
* their behalf. Either way the record keeps it, because a decision nobody can read is a
|
|
23
|
+
* decision that never happened. */
|
|
24
|
+
decision?: string;
|
|
25
|
+
resolvedAt?: string;
|
|
21
26
|
};
|
|
22
27
|
export type OverviewProject = {
|
|
23
28
|
id: string;
|
|
@@ -37,6 +42,13 @@ export type OverviewProject = {
|
|
|
37
42
|
}[];
|
|
38
43
|
};
|
|
39
44
|
};
|
|
45
|
+
/**
|
|
46
|
+
* 一条例外是不是「用户还欠着一个决定」。
|
|
47
|
+
*
|
|
48
|
+
* 一条判据,全篇通用:`status` 是 open(还没结),受众是 boss(或者是没写 audience 的旧记录,
|
|
49
|
+
* 那是当年的默认受众)。supervisor 级的是团队内部的事,用户从来没被问过,所以不算。
|
|
50
|
+
*/
|
|
51
|
+
export declare function isOpenUserDecision(item: OverviewDecision): boolean;
|
|
40
52
|
/** Internal coordination is not a request for the user's attention. */
|
|
41
53
|
export declare function userProjectDecisions(project: OverviewProject): OverviewDecision[];
|
|
42
54
|
/**
|
|
@@ -51,6 +63,29 @@ export declare function projectsNeedingUserDecision(projects: OverviewProject[])
|
|
|
51
63
|
projects: number;
|
|
52
64
|
cases: number;
|
|
53
65
|
};
|
|
66
|
+
/**
|
|
67
|
+
* IS THIS PROJECT STILL THE READER'S BUSINESS?
|
|
68
|
+
*
|
|
69
|
+
* Two ways a project stops being the reader's business: it ENDED (cancelled or failed), or its
|
|
70
|
+
* OWNER SESSION WAS ARCHIVED - and a project whose owner sits in the archive has nobody left to
|
|
71
|
+
* run it. The second case is not a status: the record still says 「进行中」, but the session that
|
|
72
|
+
* was supposed to drive it is gone, so the panel cannot even offer 「让负责人继续」. A project
|
|
73
|
+
* like that is history with its records intact, not work in progress.
|
|
74
|
+
*
|
|
75
|
+
* A project that has NO owner yet is NOT over: it simply has not started, and the reader is the
|
|
76
|
+
* one who can start it. Only an owner that EXISTS and is archived makes a project unmanaged.
|
|
77
|
+
*/
|
|
78
|
+
export declare function projectIsOver(project: {
|
|
79
|
+
status?: string;
|
|
80
|
+
}, ownerArchived?: boolean): boolean;
|
|
81
|
+
/**
|
|
82
|
+
* WHICH VIEW A PROJECT BELONGS TO - the server decides, the client re-applies the same rule.
|
|
83
|
+
*
|
|
84
|
+
* The split is not 「ended / not ended」 - it is 「still owed something / nothing left to say」.
|
|
85
|
+
* A project that still owes the reader a decision stays in the current list, because hiding it
|
|
86
|
+
* would leave that decision with nobody. Everything else that is over goes to history.
|
|
87
|
+
*/
|
|
88
|
+
export declare function projectInView(project: OverviewProject, ownerArchived: boolean, view: "current" | "history"): boolean;
|
|
54
89
|
export declare function ownerProjectReports(project: OverviewProject): OverviewReport[];
|
|
55
90
|
export declare function latestProjectDelivery<T extends OverviewReport>(project: {
|
|
56
91
|
ownerConversationId?: string;
|
|
@@ -1,6 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 一条例外是不是「用户还欠着一个决定」。
|
|
3
|
+
*
|
|
4
|
+
* 一条判据,全篇通用:`status` 是 open(还没结),受众是 boss(或者是没写 audience 的旧记录,
|
|
5
|
+
* 那是当年的默认受众)。supervisor 级的是团队内部的事,用户从来没被问过,所以不算。
|
|
6
|
+
*/
|
|
7
|
+
export function isOpenUserDecision(item) {
|
|
8
|
+
return item.status === "open" && (item.audience === "boss" || item.audience === undefined);
|
|
9
|
+
}
|
|
1
10
|
/** Internal coordination is not a request for the user's attention. */
|
|
2
11
|
export function userProjectDecisions(project) {
|
|
3
|
-
return (project.escalations ?? []).filter(
|
|
12
|
+
return (project.escalations ?? []).filter(isOpenUserDecision);
|
|
4
13
|
}
|
|
5
14
|
/**
|
|
6
15
|
* HOW MANY PROJECTS NEED THE USER - counted by project, not by case.
|
|
@@ -17,6 +26,35 @@ export function projectsNeedingUserDecision(projects) {
|
|
|
17
26
|
cases: withCases.reduce((sum, project) => sum + userProjectDecisions(project).length, 0),
|
|
18
27
|
};
|
|
19
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* IS THIS PROJECT STILL THE READER'S BUSINESS?
|
|
31
|
+
*
|
|
32
|
+
* Two ways a project stops being the reader's business: it ENDED (cancelled or failed), or its
|
|
33
|
+
* OWNER SESSION WAS ARCHIVED - and a project whose owner sits in the archive has nobody left to
|
|
34
|
+
* run it. The second case is not a status: the record still says 「进行中」, but the session that
|
|
35
|
+
* was supposed to drive it is gone, so the panel cannot even offer 「让负责人继续」. A project
|
|
36
|
+
* like that is history with its records intact, not work in progress.
|
|
37
|
+
*
|
|
38
|
+
* A project that has NO owner yet is NOT over: it simply has not started, and the reader is the
|
|
39
|
+
* one who can start it. Only an owner that EXISTS and is archived makes a project unmanaged.
|
|
40
|
+
*/
|
|
41
|
+
export function projectIsOver(project, ownerArchived = false) {
|
|
42
|
+
if (["cancelled", "failed"].includes(project.status ?? ""))
|
|
43
|
+
return true;
|
|
44
|
+
return ownerArchived;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* WHICH VIEW A PROJECT BELONGS TO - the server decides, the client re-applies the same rule.
|
|
48
|
+
*
|
|
49
|
+
* The split is not 「ended / not ended」 - it is 「still owed something / nothing left to say」.
|
|
50
|
+
* A project that still owes the reader a decision stays in the current list, because hiding it
|
|
51
|
+
* would leave that decision with nobody. Everything else that is over goes to history.
|
|
52
|
+
*/
|
|
53
|
+
export function projectInView(project, ownerArchived, view) {
|
|
54
|
+
const over = projectIsOver(project, ownerArchived);
|
|
55
|
+
const owesTheReader = userProjectDecisions(project).length > 0;
|
|
56
|
+
return view === "history" ? over && !owesTheReader : !over || owesTheReader;
|
|
57
|
+
}
|
|
20
58
|
export function ownerProjectReports(project) {
|
|
21
59
|
return (project.reports ?? []).filter(report => !report.taskId && ["progress", "delivery"].includes(report.kind ?? "")
|
|
22
60
|
&& (report.authorConversationId === project.ownerConversationId && Boolean(project.ownerConversationId)
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type WorkspacePreviewKind = "image" | "pdf" | "audio" | "video" | "table" | "document" | "download";
|
|
2
|
+
export declare function workspaceFileExtension(path: string): string;
|
|
3
|
+
export declare function workspaceFileMime(path: string): string | undefined;
|
|
4
|
+
export declare function workspacePreviewKind(path: string): WorkspacePreviewKind | undefined;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const types = {
|
|
2
|
+
png: "image/png", jpg: "image/jpeg", jpeg: "image/jpeg", gif: "image/gif", webp: "image/webp", avif: "image/avif",
|
|
3
|
+
pdf: "application/pdf", mp3: "audio/mpeg", wav: "audio/wav", ogg: "audio/ogg", m4a: "audio/mp4",
|
|
4
|
+
mp4: "video/mp4", webm: "video/webm", ogv: "video/ogg",
|
|
5
|
+
};
|
|
6
|
+
export function workspaceFileExtension(path) {
|
|
7
|
+
return /\.([^./]+)$/.exec(path)?.[1].toLowerCase() ?? "";
|
|
8
|
+
}
|
|
9
|
+
export function workspaceFileMime(path) {
|
|
10
|
+
const extension = workspaceFileExtension(path);
|
|
11
|
+
return Object.hasOwn(types, extension) ? types[extension] : undefined;
|
|
12
|
+
}
|
|
13
|
+
export function workspacePreviewKind(path) {
|
|
14
|
+
const mime = workspaceFileMime(path);
|
|
15
|
+
if (mime === "application/pdf")
|
|
16
|
+
return "pdf";
|
|
17
|
+
if (mime?.startsWith("image/"))
|
|
18
|
+
return "image";
|
|
19
|
+
if (mime?.startsWith("audio/"))
|
|
20
|
+
return "audio";
|
|
21
|
+
if (mime?.startsWith("video/"))
|
|
22
|
+
return "video";
|
|
23
|
+
if (/\.(csv|tsv)$/i.test(path))
|
|
24
|
+
return "table";
|
|
25
|
+
if (/\.docx$/i.test(path))
|
|
26
|
+
return "document";
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@evandu/super-pi",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.224",
|
|
4
4
|
"description": "Web control plane and companion for Pi",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -46,6 +46,8 @@
|
|
|
46
46
|
"scripts/markdown-syntax.test.mjs",
|
|
47
47
|
"scripts/file-mentions.test.mjs",
|
|
48
48
|
"scripts/sidebar-refresh.test.mjs",
|
|
49
|
+
"scripts/sidebar-project-leak.test.mjs",
|
|
50
|
+
"scripts/archive-drops-decisions.test.mjs",
|
|
49
51
|
"scripts/provider-network.test.mjs",
|
|
50
52
|
"scripts/project-overview.test.mjs",
|
|
51
53
|
"scripts/team-handoff-view.test.mjs",
|
|
@@ -290,6 +292,7 @@
|
|
|
290
292
|
"check:browser": [
|
|
291
293
|
"scripts/markdown-syntax-browser.test.mjs",
|
|
292
294
|
"scripts/file-mentions-browser.test.mjs",
|
|
295
|
+
"scripts/workspace-explorer-browser.test.mjs",
|
|
293
296
|
"scripts/message-actions-browser.test.mjs",
|
|
294
297
|
"scripts/sidebar-refresh-browser.test.mjs",
|
|
295
298
|
"scripts/provider-network-browser.test.mjs",
|