@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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 OpenDIKW contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# @opendikw/web-client
|
|
2
|
+
|
|
3
|
+
MIT-licensed Core and Agent clients extracted from the OpenDIKW workbench.
|
|
4
|
+
Node 24 or newer is required. Import the documented subpath matching your use:
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
import { DikwClient } from "@opendikw/web-client/core";
|
|
8
|
+
import { AgentClient } from "@opendikw/web-client/agent";
|
|
9
|
+
import type { AuthState, PageReadResult } from "@opendikw/web-client/types";
|
|
10
|
+
|
|
11
|
+
const core = new DikwClient({ baseUrl: "https://core.example", token: "server-token" });
|
|
12
|
+
const health = await core.get("/v1/health");
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
`/core`, `/agent`, and `/types` have no browser globals at module initialization.
|
|
16
|
+
Browser operations are provided by `/import` (archive and asset helpers),
|
|
17
|
+
`/convert` (conversion and cache), `/translate` (translation and cache),
|
|
18
|
+
`/document` (Markdown parsing, references and formatting), and `/connection`
|
|
19
|
+
(the default development Core URL). They do not own application routes or storage keys.
|
|
20
|
+
|
|
21
|
+
Conversion and translation caches accept `{ namespace }`. Omit it to retain
|
|
22
|
+
the existing cache database names and TTL. Applications may use an identity
|
|
23
|
+
and Core-specific namespace to keep local caches separate.
|
|
24
|
+
|
|
25
|
+
Build explicitly with `npm run build:packages` from the repository root;
|
|
26
|
+
install scripts are disabled. npm packages contain compiled ESM and declarations,
|
|
27
|
+
this README and the MIT license. Import through `exports`, not package source paths.
|
|
28
|
+
|
|
29
|
+
Source provenance: extracted from OpenDIKW/dikw-web at
|
|
30
|
+
`c924b157747f737be56f8c3361068d3d543c86e9`; original commit history and authors
|
|
31
|
+
remain in that repository.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { AgentSession, AgentStreamEvent, SessionSummary } from "../types/agent.js";
|
|
2
|
+
import type { SessionTraceView } from "../types/trace.js";
|
|
3
|
+
export interface AgentClientOptions {
|
|
4
|
+
coreUrl?: string;
|
|
5
|
+
token?: string;
|
|
6
|
+
}
|
|
7
|
+
export declare class AgentClientError extends Error {
|
|
8
|
+
readonly status: number;
|
|
9
|
+
readonly code: string;
|
|
10
|
+
constructor(status: number, code: string, message: string);
|
|
11
|
+
}
|
|
12
|
+
export declare class AgentClient {
|
|
13
|
+
private readonly options;
|
|
14
|
+
constructor(options?: AgentClientOptions);
|
|
15
|
+
listSessions(signal?: AbortSignal): Promise<SessionSummary[]>;
|
|
16
|
+
createSession(signal?: AbortSignal): Promise<AgentSession>;
|
|
17
|
+
getSession(sessionId: string, signal?: AbortSignal): Promise<AgentSession>;
|
|
18
|
+
getSessionTraces(sessionId: string, signal?: AbortSignal): Promise<SessionTraceView>;
|
|
19
|
+
renameSession(sessionId: string, title: string, signal?: AbortSignal): Promise<AgentSession>;
|
|
20
|
+
deleteSession(sessionId: string, signal?: AbortSignal): Promise<void>;
|
|
21
|
+
abort(sessionId: string, signal?: AbortSignal): Promise<void>;
|
|
22
|
+
confirmProposal(sessionId: string, proposalId: string, signal?: AbortSignal): Promise<AgentSession>;
|
|
23
|
+
rejectProposal(sessionId: string, proposalId: string, signal?: AbortSignal): Promise<AgentSession>;
|
|
24
|
+
sendMessage(sessionId: string, message: string, signal?: AbortSignal): AsyncGenerator<AgentStreamEvent>;
|
|
25
|
+
private connectionPayload;
|
|
26
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { decodeNdjsonStream } from "../core/ndjson.js";
|
|
2
|
+
export class AgentClientError extends Error {
|
|
3
|
+
status;
|
|
4
|
+
code;
|
|
5
|
+
constructor(status, code, message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "AgentClientError";
|
|
8
|
+
this.status = status;
|
|
9
|
+
this.code = code;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export class AgentClient {
|
|
13
|
+
options;
|
|
14
|
+
constructor(options = {}) {
|
|
15
|
+
this.options = options;
|
|
16
|
+
}
|
|
17
|
+
async listSessions(signal) {
|
|
18
|
+
return requestJson("/agent/sessions", { signal });
|
|
19
|
+
}
|
|
20
|
+
async createSession(signal) {
|
|
21
|
+
return requestJson("/agent/sessions", { method: "POST", signal });
|
|
22
|
+
}
|
|
23
|
+
async getSession(sessionId, signal) {
|
|
24
|
+
return requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}`, {
|
|
25
|
+
signal,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
async getSessionTraces(sessionId, signal) {
|
|
29
|
+
return requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}/traces`, { signal });
|
|
30
|
+
}
|
|
31
|
+
async renameSession(sessionId, title, signal) {
|
|
32
|
+
return requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}`, {
|
|
33
|
+
method: "PATCH",
|
|
34
|
+
body: JSON.stringify({ title }),
|
|
35
|
+
signal,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
async deleteSession(sessionId, signal) {
|
|
39
|
+
await requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}`, {
|
|
40
|
+
method: "DELETE",
|
|
41
|
+
signal,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
async abort(sessionId, signal) {
|
|
45
|
+
await requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}/abort`, {
|
|
46
|
+
method: "POST",
|
|
47
|
+
signal,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
async confirmProposal(sessionId, proposalId, signal) {
|
|
51
|
+
return requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}/proposals/${encodeURIComponent(proposalId)}/confirm`, { method: "POST", body: JSON.stringify(this.connectionPayload()), signal });
|
|
52
|
+
}
|
|
53
|
+
async rejectProposal(sessionId, proposalId, signal) {
|
|
54
|
+
return requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}/proposals/${encodeURIComponent(proposalId)}/reject`, { method: "POST", signal });
|
|
55
|
+
}
|
|
56
|
+
async *sendMessage(sessionId, message, signal) {
|
|
57
|
+
const response = await fetch(`/agent/sessions/${encodeURIComponent(sessionId)}/messages`, {
|
|
58
|
+
method: "POST",
|
|
59
|
+
headers: {
|
|
60
|
+
Accept: "application/x-ndjson",
|
|
61
|
+
"Content-Type": "application/json",
|
|
62
|
+
},
|
|
63
|
+
body: JSON.stringify({ message, ...this.connectionPayload() }),
|
|
64
|
+
signal,
|
|
65
|
+
});
|
|
66
|
+
if (!response.ok) {
|
|
67
|
+
throw await errorFromResponse(response);
|
|
68
|
+
}
|
|
69
|
+
if (!response.body) {
|
|
70
|
+
throw new AgentClientError(response.status, "empty_stream", "Agent returned an empty stream");
|
|
71
|
+
}
|
|
72
|
+
for await (const event of decodeNdjsonStream(response.body)) {
|
|
73
|
+
yield event;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
connectionPayload() {
|
|
77
|
+
return {
|
|
78
|
+
...(this.options.coreUrl ? { coreUrl: this.options.coreUrl } : {}),
|
|
79
|
+
...(this.options.token ? { token: this.options.token } : {}),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
async function requestJson(path, init = {}) {
|
|
84
|
+
const response = await fetch(path, {
|
|
85
|
+
...init,
|
|
86
|
+
headers: {
|
|
87
|
+
Accept: "application/json",
|
|
88
|
+
...(init.body ? { "Content-Type": "application/json" } : {}),
|
|
89
|
+
...init.headers,
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
if (!response.ok) {
|
|
93
|
+
throw await errorFromResponse(response);
|
|
94
|
+
}
|
|
95
|
+
if (response.status === 204) {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
return (await response.json());
|
|
99
|
+
}
|
|
100
|
+
async function errorFromResponse(response) {
|
|
101
|
+
const text = await response.text();
|
|
102
|
+
try {
|
|
103
|
+
const parsed = JSON.parse(text);
|
|
104
|
+
if (parsed.error?.code && parsed.error.message) {
|
|
105
|
+
return new AgentClientError(response.status, parsed.error.code, parsed.error.message);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
// fall through to plain text
|
|
110
|
+
}
|
|
111
|
+
return new AgentClientError(response.status, `http_${response.status}`, text || response.statusText);
|
|
112
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./agentClient.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./agentClient.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const defaultServerUrl = "http://127.0.0.1:8765";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const defaultServerUrl = "http://127.0.0.1:8765";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./mineru-convert.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./mineru-convert.js";
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
export declare const MINERU_EXTENSIONS: ReadonlySet<string>;
|
|
2
|
+
/** Cached conversions are retained for 7 days from their write time, then
|
|
3
|
+
* swept on the next time the cache is opened (issue: IndexedDB cache growth).
|
|
4
|
+
* Absolute TTL — a cache hit does not refresh ``cachedAt``. */
|
|
5
|
+
export declare const CACHE_TTL_MS: number;
|
|
6
|
+
export interface ConvertedSource {
|
|
7
|
+
input: File;
|
|
8
|
+
inputSha: string;
|
|
9
|
+
stem: string;
|
|
10
|
+
markdown: string;
|
|
11
|
+
/** "assets/<relpath>" keys (matches the wire form from sidecar). */
|
|
12
|
+
assets: Map<string, Uint8Array>;
|
|
13
|
+
}
|
|
14
|
+
export type ConvertProgress = {
|
|
15
|
+
phase: "hashing";
|
|
16
|
+
} | {
|
|
17
|
+
phase: "uploading";
|
|
18
|
+
} | {
|
|
19
|
+
phase: "polling";
|
|
20
|
+
} | {
|
|
21
|
+
phase: "downloading";
|
|
22
|
+
} | {
|
|
23
|
+
phase: "cache_hit";
|
|
24
|
+
};
|
|
25
|
+
export interface ConvertCache {
|
|
26
|
+
get(key: string): Promise<ConvertedSource | null>;
|
|
27
|
+
put(key: string, value: ConvertedSource): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
export interface ConvertOptions {
|
|
30
|
+
signal?: AbortSignal;
|
|
31
|
+
onProgress?: (e: ConvertProgress) => void;
|
|
32
|
+
cache?: ConvertCache | null;
|
|
33
|
+
/** When set, forwarded to the sidecar so it records the true original
|
|
34
|
+
* filename in frontmatter even though the uploaded File was renamed
|
|
35
|
+
* (kebab-cased per ADR 0004) for MinerU. Defaults to the uploaded File's name. */
|
|
36
|
+
originalFilename?: string;
|
|
37
|
+
/** Override for tests. */
|
|
38
|
+
fetch?: typeof fetch;
|
|
39
|
+
/** Inter-poll interval (and transient-retry wait) for the job status poll.
|
|
40
|
+
* Defaults to POLL_INTERVAL_MS; overridable for tests. */
|
|
41
|
+
pollIntervalMs?: number;
|
|
42
|
+
}
|
|
43
|
+
export type MineruConvertErrorCode = "mineru_auth" | "mineru_input" | "mineru_quota" | "mineru_timeout" | "mineru_api" | "mineru_disabled" | "aborted" | "invalid_response";
|
|
44
|
+
export declare class MineruConvertError extends Error {
|
|
45
|
+
readonly code: MineruConvertErrorCode;
|
|
46
|
+
constructor(code: MineruConvertErrorCode, message: string);
|
|
47
|
+
}
|
|
48
|
+
export declare function convertSource(file: File, opts?: ConvertOptions): Promise<ConvertedSource>;
|
|
49
|
+
export declare function convertedToFiles(c: ConvertedSource): File[];
|
|
50
|
+
/** In-memory cache. Used as a fallback when IndexedDB is unavailable (jsdom)
|
|
51
|
+
* and as a deterministic shim for tests. */
|
|
52
|
+
export declare class MemoryConvertCache implements ConvertCache {
|
|
53
|
+
private readonly store;
|
|
54
|
+
get(key: string): Promise<ConvertedSource | null>;
|
|
55
|
+
put(key: string, value: ConvertedSource): Promise<void>;
|
|
56
|
+
}
|
|
57
|
+
/** IndexedDB-backed cache. Returns null when IndexedDB is not available
|
|
58
|
+
* (jsdom test env, very old browsers). Caller should treat ``null`` as
|
|
59
|
+
* "no cache" and proceed without bypass. */
|
|
60
|
+
export declare function tryOpenDefaultCache(options?: {
|
|
61
|
+
namespace?: string;
|
|
62
|
+
}): Promise<ConvertCache | null>;
|
|
63
|
+
/** A cache entry is expired when it was written more than CACHE_TTL_MS ago.
|
|
64
|
+
* Exactly at the TTL is kept (strictly-older test). A missing or non-finite
|
|
65
|
+
* ``cachedAt`` (legacy / corrupt record) is treated as expired so the sweep
|
|
66
|
+
* reclaims it. Future-dated entries (clock skew) are kept. */
|
|
67
|
+
export declare function isCacheEntryExpired(cachedAt: unknown, now: number, ttlMs?: number): boolean;
|
|
68
|
+
export declare class IDBConvertCache implements ConvertCache {
|
|
69
|
+
private readonly db;
|
|
70
|
+
constructor(db: IDBDatabase);
|
|
71
|
+
/** Walk every entry with a readwrite cursor and delete those older than
|
|
72
|
+
* CACHE_TTL_MS. A cursor (rather than getAll) keeps only one record —
|
|
73
|
+
* which carries the full markdown + asset bytes — in memory at a time.
|
|
74
|
+
* Resolves on transaction commit (so the deletes are durable, and a
|
|
75
|
+
* delete-triggered abort surfaces as a rejection rather than a false
|
|
76
|
+
* success); rejects on a transaction/cursor error so the caller can
|
|
77
|
+
* swallow it. ``now`` is injectable for tests. */
|
|
78
|
+
sweepExpired(now?: number): Promise<void>;
|
|
79
|
+
get(key: string): Promise<ConvertedSource | null>;
|
|
80
|
+
put(key: string, value: ConvertedSource): Promise<void>;
|
|
81
|
+
private txGet;
|
|
82
|
+
private txPut;
|
|
83
|
+
}
|