dsh-markdown-studio 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.en.md +1 -1
- package/README.md +1 -1
- package/dist/client.js +1 -1
- package/dist/client.studio.js +104 -104
- package/dist/index.js +136 -109
- package/dist/math.js +1 -1
- package/dist/mermaid.js +195 -195
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -89,8 +89,14 @@ var AiHostService = class {
|
|
|
89
89
|
models = [];
|
|
90
90
|
}
|
|
91
91
|
const def = this.getDefaultModel()?.currentSelection();
|
|
92
|
-
const
|
|
93
|
-
const
|
|
92
|
+
const defProvider = trimString(def?.provider);
|
|
93
|
+
const defModel = trimString(def?.model);
|
|
94
|
+
const matched = defProvider && defModel ? { provider: defProvider, model: defModel } : defProvider ? (() => {
|
|
95
|
+
const first = models.find((item) => item.provider === defProvider);
|
|
96
|
+
return first ? { provider: first.provider, model: first.id } : { provider: "", model: "" };
|
|
97
|
+
})() : models[0] ? { provider: models[0].provider, model: models[0].id } : { provider: "", model: "" };
|
|
98
|
+
const provider = matched.provider;
|
|
99
|
+
const model = matched.model;
|
|
94
100
|
return {
|
|
95
101
|
available: Boolean(provider && model),
|
|
96
102
|
provider,
|
|
@@ -118,6 +124,7 @@ var AiHostService = class {
|
|
|
118
124
|
const { system, prompt } = validateStreamTexts(input.system, input.prompt);
|
|
119
125
|
const route = await this.resolveRoute(input.provider, input.model);
|
|
120
126
|
const reasoningEffort = await resolveFastReasoningEffort(llm, route.provider, route.model, signal);
|
|
127
|
+
if (signal?.aborted) throw new AiHostError("ABORTED", "\u5DF2\u505C\u6B62\u751F\u6210");
|
|
121
128
|
const message = {
|
|
122
129
|
id: crypto.randomUUID(),
|
|
123
130
|
role: "user",
|
|
@@ -144,7 +151,7 @@ var AiHostService = class {
|
|
|
144
151
|
};
|
|
145
152
|
|
|
146
153
|
// host/application/image-store.ts
|
|
147
|
-
import { createHash } from "node:crypto";
|
|
154
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
148
155
|
import { mkdir, readdir, readFile, rename, stat, unlink, writeFile } from "node:fs/promises";
|
|
149
156
|
import path from "node:path";
|
|
150
157
|
|
|
@@ -174,7 +181,7 @@ var ImageHostError = class extends Error {
|
|
|
174
181
|
}
|
|
175
182
|
};
|
|
176
183
|
function digest(value) {
|
|
177
|
-
return createHash("sha256").update(String(value)).digest("hex");
|
|
184
|
+
return createHash("sha256").update(Buffer.isBuffer(value) ? value : String(value)).digest("hex");
|
|
178
185
|
}
|
|
179
186
|
function hasImageMagicBytes(buffer, mime) {
|
|
180
187
|
if (mime === "image/png") {
|
|
@@ -279,33 +286,36 @@ var ImageHostService = class {
|
|
|
279
286
|
const filename = id + "." + image.ext;
|
|
280
287
|
const dir = this.assetsDir();
|
|
281
288
|
const file = path.join(dir, filename);
|
|
282
|
-
await
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
const info = await stat(file);
|
|
286
|
-
exists = info.isFile() && info.size === image.buffer.length;
|
|
287
|
-
} catch (error) {
|
|
288
|
-
exists = false;
|
|
289
|
-
}
|
|
290
|
-
if (!exists) {
|
|
291
|
-
const temp = file + "." + process.pid + "-" + Date.now().toString(36) + ".tmp";
|
|
289
|
+
await this.enqueueManifest(async () => {
|
|
290
|
+
await mkdir(dir, { recursive: true, mode: 448 });
|
|
291
|
+
let exists = false;
|
|
292
292
|
try {
|
|
293
|
-
|
|
294
|
-
|
|
293
|
+
const info = await stat(file);
|
|
294
|
+
exists = info.isFile() && info.size === image.buffer.length;
|
|
295
295
|
} catch (error) {
|
|
296
|
-
|
|
297
|
-
|
|
296
|
+
exists = false;
|
|
297
|
+
}
|
|
298
|
+
if (!exists) {
|
|
299
|
+
const temp = file + "." + process.pid + "-" + Date.now().toString(36) + "-" + randomBytes(6).toString("hex") + ".tmp";
|
|
300
|
+
try {
|
|
301
|
+
await writeFile(temp, image.buffer, { mode: 420 });
|
|
302
|
+
await rename(temp, file);
|
|
303
|
+
} catch (error) {
|
|
304
|
+
await unlink(temp).catch(() => void 0);
|
|
305
|
+
throw error;
|
|
306
|
+
}
|
|
298
307
|
}
|
|
299
|
-
}
|
|
300
|
-
await this.enqueueManifest(async () => {
|
|
301
308
|
const manifest = await this.loadManifest();
|
|
302
309
|
if (!manifest[filename]) {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
310
|
+
const next = {
|
|
311
|
+
...manifest,
|
|
312
|
+
[filename]: {
|
|
313
|
+
mime: image.mime,
|
|
314
|
+
bytes: image.buffer.length,
|
|
315
|
+
storedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
316
|
+
}
|
|
307
317
|
};
|
|
308
|
-
await this.saveManifest(
|
|
318
|
+
await this.saveManifest(next);
|
|
309
319
|
}
|
|
310
320
|
});
|
|
311
321
|
const localUrl = this.localServerInfo ? `${this.localServerInfo.url}/uploads/${filename}` : "";
|
|
@@ -372,12 +382,12 @@ var ImageHostService = class {
|
|
|
372
382
|
if (!filename) {
|
|
373
383
|
throw new ImageHostError("ASSET_NOT_FOUND", "\u672C\u5730\u56FE\u7247\u4E0D\u5B58\u5728", "\u8BF7\u5237\u65B0\u56FE\u7247\u5217\u8868\u3002");
|
|
374
384
|
}
|
|
375
|
-
try {
|
|
376
|
-
await unlink(path.join(this.assetsDir(), filename));
|
|
377
|
-
} catch (error) {
|
|
378
|
-
if (!error || error.code !== "ENOENT") throw error;
|
|
379
|
-
}
|
|
380
385
|
await this.enqueueManifest(async () => {
|
|
386
|
+
try {
|
|
387
|
+
await unlink(path.join(this.assetsDir(), filename));
|
|
388
|
+
} catch (error) {
|
|
389
|
+
if (!error || error.code !== "ENOENT") throw error;
|
|
390
|
+
}
|
|
381
391
|
const manifest = await this.loadManifest();
|
|
382
392
|
if (manifest[filename]) {
|
|
383
393
|
const next = { ...manifest };
|
|
@@ -410,12 +420,15 @@ function isLoopback(remote) {
|
|
|
410
420
|
function isJsonContentType(value) {
|
|
411
421
|
return value.split(";")[0].trim().toLowerCase() === "application/json";
|
|
412
422
|
}
|
|
423
|
+
var ALLOWED_DESKTOP_PROTOCOLS = /* @__PURE__ */ new Set(["dsh-app:", "dsh:", "app:", "file:"]);
|
|
413
424
|
function isAllowedOrigin(origin, host, site) {
|
|
414
425
|
if (!origin) return true;
|
|
415
426
|
if (origin === "null") return site === "same-origin" || site === "none";
|
|
416
427
|
try {
|
|
417
428
|
const parsed = new URL(origin);
|
|
418
|
-
if (parsed.protocol !== "http:" && parsed.protocol !== "https:")
|
|
429
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
430
|
+
return ALLOWED_DESKTOP_PROTOCOLS.has(parsed.protocol);
|
|
431
|
+
}
|
|
419
432
|
return parsed.host === host;
|
|
420
433
|
} catch (error) {
|
|
421
434
|
return false;
|
|
@@ -647,10 +660,7 @@ async function readJsonBody(req, limit) {
|
|
|
647
660
|
size += buffer.length;
|
|
648
661
|
if (size > limit) {
|
|
649
662
|
fail(new ImageHostError("BODY_TOO_LARGE", "\u8BF7\u6C42\u5185\u5BB9\u8FC7\u5927", "\u8BF7\u51CF\u5C11\u4E00\u6B21\u4E0A\u4F20\u7684\u56FE\u7247\u6570\u91CF\u3002"));
|
|
650
|
-
|
|
651
|
-
req.destroy();
|
|
652
|
-
} catch (error) {
|
|
653
|
-
}
|
|
663
|
+
if (typeof req.pause === "function") req.pause();
|
|
654
664
|
return;
|
|
655
665
|
}
|
|
656
666
|
chunks.push(buffer);
|
|
@@ -691,6 +701,7 @@ function headerValue2(value) {
|
|
|
691
701
|
function isJsonContentType2(value) {
|
|
692
702
|
return value.split(";")[0].trim().toLowerCase() === "application/json";
|
|
693
703
|
}
|
|
704
|
+
var ALLOWED_DESKTOP_PROTOCOLS2 = /* @__PURE__ */ new Set(["dsh-app:", "dsh:", "app:", "file:"]);
|
|
694
705
|
function isAllowedOrigin2(origin, host, site) {
|
|
695
706
|
if (!origin) return true;
|
|
696
707
|
if (origin === "null") return site === "same-origin" || site === "none";
|
|
@@ -700,69 +711,87 @@ function isAllowedOrigin2(origin, host, site) {
|
|
|
700
711
|
} catch (error) {
|
|
701
712
|
return false;
|
|
702
713
|
}
|
|
703
|
-
if (parsed.protocol !== "http:" && parsed.protocol !== "https:")
|
|
714
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
715
|
+
return ALLOWED_DESKTOP_PROTOCOLS2.has(parsed.protocol);
|
|
716
|
+
}
|
|
704
717
|
return parsed.host === host;
|
|
705
718
|
}
|
|
719
|
+
function isLoopbackRequest(req) {
|
|
720
|
+
const remote = req.socket && req.socket.remoteAddress ? String(req.socket.remoteAddress) : "";
|
|
721
|
+
return remote === "127.0.0.1" || remote === "::1" || remote === "::ffff:127.0.0.1";
|
|
722
|
+
}
|
|
723
|
+
function validateImageRequest(req, res) {
|
|
724
|
+
if (req.method !== "POST") {
|
|
725
|
+
sendJson2(res, 405, { ok: false, error: { code: "METHOD_NOT_ALLOWED", message: "Method not allowed" } });
|
|
726
|
+
return false;
|
|
727
|
+
}
|
|
728
|
+
if (!isLoopbackRequest(req)) {
|
|
729
|
+
sendJson2(res, 403, { ok: false, error: { code: "LOOPBACK_ONLY", message: "\u56FE\u7247\u63A5\u53E3\u4EC5\u5141\u8BB8\u672C\u673A\u8BBF\u95EE" } });
|
|
730
|
+
return false;
|
|
731
|
+
}
|
|
732
|
+
const host = headerValue2(req.headers.host);
|
|
733
|
+
const origin = headerValue2(req.headers.origin);
|
|
734
|
+
const site = headerValue2(req.headers["sec-fetch-site"]).toLowerCase();
|
|
735
|
+
if (!isAllowedOrigin2(origin, host, site)) {
|
|
736
|
+
sendJson2(res, 403, {
|
|
737
|
+
ok: false,
|
|
738
|
+
error: {
|
|
739
|
+
code: "ORIGIN_REJECTED",
|
|
740
|
+
message: "\u8BF7\u6C42\u6765\u6E90\u4E0D\u88AB\u5141\u8BB8",
|
|
741
|
+
hint: "origin=" + (origin || "(none)") + " host=" + (host || "(none)") + " sec-fetch-site=" + (site || "(none)")
|
|
742
|
+
}
|
|
743
|
+
});
|
|
744
|
+
return false;
|
|
745
|
+
}
|
|
746
|
+
if (!isJsonContentType2(headerValue2(req.headers["content-type"]))) {
|
|
747
|
+
sendJson2(res, 415, {
|
|
748
|
+
ok: false,
|
|
749
|
+
error: { code: "CONTENT_TYPE_REQUIRED", message: "\u9700\u8981 application/json \u8BF7\u6C42" }
|
|
750
|
+
});
|
|
751
|
+
return false;
|
|
752
|
+
}
|
|
753
|
+
return true;
|
|
754
|
+
}
|
|
755
|
+
async function dispatchImageAction(service, body, res) {
|
|
756
|
+
const action = typeof body.action === "string" ? body.action : "";
|
|
757
|
+
if (action === "health") {
|
|
758
|
+
sendJson2(res, 200, { ok: true, service: "markdown-studio-images", version: 5, ...service.info() });
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
if (action === "asset.store") {
|
|
762
|
+
const result = await service.storeAsset({ dataUrl: body.dataUrl, filename: body.filename });
|
|
763
|
+
sendJson2(res, 200, { ok: true, ...result });
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
if (action === "asset.list") {
|
|
767
|
+
sendJson2(res, 200, { ok: true, ...await service.listAssets() });
|
|
768
|
+
return;
|
|
769
|
+
}
|
|
770
|
+
if (action === "asset.delete") {
|
|
771
|
+
sendJson2(res, 200, { ok: true, ...await service.deleteAsset(body) });
|
|
772
|
+
return;
|
|
773
|
+
}
|
|
774
|
+
sendJson2(res, 400, { ok: false, error: { code: "UNKNOWN_ACTION", message: "\u672A\u77E5\u64CD\u4F5C: " + action } });
|
|
775
|
+
}
|
|
706
776
|
function createImageRoute(options) {
|
|
707
777
|
const service = options.service;
|
|
708
778
|
if (!service) throw new Error("createImageRoute needs a service");
|
|
709
779
|
const maxBodyBytes = options.maxBodyBytes || MAX_BODY_BYTES;
|
|
710
780
|
return async function imageRoute(req, res) {
|
|
711
781
|
try {
|
|
712
|
-
if (req
|
|
713
|
-
sendJson2(res, 405, { ok: false, error: { code: "METHOD_NOT_ALLOWED", message: "Method not allowed" } });
|
|
714
|
-
return;
|
|
715
|
-
}
|
|
716
|
-
const remote = req.socket && req.socket.remoteAddress ? String(req.socket.remoteAddress) : "";
|
|
717
|
-
const loopback = remote === "127.0.0.1" || remote === "::1" || remote === "::ffff:127.0.0.1";
|
|
718
|
-
if (!loopback) {
|
|
719
|
-
sendJson2(res, 403, { ok: false, error: { code: "LOOPBACK_ONLY", message: "\u56FE\u7247\u63A5\u53E3\u4EC5\u5141\u8BB8\u672C\u673A\u8BBF\u95EE" } });
|
|
720
|
-
return;
|
|
721
|
-
}
|
|
722
|
-
const host = headerValue2(req.headers.host);
|
|
723
|
-
const origin = headerValue2(req.headers.origin);
|
|
724
|
-
const site = headerValue2(req.headers["sec-fetch-site"]).toLowerCase();
|
|
725
|
-
if (!isAllowedOrigin2(origin, host, site)) {
|
|
726
|
-
sendJson2(res, 403, {
|
|
727
|
-
ok: false,
|
|
728
|
-
error: {
|
|
729
|
-
code: "ORIGIN_REJECTED",
|
|
730
|
-
message: "\u8BF7\u6C42\u6765\u6E90\u4E0D\u88AB\u5141\u8BB8",
|
|
731
|
-
hint: "origin=" + (origin || "(none)") + " host=" + (host || "(none)") + " sec-fetch-site=" + (site || "(none)")
|
|
732
|
-
}
|
|
733
|
-
});
|
|
734
|
-
return;
|
|
735
|
-
}
|
|
736
|
-
const contentType = headerValue2(req.headers["content-type"]);
|
|
737
|
-
if (!isJsonContentType2(contentType)) {
|
|
738
|
-
sendJson2(res, 415, {
|
|
739
|
-
ok: false,
|
|
740
|
-
error: { code: "CONTENT_TYPE_REQUIRED", message: "\u9700\u8981 application/json \u8BF7\u6C42" }
|
|
741
|
-
});
|
|
742
|
-
return;
|
|
743
|
-
}
|
|
782
|
+
if (!validateImageRequest(req, res)) return;
|
|
744
783
|
const body = await readJsonBody(req, maxBodyBytes);
|
|
745
|
-
|
|
746
|
-
if (action === "health") {
|
|
747
|
-
sendJson2(res, 200, { ok: true, service: "markdown-studio-images", version: 5, ...service.info() });
|
|
748
|
-
return;
|
|
749
|
-
}
|
|
750
|
-
if (action === "asset.store") {
|
|
751
|
-
const result = await service.storeAsset({ dataUrl: body.dataUrl, filename: body.filename });
|
|
752
|
-
sendJson2(res, 200, { ok: true, ...result });
|
|
753
|
-
return;
|
|
754
|
-
}
|
|
755
|
-
if (action === "asset.list") {
|
|
756
|
-
sendJson2(res, 200, { ok: true, ...await service.listAssets() });
|
|
757
|
-
return;
|
|
758
|
-
}
|
|
759
|
-
if (action === "asset.delete") {
|
|
760
|
-
sendJson2(res, 200, { ok: true, ...await service.deleteAsset(body) });
|
|
761
|
-
return;
|
|
762
|
-
}
|
|
763
|
-
sendJson2(res, 400, { ok: false, error: { code: "UNKNOWN_ACTION", message: "\u672A\u77E5\u64CD\u4F5C: " + action } });
|
|
784
|
+
await dispatchImageAction(service, body, res);
|
|
764
785
|
} catch (error) {
|
|
765
786
|
if (error instanceof ImageHostError) {
|
|
787
|
+
if (error.code === "BODY_TOO_LARGE" && !res.headersSent) {
|
|
788
|
+
res.setHeader("connection", "close");
|
|
789
|
+
sendJson2(res, 413, {
|
|
790
|
+
ok: false,
|
|
791
|
+
error: { code: error.code, message: error.message, hint: error.hint, detail: error.detail }
|
|
792
|
+
});
|
|
793
|
+
return;
|
|
794
|
+
}
|
|
766
795
|
sendJson2(res, 200, {
|
|
767
796
|
ok: false,
|
|
768
797
|
error: { code: error.code, message: error.message, hint: error.hint, detail: error.detail }
|
|
@@ -780,34 +809,35 @@ function createImageRoute(options) {
|
|
|
780
809
|
}
|
|
781
810
|
};
|
|
782
811
|
}
|
|
812
|
+
function isLoopbackReq(req) {
|
|
813
|
+
const remote = req.socket?.remoteAddress ? String(req.socket.remoteAddress) : "";
|
|
814
|
+
return remote === "127.0.0.1" || remote === "::1" || remote === "::ffff:127.0.0.1";
|
|
815
|
+
}
|
|
816
|
+
function sendAssetText(res, status, text, headOnly = false) {
|
|
817
|
+
res.writeHead(status, { "content-type": "text/plain; charset=utf-8" });
|
|
818
|
+
res.end(headOnly ? void 0 : text);
|
|
819
|
+
}
|
|
783
820
|
function createAssetRoute(options) {
|
|
784
821
|
const service = options.service;
|
|
785
822
|
if (!service) throw new Error("createAssetRoute needs a service");
|
|
786
823
|
return async function assetRoute(req, res) {
|
|
787
824
|
try {
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
if (!loopback) {
|
|
791
|
-
res.writeHead(403, { "content-type": "text/plain; charset=utf-8" });
|
|
792
|
-
res.end("loopback only");
|
|
825
|
+
if (!isLoopbackReq(req)) {
|
|
826
|
+
sendAssetText(res, 403, "loopback only");
|
|
793
827
|
return;
|
|
794
828
|
}
|
|
795
829
|
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
796
|
-
res
|
|
797
|
-
res.end("method not allowed");
|
|
830
|
+
sendAssetText(res, 405, "method not allowed");
|
|
798
831
|
return;
|
|
799
832
|
}
|
|
800
833
|
const pathname = new URL(req.url || "/", "http://localhost").pathname;
|
|
801
834
|
if (pathname.indexOf(ASSET_PREFIX) !== 0) {
|
|
802
|
-
res
|
|
803
|
-
res.end("not found");
|
|
835
|
+
sendAssetText(res, 404, "not found");
|
|
804
836
|
return;
|
|
805
837
|
}
|
|
806
|
-
const
|
|
807
|
-
const asset = await service.readAsset(filename);
|
|
838
|
+
const asset = await service.readAsset(pathname.slice(ASSET_PREFIX.length));
|
|
808
839
|
if (!asset) {
|
|
809
|
-
res
|
|
810
|
-
res.end("not found");
|
|
840
|
+
sendAssetText(res, 404, "not found");
|
|
811
841
|
return;
|
|
812
842
|
}
|
|
813
843
|
res.writeHead(200, {
|
|
@@ -816,13 +846,10 @@ function createAssetRoute(options) {
|
|
|
816
846
|
"cache-control": "private, max-age=31536000, immutable",
|
|
817
847
|
"x-content-type-options": "nosniff"
|
|
818
848
|
});
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
res.writeHead(500, { "content-type": "text/plain; charset=utf-8" });
|
|
824
|
-
}
|
|
825
|
-
res.end("internal error");
|
|
849
|
+
res.end(req.method === "HEAD" ? void 0 : asset.buffer);
|
|
850
|
+
} catch {
|
|
851
|
+
if (!res.headersSent) sendAssetText(res, 500, "internal error", req.method === "HEAD");
|
|
852
|
+
else res.end();
|
|
826
853
|
}
|
|
827
854
|
};
|
|
828
855
|
}
|
|
@@ -905,12 +932,12 @@ function startLocalImageServer(options) {
|
|
|
905
932
|
return new Promise(function(resolve) {
|
|
906
933
|
function onError(error) {
|
|
907
934
|
server.removeListener("listening", onListening);
|
|
908
|
-
if (error
|
|
935
|
+
if (error?.code === "EADDRINUSE") {
|
|
909
936
|
void attempt().then(resolve);
|
|
910
937
|
return;
|
|
911
938
|
}
|
|
912
939
|
service.localServerInfo = null;
|
|
913
|
-
service.localServerError = String(error
|
|
940
|
+
service.localServerError = String(error?.message || error);
|
|
914
941
|
resolve(null);
|
|
915
942
|
}
|
|
916
943
|
function onListening() {
|