@opendikw/web-client 0.1.0-rc.1
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/LICENSE +21 -0
- package/README.md +31 -0
- package/dist/agent/agentClient.d.ts +26 -0
- package/dist/agent/agentClient.js +112 -0
- package/dist/agent/index.d.ts +1 -0
- package/dist/agent/index.js +1 -0
- package/dist/connection/index.d.ts +1 -0
- package/dist/connection/index.js +1 -0
- package/dist/convert/index.d.ts +1 -0
- package/dist/convert/index.js +1 -0
- package/dist/convert/mineru-convert.d.ts +83 -0
- package/dist/convert/mineru-convert.js +565 -0
- package/dist/core/client.d.ts +94 -0
- package/dist/core/client.js +386 -0
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.js +2 -0
- package/dist/core/ndjson.d.ts +9 -0
- package/dist/core/ndjson.js +57 -0
- package/dist/document/format.d.ts +17 -0
- package/dist/document/format.js +100 -0
- package/dist/document/index.d.ts +5 -0
- package/dist/document/index.js +5 -0
- package/dist/document/lang.d.ts +5 -0
- package/dist/document/lang.js +15 -0
- package/dist/document/markdown-blocks.d.ts +8 -0
- package/dist/document/markdown-blocks.js +169 -0
- package/dist/document/markdown.d.ts +32 -0
- package/dist/document/markdown.js +199 -0
- package/dist/document/source-inline-refs.d.ts +35 -0
- package/dist/document/source-inline-refs.js +242 -0
- package/dist/import/frontmatter-merge.d.ts +5 -0
- package/dist/import/frontmatter-merge.js +89 -0
- package/dist/import/import-bundle.d.ts +90 -0
- package/dist/import/import-bundle.js +336 -0
- package/dist/import/index.d.ts +5 -0
- package/dist/import/index.js +5 -0
- package/dist/import/kebab-source-name.d.ts +5 -0
- package/dist/import/kebab-source-name.js +31 -0
- package/dist/import/md-asset-refs.d.ts +27 -0
- package/dist/import/md-asset-refs.js +97 -0
- package/dist/import/tar-reader.d.ts +9 -0
- package/dist/import/tar-reader.js +114 -0
- package/dist/import/tar.d.ts +8 -0
- package/dist/import/tar.js +104 -0
- package/dist/translate/index.d.ts +1 -0
- package/dist/translate/index.js +1 -0
- package/dist/translate/translate.d.ts +69 -0
- package/dist/translate/translate.js +437 -0
- package/dist/types/agent.d.ts +82 -0
- package/dist/types/agent.js +1 -0
- package/dist/types/auth.d.ts +15 -0
- package/dist/types/auth.js +1 -0
- package/dist/types/index.d.ts +388 -0
- package/dist/types/index.js +3 -0
- package/dist/types/trace.d.ts +27 -0
- package/dist/types/trace.js +7 -0
- package/package.json +60 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Mirrors dikw-core/src/dikw_core/md_inspect.py {_IMG_MD, _IMG_WIKILINK,
|
|
2
|
+
// _is_remote, _resolve_local}. The web importer must agree byte-for-byte
|
|
3
|
+
// on which paths are picked up — divergence shows up as missing assets
|
|
4
|
+
// after import.
|
|
5
|
+
const STANDARD_IMG_RE = /!\[([^\]]*)\]\(\s*([^)\n]+?)(?=\s+"[^"\n]*"\s*\)|\s*\))(?:\s+"[^"\n]*")?\s*\)/g;
|
|
6
|
+
const WIKILINK_IMG_RE = /!\[\[([^\]|]+?)(?:\|([^\]]+))?\]\]/g;
|
|
7
|
+
export function extractAssetRefs(body) {
|
|
8
|
+
const refs = [];
|
|
9
|
+
for (const m of body.matchAll(STANDARD_IMG_RE)) {
|
|
10
|
+
refs.push({
|
|
11
|
+
originalPath: m[2],
|
|
12
|
+
alt: m[1] ?? "",
|
|
13
|
+
start: m.index ?? 0,
|
|
14
|
+
end: (m.index ?? 0) + m[0].length,
|
|
15
|
+
syntax: "markdown",
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
for (const m of body.matchAll(WIKILINK_IMG_RE)) {
|
|
19
|
+
refs.push({
|
|
20
|
+
originalPath: m[1],
|
|
21
|
+
alt: m[2] ?? "",
|
|
22
|
+
start: m.index ?? 0,
|
|
23
|
+
end: (m.index ?? 0) + m[0].length,
|
|
24
|
+
syntax: "wikilink",
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
refs.sort((a, b) => a.start - b.start);
|
|
28
|
+
return refs;
|
|
29
|
+
}
|
|
30
|
+
const REMOTE_SCHEMES = /^([a-zA-Z][a-zA-Z0-9+\-.]*):/;
|
|
31
|
+
export function isRemoteRef(originalPath) {
|
|
32
|
+
const m = originalPath.match(REMOTE_SCHEMES);
|
|
33
|
+
if (!m)
|
|
34
|
+
return false;
|
|
35
|
+
// ``file:`` is treated as local in core; mirror that.
|
|
36
|
+
return m[1].toLowerCase() !== "file";
|
|
37
|
+
}
|
|
38
|
+
/** Strip a YAML front-matter block (``---`` ... ``---``) off the body.
|
|
39
|
+
* Web side doesn't parse the YAML — we only need the body for asset extraction.
|
|
40
|
+
* If the front-matter block is unterminated we leave the text alone. */
|
|
41
|
+
export function stripFrontmatter(text) {
|
|
42
|
+
if (!text.startsWith("---"))
|
|
43
|
+
return text;
|
|
44
|
+
// Match the opening ``---`` line, then anything up to the next ``---`` line.
|
|
45
|
+
const re = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?/;
|
|
46
|
+
const m = text.match(re);
|
|
47
|
+
if (!m)
|
|
48
|
+
return text;
|
|
49
|
+
return text.slice(m[0].length);
|
|
50
|
+
}
|
|
51
|
+
/** POSIX-style join + normalize (handles ``..`` and ``.``, keeps no leading slash). */
|
|
52
|
+
export function posixJoinNormalize(base, rel) {
|
|
53
|
+
const parts = (base ? base.split("/") : []).concat(rel.split(/[\\/]/));
|
|
54
|
+
const out = [];
|
|
55
|
+
for (const part of parts) {
|
|
56
|
+
if (part === "" || part === ".")
|
|
57
|
+
continue;
|
|
58
|
+
if (part === "..") {
|
|
59
|
+
if (out.length === 0) {
|
|
60
|
+
// Escapes the root — return a sentinel the caller can reject.
|
|
61
|
+
out.push("..");
|
|
62
|
+
}
|
|
63
|
+
else if (out[out.length - 1] === "..") {
|
|
64
|
+
out.push("..");
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
out.pop();
|
|
68
|
+
}
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
out.push(part);
|
|
72
|
+
}
|
|
73
|
+
return out.join("/");
|
|
74
|
+
}
|
|
75
|
+
/** Sibling-of-md → project-root two-stage lookup, mirroring core's _resolve_local.
|
|
76
|
+
* Returns the POSIX-relative path under project root that the reference points at,
|
|
77
|
+
* or null if neither candidate exists in ``available``. Remote refs and refs that
|
|
78
|
+
* escape the project root return null and are treated as missing by the caller. */
|
|
79
|
+
export function resolveAssetRef(originalPath, ctx) {
|
|
80
|
+
if (isRemoteRef(originalPath))
|
|
81
|
+
return null;
|
|
82
|
+
if (originalPath.startsWith("/"))
|
|
83
|
+
return null;
|
|
84
|
+
const lastSlash = ctx.mdRelPath.lastIndexOf("/");
|
|
85
|
+
const mdDir = lastSlash >= 0 ? ctx.mdRelPath.slice(0, lastSlash) : "";
|
|
86
|
+
const candidates = [
|
|
87
|
+
posixJoinNormalize(mdDir, originalPath),
|
|
88
|
+
posixJoinNormalize("", originalPath),
|
|
89
|
+
];
|
|
90
|
+
for (const cand of candidates) {
|
|
91
|
+
if (cand.startsWith(".."))
|
|
92
|
+
continue;
|
|
93
|
+
if (ctx.available.has(cand))
|
|
94
|
+
return cand;
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare class TarReaderError extends Error {
|
|
2
|
+
readonly code: "unsafe_path" | "bad_checksum" | "truncated" | "unsupported_format";
|
|
3
|
+
constructor(code: TarReaderError["code"], message: string);
|
|
4
|
+
}
|
|
5
|
+
export interface TarEntry {
|
|
6
|
+
archivePath: string;
|
|
7
|
+
data: Uint8Array;
|
|
8
|
+
}
|
|
9
|
+
export declare function readTar(input: Uint8Array): TarEntry[];
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// Minimal USTAR reader: parses the exact shape buildTar emits in
|
|
2
|
+
// ``import-bundle.ts`` (mode=0o644, uid=gid=0, mtime=0, magic "ustar\0",
|
|
3
|
+
// version "00", typeflag '0'). Used by the browser to consume the tar.gz
|
|
4
|
+
// stream returned from /web/mineru/convert. Arbitrary tar shapes
|
|
5
|
+
// (PAX, GNU long-name, symlinks, dirs) are intentionally rejected.
|
|
6
|
+
const TAR_BLOCK = 512;
|
|
7
|
+
export class TarReaderError extends Error {
|
|
8
|
+
code;
|
|
9
|
+
constructor(code, message) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = "TarReaderError";
|
|
12
|
+
this.code = code;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function readCstr(view, offset, length) {
|
|
16
|
+
let end = offset + length;
|
|
17
|
+
for (let i = offset; i < offset + length; i++) {
|
|
18
|
+
if (view[i] === 0) {
|
|
19
|
+
end = i;
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return new TextDecoder("utf-8").decode(view.subarray(offset, end));
|
|
24
|
+
}
|
|
25
|
+
function readOctal(view, offset, length) {
|
|
26
|
+
const s = readCstr(view, offset, length).trim();
|
|
27
|
+
if (s === "")
|
|
28
|
+
return 0;
|
|
29
|
+
const n = parseInt(s, 8);
|
|
30
|
+
// parseInt returns NaN for strings with no leading octal digit (e.g. a tar
|
|
31
|
+
// produced by a non-strict writer that left garbage in a numeric field).
|
|
32
|
+
// We refuse rather than letting NaN silently corrupt offset/size math
|
|
33
|
+
// downstream (`dataStart + NaN === NaN`; comparisons against NaN all return
|
|
34
|
+
// false, masking truncation/overflow checks).
|
|
35
|
+
if (!Number.isFinite(n) || n < 0) {
|
|
36
|
+
throw new TarReaderError("unsupported_format", `non-octal numeric tar field at offset ${offset}: ${JSON.stringify(s)}`);
|
|
37
|
+
}
|
|
38
|
+
return n;
|
|
39
|
+
}
|
|
40
|
+
function isZeroBlock(view, offset) {
|
|
41
|
+
for (let i = 0; i < TAR_BLOCK; i++) {
|
|
42
|
+
if (view[offset + i] !== 0)
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
function verifyChecksum(view, offset) {
|
|
48
|
+
const stored = readOctal(view, offset + 148, 8);
|
|
49
|
+
let sum = 0;
|
|
50
|
+
for (let i = 0; i < TAR_BLOCK; i++) {
|
|
51
|
+
if (i >= 148 && i < 156) {
|
|
52
|
+
sum += 0x20;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
sum += view[offset + i];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return sum === stored;
|
|
59
|
+
}
|
|
60
|
+
function isUnsafePath(path) {
|
|
61
|
+
if (path === "")
|
|
62
|
+
return true;
|
|
63
|
+
if (path.startsWith("/") || path.startsWith("\\"))
|
|
64
|
+
return true;
|
|
65
|
+
if (/^[a-zA-Z]:[/\\]/.test(path))
|
|
66
|
+
return true;
|
|
67
|
+
const norm = path.replace(/\\+/g, "/");
|
|
68
|
+
for (const seg of norm.split("/")) {
|
|
69
|
+
if (seg === "..")
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
export function readTar(input) {
|
|
75
|
+
const entries = [];
|
|
76
|
+
let pos = 0;
|
|
77
|
+
while (pos < input.length) {
|
|
78
|
+
if (pos + TAR_BLOCK > input.length) {
|
|
79
|
+
throw new TarReaderError("truncated", `tar truncated at offset ${pos}`);
|
|
80
|
+
}
|
|
81
|
+
if (isZeroBlock(input, pos)) {
|
|
82
|
+
// Standard end marker is two zero blocks. We accept a single zero block
|
|
83
|
+
// at the tail too — buildTar always writes two, but defensive parsers
|
|
84
|
+
// shouldn't crash on a slightly truncated trailer.
|
|
85
|
+
return entries;
|
|
86
|
+
}
|
|
87
|
+
if (!verifyChecksum(input, pos)) {
|
|
88
|
+
throw new TarReaderError("bad_checksum", `tar header checksum mismatch at offset ${pos}`);
|
|
89
|
+
}
|
|
90
|
+
const magic = readCstr(input, pos + 257, 6);
|
|
91
|
+
if (magic !== "ustar") {
|
|
92
|
+
throw new TarReaderError("unsupported_format", `unexpected tar magic ${JSON.stringify(magic)} at offset ${pos}`);
|
|
93
|
+
}
|
|
94
|
+
const typeflag = input[pos + 156];
|
|
95
|
+
if (typeflag !== 0 && typeflag !== 0x30) {
|
|
96
|
+
throw new TarReaderError("unsupported_format", `unsupported tar typeflag 0x${typeflag.toString(16)} at offset ${pos}`);
|
|
97
|
+
}
|
|
98
|
+
const name = readCstr(input, pos, 100);
|
|
99
|
+
const prefix = readCstr(input, pos + 345, 155);
|
|
100
|
+
const archivePath = prefix ? `${prefix}/${name}` : name;
|
|
101
|
+
if (isUnsafePath(archivePath)) {
|
|
102
|
+
throw new TarReaderError("unsafe_path", `unsafe tar entry path: ${JSON.stringify(archivePath)}`);
|
|
103
|
+
}
|
|
104
|
+
const size = readOctal(input, pos + 124, 12);
|
|
105
|
+
const dataStart = pos + TAR_BLOCK;
|
|
106
|
+
const dataEnd = dataStart + size;
|
|
107
|
+
if (dataEnd > input.length) {
|
|
108
|
+
throw new TarReaderError("truncated", `tar entry ${archivePath} data truncated`);
|
|
109
|
+
}
|
|
110
|
+
entries.push({ archivePath, data: input.slice(dataStart, dataEnd) });
|
|
111
|
+
pos = dataStart + Math.ceil(size / TAR_BLOCK) * TAR_BLOCK;
|
|
112
|
+
}
|
|
113
|
+
return entries;
|
|
114
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// Isomorphic USTAR writer. Pure: only uses TextEncoder + Uint8Array,
|
|
2
|
+
// so browser (src/utils/import-bundle.ts) and server (server/web/http.ts)
|
|
3
|
+
// share one implementation. Drift between two tar formats would break
|
|
4
|
+
// the browser's tar-reader which reads sidecar-produced archives.
|
|
5
|
+
//
|
|
6
|
+
// Output is byte-stable: mode=0o644, uid=gid=0, mtime=0, typeflag '0',
|
|
7
|
+
// magic "ustar\0", version "00". Path-splitting per POSIX 1003.1-1988
|
|
8
|
+
// (name ≤100, prefix ≤155); anything beyond that throws.
|
|
9
|
+
const TAR_BLOCK = 512;
|
|
10
|
+
const NAME_FIELD_MAX = 100;
|
|
11
|
+
const PREFIX_FIELD_MAX = 155;
|
|
12
|
+
const USTAR_PATH_MAX = NAME_FIELD_MAX + 1 + PREFIX_FIELD_MAX;
|
|
13
|
+
function writeOctal(view, offset, length, value) {
|
|
14
|
+
const oct = value.toString(8);
|
|
15
|
+
if (oct.length > length - 1) {
|
|
16
|
+
throw new Error(`tar field overflow: octal ${oct} does not fit in ${length} bytes`);
|
|
17
|
+
}
|
|
18
|
+
const padded = oct.padStart(length - 1, "0");
|
|
19
|
+
for (let i = 0; i < padded.length; i++) {
|
|
20
|
+
view[offset + i] = padded.charCodeAt(i);
|
|
21
|
+
}
|
|
22
|
+
view[offset + length - 1] = 0;
|
|
23
|
+
}
|
|
24
|
+
function writeAscii(view, offset, length, value) {
|
|
25
|
+
const bytes = new TextEncoder().encode(value);
|
|
26
|
+
if (bytes.length > length) {
|
|
27
|
+
throw new Error(`tar field overflow: ${JSON.stringify(value)} exceeds ${length} bytes`);
|
|
28
|
+
}
|
|
29
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
30
|
+
view[offset + i] = bytes[i];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export function splitUstarPath(archivePath) {
|
|
34
|
+
const bytes = new TextEncoder().encode(archivePath);
|
|
35
|
+
if (bytes.length <= NAME_FIELD_MAX) {
|
|
36
|
+
return { name: archivePath, prefix: "" };
|
|
37
|
+
}
|
|
38
|
+
if (bytes.length > USTAR_PATH_MAX) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
for (let i = archivePath.length - 1; i > 0; i--) {
|
|
42
|
+
if (archivePath[i] !== "/")
|
|
43
|
+
continue;
|
|
44
|
+
const head = archivePath.slice(0, i);
|
|
45
|
+
const tail = archivePath.slice(i + 1);
|
|
46
|
+
const headLen = new TextEncoder().encode(head).length;
|
|
47
|
+
const tailLen = new TextEncoder().encode(tail).length;
|
|
48
|
+
if (tailLen <= NAME_FIELD_MAX && headLen <= PREFIX_FIELD_MAX) {
|
|
49
|
+
return { name: tail, prefix: head };
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
function ustarHeader(archivePath, size) {
|
|
55
|
+
const split = splitUstarPath(archivePath);
|
|
56
|
+
if (split === null) {
|
|
57
|
+
throw new Error(`archive path too long for USTAR (max ${USTAR_PATH_MAX} bytes, requires PAX extended headers we don't emit): ${archivePath}`);
|
|
58
|
+
}
|
|
59
|
+
const header = new Uint8Array(TAR_BLOCK);
|
|
60
|
+
writeAscii(header, 0, 100, split.name);
|
|
61
|
+
writeOctal(header, 100, 8, 0o644);
|
|
62
|
+
writeOctal(header, 108, 8, 0);
|
|
63
|
+
writeOctal(header, 116, 8, 0);
|
|
64
|
+
writeOctal(header, 124, 12, size);
|
|
65
|
+
writeOctal(header, 136, 12, 0);
|
|
66
|
+
for (let i = 148; i < 156; i++)
|
|
67
|
+
header[i] = 0x20;
|
|
68
|
+
header[156] = 0x30;
|
|
69
|
+
writeAscii(header, 257, 6, "ustar\0");
|
|
70
|
+
header[263] = 0x30;
|
|
71
|
+
header[264] = 0x30;
|
|
72
|
+
if (split.prefix) {
|
|
73
|
+
writeAscii(header, 345, PREFIX_FIELD_MAX, split.prefix);
|
|
74
|
+
}
|
|
75
|
+
let sum = 0;
|
|
76
|
+
for (let i = 0; i < TAR_BLOCK; i++)
|
|
77
|
+
sum += header[i];
|
|
78
|
+
const oct = sum.toString(8).padStart(6, "0");
|
|
79
|
+
for (let i = 0; i < 6; i++)
|
|
80
|
+
header[148 + i] = oct.charCodeAt(i);
|
|
81
|
+
header[154] = 0;
|
|
82
|
+
header[155] = 0x20;
|
|
83
|
+
return header;
|
|
84
|
+
}
|
|
85
|
+
export function buildTar(entries) {
|
|
86
|
+
let total = 0;
|
|
87
|
+
for (const e of entries) {
|
|
88
|
+
total += TAR_BLOCK;
|
|
89
|
+
total += Math.ceil(e.data.length / TAR_BLOCK) * TAR_BLOCK;
|
|
90
|
+
}
|
|
91
|
+
total += TAR_BLOCK * 2;
|
|
92
|
+
const out = new Uint8Array(total);
|
|
93
|
+
let pos = 0;
|
|
94
|
+
for (const e of entries) {
|
|
95
|
+
const header = ustarHeader(e.archivePath, e.data.length);
|
|
96
|
+
out.set(header, pos);
|
|
97
|
+
pos += TAR_BLOCK;
|
|
98
|
+
out.set(e.data, pos);
|
|
99
|
+
pos += e.data.length;
|
|
100
|
+
const pad = (TAR_BLOCK - (e.data.length % TAR_BLOCK)) % TAR_BLOCK;
|
|
101
|
+
pos += pad;
|
|
102
|
+
}
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./translate.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./translate.js";
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/** A block translated so far, mirroring the server's `{ i, tr }` wire shape. */
|
|
2
|
+
export interface TranslatedBlock {
|
|
3
|
+
i: number;
|
|
4
|
+
tr: string;
|
|
5
|
+
}
|
|
6
|
+
export type TranslateProgress = {
|
|
7
|
+
phase: "hashing";
|
|
8
|
+
} | {
|
|
9
|
+
phase: "submitting";
|
|
10
|
+
} | {
|
|
11
|
+
phase: "polling";
|
|
12
|
+
} | {
|
|
13
|
+
phase: "cache_hit";
|
|
14
|
+
}
|
|
15
|
+
/** Emitted while the job runs as more batches land, carrying every block
|
|
16
|
+
* translated so far so the caller can reveal them progressively. */
|
|
17
|
+
| {
|
|
18
|
+
phase: "partial";
|
|
19
|
+
blocks: TranslatedBlock[];
|
|
20
|
+
};
|
|
21
|
+
export interface TranslateCache {
|
|
22
|
+
get(key: string): Promise<string[] | null>;
|
|
23
|
+
put(key: string, value: string[]): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
export interface TranslateOptions {
|
|
26
|
+
/** Target language code; defaults to "zh". */
|
|
27
|
+
targetLang?: string;
|
|
28
|
+
signal?: AbortSignal;
|
|
29
|
+
onProgress?: (e: TranslateProgress) => void;
|
|
30
|
+
cache?: TranslateCache | null;
|
|
31
|
+
/** When true, skip the cache *read* (force a fresh translation) but still
|
|
32
|
+
* write the fresh result back. Drives the reader's "再翻译一次" action. */
|
|
33
|
+
refresh?: boolean;
|
|
34
|
+
/** Override for tests. */
|
|
35
|
+
fetch?: typeof fetch;
|
|
36
|
+
pollIntervalMs?: number;
|
|
37
|
+
}
|
|
38
|
+
export type TranslateErrorCode = "translator_auth" | "translator_rate_limit" | "translator_timeout" | "translator_api" | "translator_invalid_response" | "translator_input" | "translate_disabled" | "aborted" | "invalid_response";
|
|
39
|
+
export declare class TranslateError extends Error {
|
|
40
|
+
readonly code: TranslateErrorCode;
|
|
41
|
+
constructor(code: TranslateErrorCode, message: string);
|
|
42
|
+
}
|
|
43
|
+
/** Translate `blocks` into `targetLang`. Returns translations aligned 1:1 with
|
|
44
|
+
* the input order. Throws TranslateError on failure / abort. */
|
|
45
|
+
export declare function translateBlocks(blocks: string[], opts?: TranslateOptions): Promise<string[]>;
|
|
46
|
+
/** GET /web/translate/health → whether the sidecar has a translator key. The
|
|
47
|
+
* reader hides the AI 翻译 entry when this is false. Any failure → false. */
|
|
48
|
+
export declare function fetchTranslateEnabled(fetchFn?: typeof fetch): Promise<boolean>;
|
|
49
|
+
/** In-memory cache; fallback for jsdom and a deterministic test shim. */
|
|
50
|
+
export declare class MemoryTranslateCache implements TranslateCache {
|
|
51
|
+
private readonly store;
|
|
52
|
+
get(key: string): Promise<string[] | null>;
|
|
53
|
+
put(key: string, value: string[]): Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
/** IndexedDB-backed cache. Returns null when IndexedDB is unavailable (jsdom). */
|
|
56
|
+
export declare function tryOpenDefaultTranslateCache(options?: {
|
|
57
|
+
namespace?: string;
|
|
58
|
+
}): Promise<TranslateCache | null>;
|
|
59
|
+
export declare class IDBTranslateCache implements TranslateCache {
|
|
60
|
+
private readonly db;
|
|
61
|
+
constructor(db: IDBDatabase);
|
|
62
|
+
/** Delete entries older than CACHE_TTL_MS via a readwrite cursor. Resolves on
|
|
63
|
+
* transaction commit; rejects on error so the caller can swallow it. */
|
|
64
|
+
sweepExpired(now?: number): Promise<void>;
|
|
65
|
+
get(key: string): Promise<string[] | null>;
|
|
66
|
+
put(key: string, value: string[]): Promise<void>;
|
|
67
|
+
private txGet;
|
|
68
|
+
private txPut;
|
|
69
|
+
}
|