prefix-safe-json 0.0.1-alpha.0
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-APACHE +114 -0
- package/LICENSE-MIT +21 -0
- package/README.md +169 -0
- package/dist/coordinator/coordinator.d.ts +43 -0
- package/dist/coordinator/coordinator.d.ts.map +1 -0
- package/dist/coordinator/coordinator.js +407 -0
- package/dist/coordinator/coordinator.js.map +1 -0
- package/dist/coordinator/protocol.d.ts +61 -0
- package/dist/coordinator/protocol.d.ts.map +1 -0
- package/dist/coordinator/protocol.js +2 -0
- package/dist/coordinator/protocol.js.map +1 -0
- package/dist/coordinator/types.d.ts +84 -0
- package/dist/coordinator/types.d.ts.map +1 -0
- package/dist/coordinator/types.js +7 -0
- package/dist/coordinator/types.js.map +1 -0
- package/dist/diagnostics/codes.d.ts +24 -0
- package/dist/diagnostics/codes.d.ts.map +1 -0
- package/dist/diagnostics/codes.js +28 -0
- package/dist/diagnostics/codes.js.map +1 -0
- package/dist/diagnostics/factory.d.ts +8 -0
- package/dist/diagnostics/factory.d.ts.map +1 -0
- package/dist/diagnostics/factory.js +17 -0
- package/dist/diagnostics/factory.js.map +1 -0
- package/dist/grammar/frame.d.ts +58 -0
- package/dist/grammar/frame.d.ts.map +1 -0
- package/dist/grammar/frame.js +87 -0
- package/dist/grammar/frame.js.map +1 -0
- package/dist/grammar/pointer.d.ts +10 -0
- package/dist/grammar/pointer.d.ts.map +1 -0
- package/dist/grammar/pointer.js +20 -0
- package/dist/grammar/pointer.js.map +1 -0
- package/dist/grammar/stack.d.ts +73 -0
- package/dist/grammar/stack.d.ts.map +1 -0
- package/dist/grammar/stack.js +168 -0
- package/dist/grammar/stack.js.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/lexer/scanner.d.ts +108 -0
- package/dist/lexer/scanner.d.ts.map +1 -0
- package/dist/lexer/scanner.js +676 -0
- package/dist/lexer/scanner.js.map +1 -0
- package/dist/lexer/states.d.ts +35 -0
- package/dist/lexer/states.d.ts.map +1 -0
- package/dist/lexer/states.js +39 -0
- package/dist/lexer/states.js.map +1 -0
- package/dist/lexer/tokens.d.ts +22 -0
- package/dist/lexer/tokens.d.ts.map +1 -0
- package/dist/lexer/tokens.js +22 -0
- package/dist/lexer/tokens.js.map +1 -0
- package/dist/limits.d.ts +14 -0
- package/dist/limits.d.ts.map +1 -0
- package/dist/limits.js +23 -0
- package/dist/limits.js.map +1 -0
- package/dist/parser.d.ts +6 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +949 -0
- package/dist/parser.js.map +1 -0
- package/dist/providers/adapter.d.ts +10 -0
- package/dist/providers/adapter.d.ts.map +1 -0
- package/dist/providers/adapter.js +2 -0
- package/dist/providers/adapter.js.map +1 -0
- package/dist/providers/anthropic.d.ts +45 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +112 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/gemini.d.ts +31 -0
- package/dist/providers/gemini.d.ts.map +1 -0
- package/dist/providers/gemini.js +94 -0
- package/dist/providers/gemini.js.map +1 -0
- package/dist/providers/openai-compatible.d.ts +33 -0
- package/dist/providers/openai-compatible.d.ts.map +1 -0
- package/dist/providers/openai-compatible.js +160 -0
- package/dist/providers/openai-compatible.js.map +1 -0
- package/dist/providers/openai.d.ts +48 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +205 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/openrouter.d.ts +35 -0
- package/dist/providers/openrouter.d.ts.map +1 -0
- package/dist/providers/openrouter.js +55 -0
- package/dist/providers/openrouter.js.map +1 -0
- package/dist/semantic/builder.d.ts +55 -0
- package/dist/semantic/builder.d.ts.map +1 -0
- package/dist/semantic/builder.js +140 -0
- package/dist/semantic/builder.js.map +1 -0
- package/dist/semantic/events.d.ts +2 -0
- package/dist/semantic/events.d.ts.map +1 -0
- package/dist/semantic/events.js +5 -0
- package/dist/semantic/events.js.map +1 -0
- package/dist/semantic/snapshot.d.ts +49 -0
- package/dist/semantic/snapshot.d.ts.map +1 -0
- package/dist/semantic/snapshot.js +228 -0
- package/dist/semantic/snapshot.js.map +1 -0
- package/dist/types.d.ts +156 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/dist/utf8/decoder.d.ts +53 -0
- package/dist/utf8/decoder.d.ts.map +1 -0
- package/dist/utf8/decoder.js +318 -0
- package/dist/utf8/decoder.js.map +1 -0
- package/package.json +79 -0
package/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
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.
|
|
18
|
+
|
|
19
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
20
|
+
exercising permissions granted by this License.
|
|
21
|
+
|
|
22
|
+
"Source" form shall mean the preferred form for making modifications.
|
|
23
|
+
|
|
24
|
+
"Object" form shall mean any form resulting from mechanical
|
|
25
|
+
transformation or translation of a Source form.
|
|
26
|
+
|
|
27
|
+
"Work" shall mean the work of authorship made available under the License.
|
|
28
|
+
|
|
29
|
+
"Contribution" shall mean any work of authorship submitted to the
|
|
30
|
+
Licensor for inclusion in the Work.
|
|
31
|
+
|
|
32
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of whom
|
|
33
|
+
a Contribution has been received by the Licensor.
|
|
34
|
+
|
|
35
|
+
2. Grant of Copyright License.
|
|
36
|
+
|
|
37
|
+
Subject to the terms and conditions of this License, each Contributor
|
|
38
|
+
hereby grants to You a perpetual, worldwide, non-exclusive, no-charge,
|
|
39
|
+
royalty-free, irrevocable copyright license to reproduce, prepare
|
|
40
|
+
Derivative Works of, publicly display, publicly perform, sublicense,
|
|
41
|
+
and distribute the Work and such Derivative Works in Source or Object form.
|
|
42
|
+
|
|
43
|
+
3. Grant of Patent License.
|
|
44
|
+
|
|
45
|
+
Subject to the terms and conditions of this License, each Contributor
|
|
46
|
+
hereby grants to You a perpetual, worldwide, non-exclusive, no-charge,
|
|
47
|
+
royalty-free, irrevocable patent license to make, have made, use, offer
|
|
48
|
+
to sell, sell, import, and otherwise transfer the Work.
|
|
49
|
+
|
|
50
|
+
4. Redistribution.
|
|
51
|
+
|
|
52
|
+
You may reproduce and distribute copies of the Work or Derivative Works
|
|
53
|
+
thereof in any medium, with or without modifications, and in Source or
|
|
54
|
+
Object form, provided that You meet the following conditions:
|
|
55
|
+
|
|
56
|
+
(a) You must give any other recipients of the Work or Derivative Works
|
|
57
|
+
a copy of this License; and
|
|
58
|
+
|
|
59
|
+
(b) You must cause any modified files to carry prominent notices stating
|
|
60
|
+
that You changed the files; and
|
|
61
|
+
|
|
62
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
63
|
+
distribute, all copyright, patent, trademark, and attribution notices
|
|
64
|
+
from the Source form of the Work; and
|
|
65
|
+
|
|
66
|
+
(d) If the Work includes a "NOTICE" text file, You must include a readable
|
|
67
|
+
copy of the attribution notices contained within such NOTICE file.
|
|
68
|
+
|
|
69
|
+
5. Submission of Contributions.
|
|
70
|
+
|
|
71
|
+
Unless You explicitly state otherwise, any Contribution intentionally
|
|
72
|
+
submitted for inclusion in the Work by You to the Licensor shall be
|
|
73
|
+
under the terms and conditions of this License, without any additional
|
|
74
|
+
terms or conditions.
|
|
75
|
+
|
|
76
|
+
6. Trademarks.
|
|
77
|
+
|
|
78
|
+
This License does not grant permission to use the trade names, trademarks,
|
|
79
|
+
service marks, or product names of the Licensor.
|
|
80
|
+
|
|
81
|
+
7. Disclaimer of Warranty.
|
|
82
|
+
|
|
83
|
+
Unless required by applicable law or agreed to in writing, Licensor
|
|
84
|
+
provides the Work on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
|
|
85
|
+
OF ANY KIND.
|
|
86
|
+
|
|
87
|
+
8. Limitation of Liability.
|
|
88
|
+
|
|
89
|
+
In no event and under no legal theory shall any Contributor be liable to
|
|
90
|
+
You for damages, including any direct, indirect, special, incidental, or
|
|
91
|
+
consequential damages of any character arising as a result of this License
|
|
92
|
+
or out of the use or inability to use the Work.
|
|
93
|
+
|
|
94
|
+
9. Accepting Warranty or Additional Liability.
|
|
95
|
+
|
|
96
|
+
While redistributing the Work or Derivative Works thereof, You may choose
|
|
97
|
+
to offer, and charge a fee for, acceptance of support, warranty, indemnity,
|
|
98
|
+
or other liability obligations consistent with this License.
|
|
99
|
+
|
|
100
|
+
END OF TERMS AND CONDITIONS
|
|
101
|
+
|
|
102
|
+
Copyright (c) 2026 Contributors
|
|
103
|
+
|
|
104
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
105
|
+
you may not use this file except in compliance with the License.
|
|
106
|
+
You may obtain a copy of the License at
|
|
107
|
+
|
|
108
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
109
|
+
|
|
110
|
+
Unless required by applicable law or agreed to in writing, software
|
|
111
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
112
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
113
|
+
See the License for the specific language governing permissions and
|
|
114
|
+
limitations under the License.
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 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,169 @@
|
|
|
1
|
+
# prefix-safe-json
|
|
2
|
+
|
|
3
|
+
> ⚠️ **Alpha** — This project is under active development. APIs may change without notice. Do not use in production.
|
|
4
|
+
|
|
5
|
+
Incremental LLM tool-call JSON parser and deterministic repair engine.
|
|
6
|
+
|
|
7
|
+
## Problem
|
|
8
|
+
|
|
9
|
+
LLM providers (OpenAI, Anthropic, Gemini, OpenRouter) stream tool-call arguments as small JSON chunks. During streaming, the JSON is frequently incomplete: strings split mid-word, UTF-8 characters split mid-byte, containers left unclosed, numbers terminated at chunk boundaries.
|
|
10
|
+
|
|
11
|
+
This library parses each chunk incrementally and provides:
|
|
12
|
+
|
|
13
|
+
- **Committed values**: Fields whose values are definitively complete
|
|
14
|
+
- **Pending tokens**: Partially received values awaiting more data
|
|
15
|
+
- **Monotonic semantic events**: JSON Pointer-based events that are never retracted
|
|
16
|
+
- **Parse diagnostics**: Structured error reporting with severity and recoverability
|
|
17
|
+
- **Deterministic repair**: When the stream ends, a clear accounting of what was repaired and why
|
|
18
|
+
- **Execution safety**: Whether the parsed result is safe to use for tool invocation
|
|
19
|
+
|
|
20
|
+
## Core Principle
|
|
21
|
+
|
|
22
|
+
> The parser never fabricates missing data. It clearly distinguishes what is definite, what is pending, what was deterministically repaired, and what is lost.
|
|
23
|
+
|
|
24
|
+
## Why not a general "partial JSON" parser?
|
|
25
|
+
|
|
26
|
+
Most streaming JSON helpers (including ones already used in the wild for
|
|
27
|
+
LLM tool-call arguments) are designed to show *something* as early as
|
|
28
|
+
possible, even if that means guessing. Concretely, given a stream that has
|
|
29
|
+
so far delivered `{"city":"Tok` (the model is still typing "Tokyo"):
|
|
30
|
+
|
|
31
|
+
| | `city` field after this chunk |
|
|
32
|
+
|---|---|
|
|
33
|
+
| [vercel/ai's `fixJson`](https://github.com/vercel/ai/blob/main/packages/ai/src/util/fix-json.ts) | `"Tok"` — closes the open string as-is and reports a successful parse |
|
|
34
|
+
| [langchain's `parsePartialJson`](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-core/src/utils/json.ts) | `"Tok"` — its recursive-descent parser returns whatever string content it collected before running out of input |
|
|
35
|
+
| **this library** (`snapshot().stableValue`) | *(absent — `city` is not committed yet)* |
|
|
36
|
+
|
|
37
|
+
Both of those are real, current, well-engineered implementations — verified
|
|
38
|
+
by cloning `vercel/ai` (commit `fbb154a0`) and `langchain-ai/langchainjs`
|
|
39
|
+
(commit `555d6f14`) and running their actual code side-by-side with this
|
|
40
|
+
library. Neither is a bug: they're built for progressively rendering a
|
|
41
|
+
value in a UI, where showing `"Tok"` and then `"Tokyo"` a moment later is
|
|
42
|
+
good UX. This library is built for the different question — *is it safe to
|
|
43
|
+
act on this value yet* — where treating `"Tok"` as the city would be
|
|
44
|
+
wrong. It's why `city` only appears in `stableValue` once its closing
|
|
45
|
+
quote genuinely arrives, and why `executable` only becomes `true` once the
|
|
46
|
+
whole document is unambiguously, definitively complete.
|
|
47
|
+
|
|
48
|
+
If you need live "filling in..." UI text, a partial-JSON renderer is the
|
|
49
|
+
right tool. If you need to know precisely when it's safe to execute a tool
|
|
50
|
+
call with the parsed arguments, that's what this library is for.
|
|
51
|
+
|
|
52
|
+
### A concrete case where this matters: silent execution on truncated data
|
|
53
|
+
|
|
54
|
+
The risk above isn't hypothetical. [Cline](https://github.com/cline/cline)
|
|
55
|
+
(a coding agent that writes files and runs terminal commands from LLM tool
|
|
56
|
+
calls — verified by cloning it, commit `81cce3d70e1`) wires a JSON-repair
|
|
57
|
+
step directly into tool execution:
|
|
58
|
+
[`sdk/packages/llms/src/providers/ai-sdk.ts:1332`](https://github.com/cline/cline/blob/81cce3d70e10244cdde40dbd0eb0bb711c93006d/sdk/packages/llms/src/providers/ai-sdk.ts#L1332)
|
|
59
|
+
registers `repairMalformedToolCall` as the Vercel AI SDK's
|
|
60
|
+
`experimental_repairToolCall` hook — whatever it returns *is* the tool call
|
|
61
|
+
that runs. That function's own docstring names its target: "truncated
|
|
62
|
+
payloads ... common with weaker models." Its repair path
|
|
63
|
+
([`sdk/packages/shared/src/parse/json.ts:42`](https://github.com/cline/cline/blob/81cce3d70e10244cdde40dbd0eb0bb711c93006d/sdk/packages/shared/src/parse/json.ts#L42))
|
|
64
|
+
runs the raw argument text through
|
|
65
|
+
[`jsonrepair`](https://github.com/josdejong/jsonrepair), which — like the
|
|
66
|
+
two implementations above — closes an unterminated string as-is rather
|
|
67
|
+
than rejecting it.
|
|
68
|
+
|
|
69
|
+
Feeding `parseJsonStream` (copied verbatim from that file) and this
|
|
70
|
+
library the same truncated `write_file` tool call — a `content` argument
|
|
71
|
+
cut off mid-value, the exact scenario the docstring describes:
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
input: {"path":"config/database.yml","content":"production:\n host: db.prod.internal\n password: correct-horse-battery-sta
|
|
75
|
+
|
|
76
|
+
Cline's parseJsonStream() -> jsonrepair:
|
|
77
|
+
{ "path": "config/database.yml",
|
|
78
|
+
"content": "production:\n host: db.prod.internal\n password: correct-horse-battery-sta" }
|
|
79
|
+
No error, no warning. This is what gets written to disk.
|
|
80
|
+
|
|
81
|
+
This library:
|
|
82
|
+
outcome: "truncated", executable: false
|
|
83
|
+
stableValue: { "path": "config/database.yml" } // content is absent, not guessed
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
To be clear about scope: `jsonrepair` also fixes things this library
|
|
87
|
+
intentionally doesn't (single quotes, trailing commas, unquoted keys) — it
|
|
88
|
+
isn't a strict RFC 8259 parser and isn't trying to be, so this library
|
|
89
|
+
can't simply replace it there. The gap is narrower and specific: nothing
|
|
90
|
+
in that repair path currently distinguishes *"the model's JSON syntax is
|
|
91
|
+
wrong"* from *"the model's JSON was cut off mid-value and the rest is
|
|
92
|
+
unknown"* — `jsonrepair` handles both the same way, silently. This library
|
|
93
|
+
draws exactly that distinction (`outcome: "truncated"`) and could gate
|
|
94
|
+
execution on it without touching the syntax-repair behavior for the other
|
|
95
|
+
case.
|
|
96
|
+
|
|
97
|
+
## Current Status (Alpha)
|
|
98
|
+
|
|
99
|
+
### Implemented
|
|
100
|
+
|
|
101
|
+
- Incremental UTF-8 decoder with split-byte handling
|
|
102
|
+
- Lexical JSON state machine (17 states)
|
|
103
|
+
- Grammar stack with object/array tracking
|
|
104
|
+
- Semantic event emission for committed scalar values
|
|
105
|
+
- Duplicate key detection and rejection
|
|
106
|
+
- `push()` / `snapshot()` / `drainEvents()` / `finish()` API
|
|
107
|
+
- Configurable resource limits (depth, input size, string length, queued events)
|
|
108
|
+
- Machine-readable test corpus with 25+ canonical fixtures
|
|
109
|
+
- Chunk invariance verification
|
|
110
|
+
- Provider stream adapters for OpenAI (legacy `function_call` and Responses API), Anthropic, Gemini, OpenRouter, and generic OpenAI-compatible endpoints, plus a coordinator for tracking multiple concurrent tool-call streams
|
|
111
|
+
|
|
112
|
+
### Not Yet Implemented
|
|
113
|
+
|
|
114
|
+
- Advanced repair engine (structural/lossy repairs)
|
|
115
|
+
- JSON Schema validation
|
|
116
|
+
- CLI tool
|
|
117
|
+
- Network/SSE client integration
|
|
118
|
+
|
|
119
|
+
## Installation
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
npm install prefix-safe-json
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Quick Start
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
import { createParser } from "prefix-safe-json";
|
|
129
|
+
|
|
130
|
+
const parser = createParser();
|
|
131
|
+
|
|
132
|
+
// Feed chunks as they arrive from the LLM stream
|
|
133
|
+
parser.push('{"tool":"calc",');
|
|
134
|
+
parser.push('"args":{"x":42}}');
|
|
135
|
+
|
|
136
|
+
// Get committed values at any point
|
|
137
|
+
const snap = parser.snapshot();
|
|
138
|
+
console.log(snap.stableValue);
|
|
139
|
+
|
|
140
|
+
// Drain semantic events
|
|
141
|
+
const events = parser.drainEvents();
|
|
142
|
+
|
|
143
|
+
// Signal stream completion
|
|
144
|
+
const result = parser.finish({ reason: "complete" });
|
|
145
|
+
console.log(result.executable); // true if safe to execute
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### End-to-end example (real API, no mocked internals)
|
|
149
|
+
|
|
150
|
+
The snippet above is illustrative; for a runnable, verified demonstration
|
|
151
|
+
against the actual provider-adapter + coordinator public API — including
|
|
152
|
+
the exact truncation scenario described above — see
|
|
153
|
+
[`examples/anthropic-truncation-safety.mjs`](examples/anthropic-truncation-safety.mjs)
|
|
154
|
+
(`node examples/anthropic-truncation-safety.mjs` after `pnpm run build`).
|
|
155
|
+
It feeds `AnthropicStreamAdapter` a realistic Anthropic Messages API SSE
|
|
156
|
+
sequence — including one cut off mid-argument by `max_tokens`, matching
|
|
157
|
+
Anthropic's real streaming shape — through `createToolCallStreamCoordinator()`,
|
|
158
|
+
and asserts the truncated call is never reported executable while a
|
|
159
|
+
genuinely complete one is. This example runs in CI on every push, so it
|
|
160
|
+
can't silently rot into a stale claim.
|
|
161
|
+
|
|
162
|
+
## License
|
|
163
|
+
|
|
164
|
+
Licensed under either of:
|
|
165
|
+
|
|
166
|
+
- MIT License ([LICENSE-MIT](LICENSE-MIT))
|
|
167
|
+
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE))
|
|
168
|
+
|
|
169
|
+
at your option.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ParserOptions } from "../types.js";
|
|
2
|
+
import { NormalizedToolStreamEvent, StreamEndReason } from "./protocol.js";
|
|
3
|
+
import { ToolCallStreamCoordinator, ToolCallCoordinatorSnapshot, ToolCallCoordinatorEvent, ToolCallCoordinatorFinalResult, CoordinatorPushResult, CoordinatorLimits } from "./types.js";
|
|
4
|
+
export declare class DefaultToolCallStreamCoordinator implements ToolCallStreamCoordinator {
|
|
5
|
+
private readonly limits;
|
|
6
|
+
private readonly parserOptions;
|
|
7
|
+
private calls;
|
|
8
|
+
private sourceKeyToInternalId;
|
|
9
|
+
private callCounter;
|
|
10
|
+
private eventQueue;
|
|
11
|
+
private diagnostics;
|
|
12
|
+
private eventsProcessed;
|
|
13
|
+
private globalSequence;
|
|
14
|
+
private isFinished;
|
|
15
|
+
/**
|
|
16
|
+
* @param limits Coordinator-level limits (concurrent tool calls, tool name
|
|
17
|
+
* size, etc.) — distinct from `parserOptions`, which configures each
|
|
18
|
+
* underlying per-call IncrementalJsonParser (limits, repairs). Previously
|
|
19
|
+
* there was no way to pass the latter through at all; every parser the
|
|
20
|
+
* coordinator created was hardcoded to library defaults.
|
|
21
|
+
*/
|
|
22
|
+
constructor(limits?: Partial<CoordinatorLimits>, parserOptions?: ParserOptions);
|
|
23
|
+
push(event: NormalizedToolStreamEvent): CoordinatorPushResult;
|
|
24
|
+
private handleStart;
|
|
25
|
+
private handleIdentity;
|
|
26
|
+
private handleNameDelta;
|
|
27
|
+
private handleArgumentsDelta;
|
|
28
|
+
private handleCallEnd;
|
|
29
|
+
private finishCall;
|
|
30
|
+
private hasCallConflict;
|
|
31
|
+
private handleProviderDiagnostic;
|
|
32
|
+
private handleStreamEnd;
|
|
33
|
+
private getCall;
|
|
34
|
+
private addDiagnostic;
|
|
35
|
+
snapshot(): ToolCallCoordinatorSnapshot;
|
|
36
|
+
drainEvents(): readonly ToolCallCoordinatorEvent[];
|
|
37
|
+
finish(meta?: {
|
|
38
|
+
reason?: StreamEndReason;
|
|
39
|
+
providerReason?: string;
|
|
40
|
+
}): ToolCallCoordinatorFinalResult;
|
|
41
|
+
}
|
|
42
|
+
export declare function createToolCallStreamCoordinator(limits?: Partial<CoordinatorLimits>, parserOptions?: ParserOptions): ToolCallStreamCoordinator;
|
|
43
|
+
//# sourceMappingURL=coordinator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coordinator.d.ts","sourceRoot":"","sources":["../../src/coordinator/coordinator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,aAAa,EAAE,MAAM,aAAa,CAAC;AAEnE,OAAO,EAAE,yBAAyB,EAAgB,eAAe,EAAE,MAAM,eAAe,CAAC;AACzF,OAAO,EACL,yBAAyB,EACzB,2BAA2B,EAC3B,wBAAwB,EACxB,8BAA8B,EAC9B,qBAAqB,EACrB,iBAAiB,EAIlB,MAAM,YAAY,CAAC;AAwBpB,qBAAa,gCAAiC,YAAW,yBAAyB;IAChF,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAoB;IAC3C,OAAO,CAAC,QAAQ,CAAC,aAAa,CAA4B;IAC1D,OAAO,CAAC,KAAK,CAAgD;IAC7D,OAAO,CAAC,qBAAqB,CAAkC;IAC/D,OAAO,CAAC,WAAW,CAAK;IACxB,OAAO,CAAC,UAAU,CAAkC;IACpD,OAAO,CAAC,WAAW,CAA+B;IAClD,OAAO,CAAC,eAAe,CAAK;IAC5B,OAAO,CAAC,cAAc,CAAK;IAC3B,OAAO,CAAC,UAAU,CAAS;IAE3B;;;;;;OAMG;gBACS,MAAM,CAAC,EAAE,OAAO,CAAC,iBAAiB,CAAC,EAAE,aAAa,CAAC,EAAE,aAAa;IAK9E,IAAI,CAAC,KAAK,EAAE,yBAAyB,GAAG,qBAAqB;IAgD7D,OAAO,CAAC,WAAW;IAqDnB,OAAO,CAAC,cAAc;IAgDtB,OAAO,CAAC,eAAe;IAqCvB,OAAO,CAAC,oBAAoB;IA8B5B,OAAO,CAAC,aAAa;IA6BrB,OAAO,CAAC,UAAU;IA8ClB,OAAO,CAAC,eAAe;IAIvB,OAAO,CAAC,wBAAwB;IAUhC,OAAO,CAAC,eAAe;IAyBvB,OAAO,CAAC,OAAO;IAcf,OAAO,CAAC,aAAa;IASrB,QAAQ,IAAI,2BAA2B;IAkBvC,WAAW,IAAI,SAAS,wBAAwB,EAAE;IAMlD,MAAM,CAAC,IAAI,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,eAAe,CAAC;QAAC,cAAc,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,8BAA8B;CAcrG;AAED,wBAAgB,+BAA+B,CAC7C,MAAM,CAAC,EAAE,OAAO,CAAC,iBAAiB,CAAC,EACnC,aAAa,CAAC,EAAE,aAAa,GAC5B,yBAAyB,CAE3B"}
|