liwenjson 0.0.0-stage → 1.5.2
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 +201 -0
- package/NOTICE +26 -0
- package/README.md +125 -2
- package/index.cjs +239 -0
- package/index.d.ts +175 -0
- package/index.mjs +22 -0
- package/mcp.cjs +88 -0
- package/package.json +60 -4
- package/vendor/liwenjson.js +18958 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
LIWENJSON
|
|
2
|
+
Copyright 2026 wenhuang2333
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (Apache-2.0).
|
|
4
|
+
See the LICENSE file in this directory for the full license text.
|
|
5
|
+
|
|
6
|
+
Build dependency
|
|
7
|
+
The project's only build dependency is the MoonBit standard library,
|
|
8
|
+
package `moonbitlang/core`, which is licensed under Apache-2.0.
|
|
9
|
+
|
|
10
|
+
Test-only oracles (not part of the product)
|
|
11
|
+
The test suite uses the following third-party libraries purely as
|
|
12
|
+
differential-testing oracles. They are never linked into, bundled with, or
|
|
13
|
+
copied by the product:
|
|
14
|
+
|
|
15
|
+
* `jsonrepair` (npm package `jsonrepair`) — TypeScript/JavaScript,
|
|
16
|
+
licensed under ISC.
|
|
17
|
+
* `json-repair` (PyPI package `json-repair`) — Python, licensed under MIT.
|
|
18
|
+
|
|
19
|
+
No source code from either library is included in this repository or in any
|
|
20
|
+
build artifact; they are executed out of process solely to compare behavior,
|
|
21
|
+
and their output is used for test reporting and documentation of intentional
|
|
22
|
+
divergences.
|
|
23
|
+
|
|
24
|
+
Licenses were verified on 2026-10-14 against the npm registry metadata for
|
|
25
|
+
`jsonrepair` 3.15.0 and the PyPI metadata and repository LICENSE for
|
|
26
|
+
`json-repair` 0.64.0.
|
package/README.md
CHANGED
|
@@ -1,3 +1,126 @@
|
|
|
1
|
-
#
|
|
1
|
+
# liwenjson (npm)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
LIWENJSON is the **execution gate for LLM output**: it decides whether a model's
|
|
4
|
+
JSON may be executed, and when it may not, returns the sentence to send back to
|
|
5
|
+
the model. This package is the MoonBit engine compiled to JavaScript — streaming
|
|
6
|
+
repair, partial parsing, and the gate for in-process callers — plus a launcher
|
|
7
|
+
that starts the local MCP server from the native binary.
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install liwenjson
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
> The package name is **`liwenjson`** — the bare name, no suffix. `1.5.2` is the
|
|
14
|
+
> first release published under it; `1.5.1` reached the npm registry on
|
|
15
|
+
> 2026-10-30 as `interjson-moonbit`.
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
import { analyze, repair } from "liwenjson";
|
|
19
|
+
|
|
20
|
+
// The gate: decide before executing tool arguments.
|
|
21
|
+
const plan = analyze(modelOutput, {
|
|
22
|
+
profile: "agent",
|
|
23
|
+
schemaJson: tool.inputSchema, // the declaration you already have
|
|
24
|
+
});
|
|
25
|
+
if (!plan.executable) {
|
|
26
|
+
throw new Error(plan.retry.prompt); // or re-emit `plan.retry.prompt` to the model
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// The convenience: repair and return text.
|
|
30
|
+
const text = repair(modelOutput, { profile: "execution" });
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## MCP server
|
|
34
|
+
|
|
35
|
+
The package declares a bin named **`liwenjson-mcp`**, which runs `mcp.cjs`. It
|
|
36
|
+
resolves the native engine and then spawns `<binary> mcp` with inherited stdio,
|
|
37
|
+
so the JSON-RPC channel stays clean (no Node process in the middle, nothing on
|
|
38
|
+
stdout but protocol messages). Resolution order:
|
|
39
|
+
|
|
40
|
+
1. `LIWENJSON_BIN` — an absolute path to the binary;
|
|
41
|
+
2. `liwenjson` on `PATH`;
|
|
42
|
+
3. the development build inside this repository.
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
npm install -g liwenjson
|
|
46
|
+
liwenjson --version # the native engine must resolve first
|
|
47
|
+
liwenjson-mcp --version # passed through to `<binary> mcp --version`
|
|
48
|
+
liwenjson-mcp --print-config=claude
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`npm install` installs the JS engine and the launcher, not the native binary.
|
|
52
|
+
If the binary is missing, `liwenjson-mcp` exits `127` with the install options
|
|
53
|
+
on stderr — the first command above must succeed before the others can.
|
|
54
|
+
|
|
55
|
+
**The npm package is the JS engine, not the MCP server.** The server itself is
|
|
56
|
+
the native binary; the launcher only finds it and hands it this process's
|
|
57
|
+
stdin/stdout. MCP therefore requires the native engine: build it with
|
|
58
|
+
`moon build cmd/liwenjson --target native --release`, or point `LIWENJSON_BIN`
|
|
59
|
+
at a binary. With no binary the launcher exits `127` and prints those options
|
|
60
|
+
on stderr rather than starting a half-working server.
|
|
61
|
+
|
|
62
|
+
Configuration for Claude Code, Cursor, Cline, Windsurf, VS Code, Codex CLI and
|
|
63
|
+
Zed, with the privacy model and troubleshooting, is in
|
|
64
|
+
[`docs/MCP.md`](../docs/MCP.md). The shortest form, with the binary on `PATH`:
|
|
65
|
+
|
|
66
|
+
```json
|
|
67
|
+
{
|
|
68
|
+
"mcpServers": {
|
|
69
|
+
"liwenjson": { "command": "liwenjson-mcp" }
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
The server exposes four read-only tools: `liwenjson_gate` (the gate; `executable`,
|
|
75
|
+
`decision`, `exactness`, `value`, `retry.prompt`), `liwenjson_repair`,
|
|
76
|
+
`liwenjson_validate`, and `liwenjson_readiness`. The gate defaults to the
|
|
77
|
+
`agent` profile, which equals `Options::for_agent()` — prose before the
|
|
78
|
+
arguments is skipped rather than refused. A refused result always carries a
|
|
79
|
+
non-empty `retry.prompt` and `value: null`; `content[0].text` is a summary and
|
|
80
|
+
`structuredContent` is the payload. **MCP is advisory**: the model can decline
|
|
81
|
+
to call the gate, so the host must gate the call itself (a Claude Code
|
|
82
|
+
`PreToolUse` hook or a host-SDK `gate()` — see
|
|
83
|
+
[`docs/INTEGRATIONS.md`](../docs/INTEGRATIONS.md#making-the-gate-mandatory)).
|
|
84
|
+
|
|
85
|
+
## Exports
|
|
86
|
+
|
|
87
|
+
| Export | Returns | Use |
|
|
88
|
+
| --- | --- | --- |
|
|
89
|
+
| `repair(input, options?)` | repaired text | a document you will parse yourself |
|
|
90
|
+
| `repairResult(input, options?)` | `{text, exactness, ambiguous, repairs, decision, executable, error, retry}` | text plus the decision |
|
|
91
|
+
| `analyze(input, options?)` | the full `RepairPlan` | **the gate**; never throws on malformed JSON |
|
|
92
|
+
| `report(input)` | `R005 3:14 message` lines, or `-` | diagnostics |
|
|
93
|
+
| `snapshot(input, upto)` | `value \| state \| path` | partial parse of a prefix |
|
|
94
|
+
| `live(input, upto, schema?)` | one streaming frame object | a UI that renders a model stream |
|
|
95
|
+
|
|
96
|
+
`options.profile` is one of `permissive`, `interactive`, `execution`, `agent`,
|
|
97
|
+
`strict`. `execution` and `agent` refuse a lossy or ambiguous repair instead of
|
|
98
|
+
inventing content; `repair` then throws, and `analyze` returns
|
|
99
|
+
`executable: false` with `error.code` and `retry`.
|
|
100
|
+
|
|
101
|
+
`options.schema` is the library's declaration language
|
|
102
|
+
(`{!query:string,!limit:integer}`). `options.schemaJson` is a JSON Schema
|
|
103
|
+
subset — `type`, `properties`, `required`, `items`, `enum`, `const`,
|
|
104
|
+
`minimum`, `maximum`, `exclusiveMinimum`, `exclusiveMaximum`, `minLength`,
|
|
105
|
+
`maxLength`, `pattern` — so a tool's existing `inputSchema` can be passed
|
|
106
|
+
directly. Unsupported keywords raise `E070` rather than being silently
|
|
107
|
+
dropped.
|
|
108
|
+
|
|
109
|
+
## Streaming
|
|
110
|
+
|
|
111
|
+
`live` is the same frame the [demo page](https://wenhuang2333.github.io/LIWENJSON/live.html)
|
|
112
|
+
draws: `consumed`, `total`, `completeness`, `snapshot`, `open_path`, `members`,
|
|
113
|
+
and (with a schema) `syntax_ready`, `schema_ready`, `settled_ready`,
|
|
114
|
+
`execution_ready`. It is stateless, so a caller can re-render on every frame;
|
|
115
|
+
for a token-by-token feed use the CLI protocol (`liwenjson serve`) or
|
|
116
|
+
the MoonBit `PartialParser`.
|
|
117
|
+
|
|
118
|
+
## Scope
|
|
119
|
+
|
|
120
|
+
This package wraps the same bundle as `static/demo/liwenjson.js`; it does not
|
|
121
|
+
reimplement the engine. The version is the engine version (`1.5.1`), published
|
|
122
|
+
to npm on 2026-10-30. The MoonBit package on mooncakes is `0.13.0` because the
|
|
123
|
+
registry currently accepts only `0.x.y`; the bytes are the same. `0.13.0` was
|
|
124
|
+
published on 2026-10-29, and `0.12.0` (the 1.5.0 correspondence) on 2026-10-28.
|
|
125
|
+
|
|
126
|
+
License: Apache-2.0.
|
package/index.cjs
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Node entry point. The MoonBit engine is compiled to a plain JavaScript
|
|
4
|
+
// script that registers its entry points through three host hooks. This module
|
|
5
|
+
// installs the hooks, loads the script once, and exports the captured
|
|
6
|
+
// functions. The global namespace is restored afterwards, so importing
|
|
7
|
+
// `liwenjson` does not leave `liwenjsonRegister*` or a `liwenjson` object behind.
|
|
8
|
+
|
|
9
|
+
const path = require("node:path");
|
|
10
|
+
|
|
11
|
+
const HOOKS = ["liwenjsonRegister1", "liwenjsonRegister2", "liwenjsonRegister3"];
|
|
12
|
+
|
|
13
|
+
function loadEngine() {
|
|
14
|
+
const registered = Object.create(null);
|
|
15
|
+
const previous = Object.create(null);
|
|
16
|
+
for (const hook of HOOKS) {
|
|
17
|
+
previous[hook] = globalThis[hook];
|
|
18
|
+
globalThis[hook] = (name, fn) => {
|
|
19
|
+
registered[name] = fn;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
require(path.join(__dirname, "vendor", "liwenjson.js"));
|
|
24
|
+
} finally {
|
|
25
|
+
for (const hook of HOOKS) {
|
|
26
|
+
if (previous[hook] === undefined) {
|
|
27
|
+
delete globalThis[hook];
|
|
28
|
+
} else {
|
|
29
|
+
globalThis[hook] = previous[hook];
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return registered;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const engine = loadEngine();
|
|
37
|
+
|
|
38
|
+
function requireString(name, value) {
|
|
39
|
+
if (typeof value !== "string") {
|
|
40
|
+
throw new TypeError(`liwenjson.${name}: input must be a string`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function profileOf(options) {
|
|
45
|
+
const profile = options && options.profile ? String(options.profile) : "";
|
|
46
|
+
if (profile && !["permissive", "interactive", "execution", "agent", "strict"].includes(profile)) {
|
|
47
|
+
throw new TypeError(`liwenjson: unknown profile "${profile}"`);
|
|
48
|
+
}
|
|
49
|
+
return profile;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* A structured refusal from the engine.
|
|
54
|
+
*
|
|
55
|
+
* The engine's string channel reports a failure as `ERROR: E010 ...`, and the
|
|
56
|
+
* package used to detect that with a prefix test and throw a bare `Error`
|
|
57
|
+
* whose message was the rest of the line. A caller that wanted the code, or
|
|
58
|
+
* the position, had to parse the message back out of an exception. This class
|
|
59
|
+
* keeps them as fields, and it still subclasses `Error`, so `catch (e) { e.message }`
|
|
60
|
+
* keeps working.
|
|
61
|
+
*/
|
|
62
|
+
class LIWENJSONError extends Error {
|
|
63
|
+
constructor(message) {
|
|
64
|
+
super(message);
|
|
65
|
+
this.name = "LIWENJSONError";
|
|
66
|
+
const match = /^(E\d{3})\b\s*(.*)$/.exec(message);
|
|
67
|
+
this.code = match ? match[1] : "";
|
|
68
|
+
this.detail = match ? match[2] : message;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function unwrap(text) {
|
|
73
|
+
if (text.startsWith("ERROR: ")) {
|
|
74
|
+
throw new LIWENJSONError(text.slice("ERROR: ".length));
|
|
75
|
+
}
|
|
76
|
+
return text;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Repair `input` and return the repaired text.
|
|
81
|
+
*
|
|
82
|
+
* With `options.profile` set, the safety profile decides what may be repaired:
|
|
83
|
+
* `agent` and `execution` refuse a lossy or ambiguous repair instead of
|
|
84
|
+
* guessing, and throw.
|
|
85
|
+
*/
|
|
86
|
+
function repair(input, options = {}) {
|
|
87
|
+
requireString("repair", input);
|
|
88
|
+
const profile = profileOf(options);
|
|
89
|
+
// A declaration is honoured, not ignored. This function used to read only
|
|
90
|
+
// `profile` and silently drop `schema`/`schemaJson`, so
|
|
91
|
+
// `repair('{"a":1}', {schemaJson: {...a: string}})` returned `{"a":1}` while
|
|
92
|
+
// the Python client and the MCP `liwenjson_repair` tool refused it (audit
|
|
93
|
+
// 4.2). The engine has no separate "repair against a declaration" entry
|
|
94
|
+
// point, and the decision fields `analyze` computes are the same check the
|
|
95
|
+
// other two entry points make, so the declaration path goes through
|
|
96
|
+
// `analyze` and returns its repaired text.
|
|
97
|
+
if (options.schemaJson !== undefined || options.schema !== undefined) {
|
|
98
|
+
const plan = analyze(input, {
|
|
99
|
+
profile: profile || "agent",
|
|
100
|
+
...(options.schemaJson !== undefined ? { schemaJson: options.schemaJson } : {}),
|
|
101
|
+
...(options.schema !== undefined ? { schema: options.schema } : {}),
|
|
102
|
+
});
|
|
103
|
+
if (!plan.executable) {
|
|
104
|
+
const reason = plan.error && plan.error.message ? plan.error.message : plan.decision;
|
|
105
|
+
throw new LIWENJSONError(
|
|
106
|
+
`${plan.error && plan.error.code ? plan.error.code : "E070"} ${reason}`,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
return plan.text === undefined ? plan.value === null ? "" : JSON.stringify(plan.value) : plan.text;
|
|
110
|
+
}
|
|
111
|
+
return unwrap(profile ? engine.repair_opts(input, profile) : engine.repair(input));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The full decision plan. This is the call an agent integration should use
|
|
116
|
+
* before executing tool arguments: it never throws on malformed JSON, and its
|
|
117
|
+
* `executable` field is the gate.
|
|
118
|
+
*/
|
|
119
|
+
function analyze(input, options = {}) {
|
|
120
|
+
requireString("analyze", input);
|
|
121
|
+
const profile = profileOf(options);
|
|
122
|
+
let plan;
|
|
123
|
+
if (options.schemaJson !== undefined) {
|
|
124
|
+
const json =
|
|
125
|
+
typeof options.schemaJson === "string"
|
|
126
|
+
? options.schemaJson
|
|
127
|
+
: JSON.stringify(options.schemaJson);
|
|
128
|
+
plan = JSON.parse(engine.analyze_json_schema(input, profile, json));
|
|
129
|
+
} else {
|
|
130
|
+
plan = JSON.parse(engine.analyze(input, profile, options.schema || ""));
|
|
131
|
+
}
|
|
132
|
+
if (plan.decision === undefined) {
|
|
133
|
+
throw new Error(plan.error || "liwenjson.analyze: could not build a plan");
|
|
134
|
+
}
|
|
135
|
+
return plan;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Repair plus the decision fields, as one object.
|
|
140
|
+
*/
|
|
141
|
+
function repairResult(input, options = {}) {
|
|
142
|
+
const plan = analyze(input, options);
|
|
143
|
+
return {
|
|
144
|
+
text: plan.text,
|
|
145
|
+
exactness: plan.exactness,
|
|
146
|
+
ambiguous: plan.ambiguous,
|
|
147
|
+
repairs: plan.repairs,
|
|
148
|
+
decision: plan.decision,
|
|
149
|
+
executable: plan.executable,
|
|
150
|
+
error: plan.error,
|
|
151
|
+
retry: plan.retry,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** One `R005 3:14 message` line per repair, or `-`. */
|
|
156
|
+
function report(input) {
|
|
157
|
+
requireString("report", input);
|
|
158
|
+
return engine.report(input);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* The partial value of the first `upto` code units.
|
|
163
|
+
*
|
|
164
|
+
* The engine's own snapshot is a `value | completeness | open path` string,
|
|
165
|
+
* which is what the browser demo draws. A programmatic caller wants the
|
|
166
|
+
* three fields separately, so they are returned as an object: the string
|
|
167
|
+
* form is still available through `snapshotText`.
|
|
168
|
+
*/
|
|
169
|
+
function snapshot(input, upto) {
|
|
170
|
+
const parts = snapshotText(input, upto).split(" | ");
|
|
171
|
+
return {
|
|
172
|
+
value: parts.length > 0 && parts[0] !== "-" ? JSON.parse(parts[0]) : null,
|
|
173
|
+
completeness: parts.length > 1 ? parts[1] : "",
|
|
174
|
+
openPath: parts.length > 2 ? parts[2] : "",
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** The partial value of the first `upto` code units, as `value | state | path`. */
|
|
179
|
+
function snapshotText(input, upto) {
|
|
180
|
+
requireString("snapshot", input);
|
|
181
|
+
return engine.snapshot(input, upto | 0);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** One frame of the streaming view, as an object. */
|
|
185
|
+
function live(input, upto, schema = "") {
|
|
186
|
+
requireString("live", input);
|
|
187
|
+
return JSON.parse(engine.live(input, upto | 0, schema));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The readiness curve for `input` against a skeleton declaration: the three
|
|
192
|
+
* document-level numbers, every field-ready event, and the verdict.
|
|
193
|
+
*/
|
|
194
|
+
function readiness(input, schema = "") {
|
|
195
|
+
requireString("readiness", input);
|
|
196
|
+
return JSON.parse(engine.readiness(input, schema));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* The delta stream for the first `upto` code units: the protocol a consumer
|
|
201
|
+
* subscribes to instead of re-reading the document on every frame.
|
|
202
|
+
*/
|
|
203
|
+
function deltas(input, upto) {
|
|
204
|
+
requireString("deltas", input);
|
|
205
|
+
return JSON.parse(engine.deltas(input, upto | 0));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* The gate, with the profile the MCP tools use.
|
|
210
|
+
*
|
|
211
|
+
* `analyze` with no profile used to run the *permissive* profile -- the
|
|
212
|
+
* library's default -- while the MCP gate, which is the product, runs the
|
|
213
|
+
* `agent` profile. A caller who read the MCP documentation and then called
|
|
214
|
+
* the npm package got a different answer from the same input. The npm package
|
|
215
|
+
* is the same product, so it defaults to the same profile; `permissive` is
|
|
216
|
+
* one explicit argument away.
|
|
217
|
+
*/
|
|
218
|
+
function gate(input, options = {}) {
|
|
219
|
+
return analyze(input, { profile: "agent", ...options });
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
module.exports = {
|
|
223
|
+
LIWENJSONError,
|
|
224
|
+
repair,
|
|
225
|
+
repairResult,
|
|
226
|
+
analyze,
|
|
227
|
+
gate,
|
|
228
|
+
report,
|
|
229
|
+
snapshot,
|
|
230
|
+
snapshotText,
|
|
231
|
+
live,
|
|
232
|
+
readiness,
|
|
233
|
+
deltas,
|
|
234
|
+
// The engine version is the package version: one declaration, one source.
|
|
235
|
+
// A literal here went stale once already (it said 1.1.0 while the
|
|
236
|
+
// package was 1.1.1), and nothing noticed, because the test pinned the
|
|
237
|
+
// same stale literal.
|
|
238
|
+
version: require("./package.json").version,
|
|
239
|
+
};
|