@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,386 @@
|
|
|
1
|
+
import { decodeNdjsonStream } from "./ndjson.js";
|
|
2
|
+
export class DikwClientError extends Error {
|
|
3
|
+
status;
|
|
4
|
+
code;
|
|
5
|
+
detail;
|
|
6
|
+
constructor(options) {
|
|
7
|
+
super(options.message);
|
|
8
|
+
this.name = "DikwClientError";
|
|
9
|
+
this.status = options.status;
|
|
10
|
+
this.code = options.code;
|
|
11
|
+
this.detail = options.detail;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export class DikwClient {
|
|
15
|
+
baseUrl;
|
|
16
|
+
token;
|
|
17
|
+
_coreId;
|
|
18
|
+
constructor(config = {}) {
|
|
19
|
+
this.baseUrl = normalizeBaseUrl(config.baseUrl ?? "");
|
|
20
|
+
this.token = config.token ?? "";
|
|
21
|
+
// Default to the normalized baseUrl, but allow callers to override —
|
|
22
|
+
// e.g. App.tsx passes the user-visible ``serverUrl`` so the same-origin
|
|
23
|
+
// proxy mode (baseUrl='') still has a distinct identity across distinct
|
|
24
|
+
// upstream cores.
|
|
25
|
+
this._coreId = normalizeBaseUrl(config.coreId ?? config.baseUrl ?? "");
|
|
26
|
+
}
|
|
27
|
+
/** Stable identifier for the core this client talks to. Used by callers that
|
|
28
|
+
* persist task state and must invalidate it if the user reconnects to a
|
|
29
|
+
* different server. */
|
|
30
|
+
get coreId() {
|
|
31
|
+
return this._coreId;
|
|
32
|
+
}
|
|
33
|
+
get(path, options = {}) {
|
|
34
|
+
return this.requestJson(path, { ...options, method: "GET" });
|
|
35
|
+
}
|
|
36
|
+
post(path, body, options = {}) {
|
|
37
|
+
return this.requestJson(path, { ...options, method: "POST", body });
|
|
38
|
+
}
|
|
39
|
+
listTasks(params = {}, signal) {
|
|
40
|
+
return this.requestJson("/v1/tasks", {
|
|
41
|
+
method: "GET",
|
|
42
|
+
params: {
|
|
43
|
+
status: params.status,
|
|
44
|
+
op: params.op,
|
|
45
|
+
limit: params.limit,
|
|
46
|
+
cursor: params.cursor,
|
|
47
|
+
},
|
|
48
|
+
signal,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
getTask(taskId, signal) {
|
|
52
|
+
return this.requestJson(`/v1/tasks/${encodeURIComponent(taskId)}`, {
|
|
53
|
+
method: "GET",
|
|
54
|
+
signal,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
async getTaskResult(taskId, signal) {
|
|
58
|
+
// ``GET /v1/tasks/{id}/result`` returns a ``TaskResultBody`` envelope
|
|
59
|
+
// ``{ task_id, status, started_at, finished_at, result, error }``. Every
|
|
60
|
+
// caller wants the unwrapped ``result`` payload (e.g. ``FixProposalReport``
|
|
61
|
+
// / ``ApplyReport``) and would otherwise read ``proposeResult.proposals``
|
|
62
|
+
// off the envelope and get ``undefined``. Unwrap centrally so each caller
|
|
63
|
+
// doesn't have to remember.
|
|
64
|
+
const envelope = await this.requestJson(`/v1/tasks/${encodeURIComponent(taskId)}/result`, {
|
|
65
|
+
method: "GET",
|
|
66
|
+
signal,
|
|
67
|
+
});
|
|
68
|
+
if (envelope.status !== "succeeded") {
|
|
69
|
+
// Use a distinct ``code`` for cancelled vs failed so callers (notably
|
|
70
|
+
// ImportPage.handlePipelineError) can route a server-cancelled task to
|
|
71
|
+
// the cancelled UI branch instead of treating it as a generic failure.
|
|
72
|
+
throw new DikwClientError({
|
|
73
|
+
status: 200,
|
|
74
|
+
code: envelope.status === "cancelled" ? "task_cancelled" : "task_not_succeeded",
|
|
75
|
+
message: `task ${taskId} terminated as ${envelope.status}; cannot return result`,
|
|
76
|
+
detail: envelope.error ?? undefined,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
if (envelope.result === null) {
|
|
80
|
+
throw new DikwClientError({
|
|
81
|
+
status: 200,
|
|
82
|
+
code: "task_result_empty",
|
|
83
|
+
message: `task ${taskId} succeeded but recorded no result`,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return envelope.result;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Authoritative terminal verdict for a task, shaped as a synthetic
|
|
90
|
+
* ``type:"final"`` event. ``streamTaskEvents`` decides *when to stop polling*
|
|
91
|
+
* from ``task_status`` + ``has_more``, but pipeline callers read *success*
|
|
92
|
+
* from a captured ``final`` event. Those two signals can disagree within a
|
|
93
|
+
* single ``/events`` response: ``task_status`` is read live (already
|
|
94
|
+
* terminal) while the ``events[]`` tail lags, so a page can report
|
|
95
|
+
* ``{task_status:"succeeded", has_more:false, events:[]}`` and the stream
|
|
96
|
+
* ends without ever yielding the ``final`` event. ``GET /v1/tasks/{id}``
|
|
97
|
+
* reads the authoritative row (the same source the Tasks list shows), so it
|
|
98
|
+
* never loses that race — use it to reconcile when the stream drained without
|
|
99
|
+
* a ``final``. Returns ``null`` if the task is somehow still non-terminal.
|
|
100
|
+
*/
|
|
101
|
+
async getTaskFinalEvent(taskId, signal) {
|
|
102
|
+
const row = await this.getTask(taskId, signal);
|
|
103
|
+
if (!isTerminalStatus(row.status)) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
type: "final",
|
|
108
|
+
seq: -1,
|
|
109
|
+
ts: row.finished_at ?? row.created_at,
|
|
110
|
+
status: row.status,
|
|
111
|
+
result: row.result,
|
|
112
|
+
error: row.error,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
cancelTask(taskId, signal) {
|
|
116
|
+
return this.requestJson(`/v1/tasks/${encodeURIComponent(taskId)}/cancel`, {
|
|
117
|
+
method: "POST",
|
|
118
|
+
signal,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
importBundle(payload, manifestJson, signal) {
|
|
122
|
+
const form = new FormData();
|
|
123
|
+
// Field names match dikw-core's routes_import.py multipart contract.
|
|
124
|
+
form.append("payload", payload, "import.tar.gz");
|
|
125
|
+
form.append("manifest", manifestJson);
|
|
126
|
+
return this.postMultipart("/v1/import", form, signal);
|
|
127
|
+
}
|
|
128
|
+
startIngest(opts = {}, signal) {
|
|
129
|
+
return this.post("/v1/ingest", { no_embed: opts.noEmbed ?? false }, { signal });
|
|
130
|
+
}
|
|
131
|
+
startSynth(opts = {}, signal) {
|
|
132
|
+
return this.post("/v1/synth", { force_all: opts.forceAll ?? false, no_embed: opts.noEmbed ?? false }, { signal });
|
|
133
|
+
}
|
|
134
|
+
startLintPropose(opts = {}, signal) {
|
|
135
|
+
return this.post("/v1/lint/propose", {
|
|
136
|
+
rule: opts.rule ?? null,
|
|
137
|
+
limit: opts.limit ?? 10,
|
|
138
|
+
enable_llm: opts.enableLlm ?? false,
|
|
139
|
+
}, { signal });
|
|
140
|
+
}
|
|
141
|
+
startLintApply(opts, signal) {
|
|
142
|
+
return this.post("/v1/lint/apply", {
|
|
143
|
+
proposal_task_id: opts.proposalTaskId,
|
|
144
|
+
pick: opts.pick ?? null,
|
|
145
|
+
skip: opts.skip ?? null,
|
|
146
|
+
}, { signal });
|
|
147
|
+
}
|
|
148
|
+
async postMultipart(path, form, signal) {
|
|
149
|
+
// FormData sets its own Content-Type with boundary — never inject one.
|
|
150
|
+
const headers = {
|
|
151
|
+
Accept: "application/json",
|
|
152
|
+
};
|
|
153
|
+
if (this.token) {
|
|
154
|
+
headers.Authorization = `Bearer ${this.token}`;
|
|
155
|
+
}
|
|
156
|
+
const response = await fetch(buildRequestUrl(this.baseUrl, path), {
|
|
157
|
+
method: "POST",
|
|
158
|
+
headers,
|
|
159
|
+
body: form,
|
|
160
|
+
signal,
|
|
161
|
+
});
|
|
162
|
+
if (!response.ok) {
|
|
163
|
+
throw await errorFromResponse(response);
|
|
164
|
+
}
|
|
165
|
+
if (response.status === 204) {
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
return (await response.json());
|
|
169
|
+
}
|
|
170
|
+
async requestJson(path, options = {}) {
|
|
171
|
+
const response = await fetch(buildRequestUrl(this.baseUrl, path, options.params), {
|
|
172
|
+
method: options.method ?? "GET",
|
|
173
|
+
headers: this.headers(options.body !== undefined),
|
|
174
|
+
body: options.body === undefined ? undefined : JSON.stringify(options.body),
|
|
175
|
+
signal: options.signal,
|
|
176
|
+
});
|
|
177
|
+
if (!response.ok) {
|
|
178
|
+
throw await errorFromResponse(response);
|
|
179
|
+
}
|
|
180
|
+
if (response.status === 204) {
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
return (await response.json());
|
|
184
|
+
}
|
|
185
|
+
streamRetrieve(body, signal) {
|
|
186
|
+
return this.streamNdjson("/v1/retrieve", {
|
|
187
|
+
method: "POST",
|
|
188
|
+
body,
|
|
189
|
+
signal,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
async *streamTaskEvents(taskId, fromSeq, signal) {
|
|
193
|
+
const path = `/v1/tasks/${encodeURIComponent(taskId)}/events`;
|
|
194
|
+
let cursor = fromSeq;
|
|
195
|
+
let failures = 0;
|
|
196
|
+
while (true) {
|
|
197
|
+
let page;
|
|
198
|
+
try {
|
|
199
|
+
page = await this.requestJson(path, {
|
|
200
|
+
method: "GET",
|
|
201
|
+
params: { from_seq: cursor, wait: 30 },
|
|
202
|
+
signal,
|
|
203
|
+
});
|
|
204
|
+
failures = 0;
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
// A long follow can sit behind a proxy/tunnel that transiently drops or
|
|
208
|
+
// 5xx's the connection. The poll is resumable from the unchanged
|
|
209
|
+
// ``cursor`` (advanced only after a successful page below), so a
|
|
210
|
+
// transient gateway/network failure should reconnect rather than abort
|
|
211
|
+
// the follow. Cancellation and non-transient errors (4xx) propagate.
|
|
212
|
+
if (signal?.aborted || !isRetryableFollowError(error)) {
|
|
213
|
+
throw error;
|
|
214
|
+
}
|
|
215
|
+
failures += 1;
|
|
216
|
+
if (failures > FOLLOW_MAX_RETRIES) {
|
|
217
|
+
throw error;
|
|
218
|
+
}
|
|
219
|
+
await abortableDelay(followBackoffMs(failures), signal);
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
for (const event of page.events) {
|
|
223
|
+
yield event;
|
|
224
|
+
}
|
|
225
|
+
cursor = page.next_from_seq;
|
|
226
|
+
if (!page.has_more && isTerminalStatus(page.task_status)) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
async *streamNdjson(path, options = {}) {
|
|
232
|
+
const response = await fetch(buildRequestUrl(this.baseUrl, path, options.params), {
|
|
233
|
+
method: options.method ?? "GET",
|
|
234
|
+
headers: this.headers(options.body !== undefined),
|
|
235
|
+
body: options.body === undefined ? undefined : JSON.stringify(options.body),
|
|
236
|
+
signal: options.signal,
|
|
237
|
+
});
|
|
238
|
+
if (!response.ok) {
|
|
239
|
+
throw await errorFromResponse(response);
|
|
240
|
+
}
|
|
241
|
+
if (!response.body) {
|
|
242
|
+
throw new DikwClientError({
|
|
243
|
+
status: response.status,
|
|
244
|
+
code: "empty_stream",
|
|
245
|
+
message: "Server returned an empty stream body",
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
for await (const event of decodeNdjsonStream(response.body)) {
|
|
249
|
+
if (!isRecord(event)) {
|
|
250
|
+
throw new DikwClientError({
|
|
251
|
+
status: response.status,
|
|
252
|
+
code: "invalid_ndjson",
|
|
253
|
+
message: "NDJSON event is not a JSON object",
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
if (event.type === "heartbeat") {
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
yield event;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
headers(hasJsonBody) {
|
|
263
|
+
const headers = {
|
|
264
|
+
Accept: "application/json, application/x-ndjson",
|
|
265
|
+
};
|
|
266
|
+
if (hasJsonBody) {
|
|
267
|
+
headers["Content-Type"] = "application/json";
|
|
268
|
+
}
|
|
269
|
+
if (this.token) {
|
|
270
|
+
headers.Authorization = `Bearer ${this.token}`;
|
|
271
|
+
}
|
|
272
|
+
return headers;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
export function buildRequestUrl(baseUrl, path, params) {
|
|
276
|
+
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
|
|
277
|
+
const origin = typeof window === "undefined" ? "http://localhost" : window.location.origin;
|
|
278
|
+
const url = baseUrl === "" ? new URL(normalizedPath, origin) : new URL(normalizedPath, baseUrl);
|
|
279
|
+
if (params) {
|
|
280
|
+
for (const [key, value] of Object.entries(params)) {
|
|
281
|
+
if (value !== null && value !== undefined && value !== "") {
|
|
282
|
+
url.searchParams.set(key, String(value));
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (baseUrl === "") {
|
|
287
|
+
return `${url.pathname}${url.search}`;
|
|
288
|
+
}
|
|
289
|
+
return url.toString();
|
|
290
|
+
}
|
|
291
|
+
export function normalizeBaseUrl(value) {
|
|
292
|
+
const trimmed = value.trim();
|
|
293
|
+
return trimmed.endsWith("/") ? trimmed.slice(0, -1) : trimmed;
|
|
294
|
+
}
|
|
295
|
+
async function errorFromResponse(response) {
|
|
296
|
+
const text = await response.text();
|
|
297
|
+
const envelope = parseErrorEnvelope(text);
|
|
298
|
+
if (envelope) {
|
|
299
|
+
return new DikwClientError({
|
|
300
|
+
status: response.status,
|
|
301
|
+
code: envelope.error.code,
|
|
302
|
+
message: envelope.error.message,
|
|
303
|
+
detail: envelope.error.detail,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
return new DikwClientError({
|
|
307
|
+
status: response.status,
|
|
308
|
+
code: `http_${response.status}`,
|
|
309
|
+
message: text.slice(0, 240) || response.statusText,
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
function parseErrorEnvelope(text) {
|
|
313
|
+
try {
|
|
314
|
+
const parsed = JSON.parse(text);
|
|
315
|
+
if (!isRecord(parsed) || !isRecord(parsed.error)) {
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
const { code, message, detail } = parsed.error;
|
|
319
|
+
if (typeof code !== "string" || typeof message !== "string") {
|
|
320
|
+
return null;
|
|
321
|
+
}
|
|
322
|
+
return {
|
|
323
|
+
error: {
|
|
324
|
+
code,
|
|
325
|
+
message,
|
|
326
|
+
detail: isRecord(detail) ? detail : undefined,
|
|
327
|
+
},
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
catch {
|
|
331
|
+
return null;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
function isRecord(value) {
|
|
335
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
336
|
+
}
|
|
337
|
+
function isTerminalStatus(status) {
|
|
338
|
+
return status === "succeeded" || status === "failed" || status === "cancelled";
|
|
339
|
+
}
|
|
340
|
+
// ── task-follow transient-error resilience ──────────────────────────────────
|
|
341
|
+
// ``streamTaskEvents`` long-polls ``GET /v1/tasks/{id}/events`` and is resumable
|
|
342
|
+
// from the last ``from_seq`` cursor (advanced only after a successful page). A
|
|
343
|
+
// reverse proxy / tunnel (Cloudflare, nginx, Caddy) can drop or 5xx a long-lived
|
|
344
|
+
// connection mid-task while the task keeps running server-side, so a transient
|
|
345
|
+
// poll failure should reconnect — not abort the follow and misreport a running
|
|
346
|
+
// task as failed.
|
|
347
|
+
const FOLLOW_RETRY_BASE_MS = 1000;
|
|
348
|
+
const FOLLOW_RETRY_CAP_MS = 15000;
|
|
349
|
+
const FOLLOW_MAX_RETRIES = 8;
|
|
350
|
+
/** Capped exponential backoff: 1s, 2s, 4s, 8s, 15s, 15s… (``attempt`` is 1-based,
|
|
351
|
+
* the count of consecutive failures). */
|
|
352
|
+
function followBackoffMs(attempt) {
|
|
353
|
+
return Math.min(FOLLOW_RETRY_BASE_MS * 2 ** (attempt - 1), FOLLOW_RETRY_CAP_MS);
|
|
354
|
+
}
|
|
355
|
+
/** Transient transport failures worth reconnecting on: upstream 5xx (502/503/504
|
|
356
|
+
* from a proxy/tunnel) and network-level fetch failures (``TypeError``). Client
|
|
357
|
+
* errors (4xx, incl. auth/404) and aborts are NOT retried. */
|
|
358
|
+
function isRetryableFollowError(error) {
|
|
359
|
+
if (error instanceof DikwClientError) {
|
|
360
|
+
return error.status >= 500;
|
|
361
|
+
}
|
|
362
|
+
if (error instanceof DOMException && error.name === "AbortError") {
|
|
363
|
+
return false;
|
|
364
|
+
}
|
|
365
|
+
return error instanceof TypeError;
|
|
366
|
+
}
|
|
367
|
+
/** ``setTimeout`` as a promise that rejects with ``AbortError`` the moment the
|
|
368
|
+
* signal fires, so a cancel during backoff exits promptly instead of waiting
|
|
369
|
+
* out the full delay. */
|
|
370
|
+
function abortableDelay(ms, signal) {
|
|
371
|
+
return new Promise((resolve, reject) => {
|
|
372
|
+
if (signal?.aborted) {
|
|
373
|
+
reject(new DOMException("Aborted", "AbortError"));
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
const onAbort = () => {
|
|
377
|
+
clearTimeout(timer);
|
|
378
|
+
reject(new DOMException("Aborted", "AbortError"));
|
|
379
|
+
};
|
|
380
|
+
const timer = setTimeout(() => {
|
|
381
|
+
signal?.removeEventListener("abort", onAbort);
|
|
382
|
+
resolve();
|
|
383
|
+
}, ms);
|
|
384
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
385
|
+
});
|
|
386
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare class InvalidNdjsonError extends Error {
|
|
2
|
+
constructor(line: string);
|
|
3
|
+
}
|
|
4
|
+
export interface NdjsonParseResult {
|
|
5
|
+
events: unknown[];
|
|
6
|
+
tail: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function parseNdjsonBuffer(buffer: string): NdjsonParseResult;
|
|
9
|
+
export declare function decodeNdjsonStream(stream: ReadableStream<Uint8Array>): AsyncGenerator<unknown>;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export class InvalidNdjsonError extends Error {
|
|
2
|
+
constructor(line) {
|
|
3
|
+
super(`Invalid NDJSON line: ${line.slice(0, 160)}`);
|
|
4
|
+
this.name = "InvalidNdjsonError";
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export function parseNdjsonBuffer(buffer) {
|
|
8
|
+
const normalized = buffer.replace(/\r\n/g, "\n");
|
|
9
|
+
const parts = normalized.split("\n");
|
|
10
|
+
const tail = parts.pop() ?? "";
|
|
11
|
+
const events = [];
|
|
12
|
+
for (const part of parts) {
|
|
13
|
+
const line = part.trim();
|
|
14
|
+
if (!line) {
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
try {
|
|
18
|
+
events.push(JSON.parse(line));
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
throw new InvalidNdjsonError(line);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return { events, tail };
|
|
25
|
+
}
|
|
26
|
+
export async function* decodeNdjsonStream(stream) {
|
|
27
|
+
const reader = stream.getReader();
|
|
28
|
+
const decoder = new TextDecoder();
|
|
29
|
+
let tail = "";
|
|
30
|
+
try {
|
|
31
|
+
while (true) {
|
|
32
|
+
const { value, done } = await reader.read();
|
|
33
|
+
if (done) {
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
const text = decoder.decode(value, { stream: true });
|
|
37
|
+
const parsed = parseNdjsonBuffer(tail + text);
|
|
38
|
+
tail = parsed.tail;
|
|
39
|
+
for (const event of parsed.events) {
|
|
40
|
+
yield event;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const finalText = decoder.decode();
|
|
44
|
+
const remainder = (tail + finalText).trim();
|
|
45
|
+
if (remainder) {
|
|
46
|
+
try {
|
|
47
|
+
yield JSON.parse(remainder);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
throw new InvalidNdjsonError(remainder);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
finally {
|
|
55
|
+
reader.releaseLock();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { TaskStatus } from "../types/index.js";
|
|
2
|
+
export declare function formatNumber(value: number | null | undefined): string;
|
|
3
|
+
export declare function formatScore(value: number | null | undefined): string;
|
|
4
|
+
export declare function formatPercent(value: number | null | undefined): string;
|
|
5
|
+
export declare function formatUnixSeconds(value: number | null | undefined): string;
|
|
6
|
+
/** Wall-clock `HH:MM` (24-hour) for a "last updated" freshness stamp. */
|
|
7
|
+
export declare function formatClockTime(value: Date): string;
|
|
8
|
+
export declare function formatIso(value: string | null | undefined): string;
|
|
9
|
+
export declare function formatDuration(start: string | null | undefined, end: string | null | undefined): string;
|
|
10
|
+
export declare function statusTone(status: string): "ok" | "warn" | "bad" | "muted" | "info";
|
|
11
|
+
export declare function isTerminalTask(status: TaskStatus): boolean;
|
|
12
|
+
export declare function basename(path: string): string;
|
|
13
|
+
export declare function displayTitle(doc: {
|
|
14
|
+
title: string | null;
|
|
15
|
+
path: string;
|
|
16
|
+
}): string;
|
|
17
|
+
export declare function truncateMiddle(value: string, max?: number): string;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
export function formatNumber(value) {
|
|
2
|
+
return new Intl.NumberFormat("zh-CN").format(value ?? 0);
|
|
3
|
+
}
|
|
4
|
+
export function formatScore(value) {
|
|
5
|
+
if (value === null || value === undefined || Number.isNaN(value)) {
|
|
6
|
+
return "-";
|
|
7
|
+
}
|
|
8
|
+
return value.toFixed(3);
|
|
9
|
+
}
|
|
10
|
+
export function formatPercent(value) {
|
|
11
|
+
if (value === null || value === undefined || Number.isNaN(value)) {
|
|
12
|
+
return "-";
|
|
13
|
+
}
|
|
14
|
+
return `${Math.round(value * 100)}%`;
|
|
15
|
+
}
|
|
16
|
+
export function formatUnixSeconds(value) {
|
|
17
|
+
if (value === null || value === undefined) {
|
|
18
|
+
return "-";
|
|
19
|
+
}
|
|
20
|
+
return new Intl.DateTimeFormat("zh-CN", {
|
|
21
|
+
year: "numeric",
|
|
22
|
+
month: "2-digit",
|
|
23
|
+
day: "2-digit",
|
|
24
|
+
hour: "2-digit",
|
|
25
|
+
minute: "2-digit",
|
|
26
|
+
}).format(new Date(value * 1000));
|
|
27
|
+
}
|
|
28
|
+
/** Wall-clock `HH:MM` (24-hour) for a "last updated" freshness stamp. */
|
|
29
|
+
export function formatClockTime(value) {
|
|
30
|
+
return new Intl.DateTimeFormat("zh-CN", {
|
|
31
|
+
hour: "2-digit",
|
|
32
|
+
minute: "2-digit",
|
|
33
|
+
hour12: false,
|
|
34
|
+
}).format(value);
|
|
35
|
+
}
|
|
36
|
+
export function formatIso(value) {
|
|
37
|
+
if (!value) {
|
|
38
|
+
return "-";
|
|
39
|
+
}
|
|
40
|
+
const date = new Date(value);
|
|
41
|
+
if (Number.isNaN(date.getTime())) {
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
return new Intl.DateTimeFormat("zh-CN", {
|
|
45
|
+
month: "2-digit",
|
|
46
|
+
day: "2-digit",
|
|
47
|
+
hour: "2-digit",
|
|
48
|
+
minute: "2-digit",
|
|
49
|
+
second: "2-digit",
|
|
50
|
+
}).format(date);
|
|
51
|
+
}
|
|
52
|
+
export function formatDuration(start, end) {
|
|
53
|
+
if (!start || !end) {
|
|
54
|
+
return "-";
|
|
55
|
+
}
|
|
56
|
+
const startMs = new Date(start).getTime();
|
|
57
|
+
const endMs = new Date(end).getTime();
|
|
58
|
+
if (Number.isNaN(startMs) || Number.isNaN(endMs) || endMs < startMs) {
|
|
59
|
+
return "-";
|
|
60
|
+
}
|
|
61
|
+
const seconds = (endMs - startMs) / 1000;
|
|
62
|
+
if (seconds < 60) {
|
|
63
|
+
return `${seconds.toFixed(seconds < 10 ? 1 : 0)}s`;
|
|
64
|
+
}
|
|
65
|
+
const minutes = Math.floor(seconds / 60);
|
|
66
|
+
const rest = Math.round(seconds % 60);
|
|
67
|
+
return `${minutes}m ${rest}s`;
|
|
68
|
+
}
|
|
69
|
+
export function statusTone(status) {
|
|
70
|
+
if (status === "ready" || status === "ok" || status === "succeeded" || status === "approved") {
|
|
71
|
+
return "ok";
|
|
72
|
+
}
|
|
73
|
+
if (status === "running" || status === "pending" || status === "candidate") {
|
|
74
|
+
return "info";
|
|
75
|
+
}
|
|
76
|
+
if (status === "cancelled" || status === "archived") {
|
|
77
|
+
return "warn";
|
|
78
|
+
}
|
|
79
|
+
if (status === "failed" || status === "error") {
|
|
80
|
+
return "bad";
|
|
81
|
+
}
|
|
82
|
+
return "muted";
|
|
83
|
+
}
|
|
84
|
+
export function isTerminalTask(status) {
|
|
85
|
+
return status === "succeeded" || status === "failed" || status === "cancelled";
|
|
86
|
+
}
|
|
87
|
+
export function basename(path) {
|
|
88
|
+
return path.split("/").filter(Boolean).at(-1) ?? path;
|
|
89
|
+
}
|
|
90
|
+
export function displayTitle(doc) {
|
|
91
|
+
return doc.title || basename(doc.path).replace(/\.md$/i, "");
|
|
92
|
+
}
|
|
93
|
+
export function truncateMiddle(value, max = 56) {
|
|
94
|
+
if (value.length <= max) {
|
|
95
|
+
return value;
|
|
96
|
+
}
|
|
97
|
+
const left = Math.max(8, Math.floor((max - 3) * 0.55));
|
|
98
|
+
const right = Math.max(8, max - 3 - left);
|
|
99
|
+
return `${value.slice(0, left)}...${value.slice(-right)}`;
|
|
100
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** True when `body` reads as English: CJK ideographs are < 15% of all letters
|
|
2
|
+
* (Latin + CJK). Mirrors the mockup's `pageIsEnglish`. A body with no letters
|
|
3
|
+
* at all is not considered English, so empty / symbol-only pages show no
|
|
4
|
+
* toggle. Markdown syntax characters aren't letters, so they don't skew it. */
|
|
5
|
+
export declare function isEnglishBody(body: string): boolean;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Heuristic page-language detection for the Base reader's bilingual toggle.
|
|
2
|
+
// An article's language isn't known before fetch, so the reader calls this
|
|
3
|
+
// AFTER render to decide whether to offer the EN→中 "AI 翻译" toggle at all.
|
|
4
|
+
/** True when `body` reads as English: CJK ideographs are < 15% of all letters
|
|
5
|
+
* (Latin + CJK). Mirrors the mockup's `pageIsEnglish`. A body with no letters
|
|
6
|
+
* at all is not considered English, so empty / symbol-only pages show no
|
|
7
|
+
* toggle. Markdown syntax characters aren't letters, so they don't skew it. */
|
|
8
|
+
export function isEnglishBody(body) {
|
|
9
|
+
const cjk = (body.match(/[一-鿿]/g) ?? []).length;
|
|
10
|
+
const latin = (body.match(/[A-Za-z]/g) ?? []).length;
|
|
11
|
+
const letters = cjk + latin;
|
|
12
|
+
if (letters === 0)
|
|
13
|
+
return false;
|
|
14
|
+
return cjk / letters < 0.15;
|
|
15
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type MarkdownBlockKind = "text" | "special";
|
|
2
|
+
export interface MarkdownBlock {
|
|
3
|
+
kind: MarkdownBlockKind;
|
|
4
|
+
/** The original markdown slice, trimmed of surrounding blank lines. */
|
|
5
|
+
md: string;
|
|
6
|
+
}
|
|
7
|
+
/** Split `body` into ordered text / special blocks (see module header). */
|
|
8
|
+
export declare function splitMarkdownBlocks(body: string): MarkdownBlock[];
|