vectorjson 0.3.2 → 0.4.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/README.md CHANGED
@@ -242,22 +242,6 @@ const result = parser.getValue(); // lazy Proxy — materializes on access
242
242
  parser.destroy();
243
243
  ```
244
244
 
245
- ### Vercel AI SDK-compatible signature
246
-
247
- If you have code that calls `parsePartialJson`, VectorJSON provides a compatible function:
248
-
249
- ```js
250
- // Before
251
- import { parsePartialJson } from "ai";
252
- const { value, state } = parsePartialJson(buffer);
253
-
254
- // After
255
- import { parsePartialJson } from "vectorjson";
256
- const { value, state } = parsePartialJson(buffer);
257
- ```
258
-
259
- > **Note:** AI SDKs (Vercel, Anthropic, TanStack) parse JSON internally inside `streamObject()`, `MessageStream`, etc. — you don't get access to the raw chunks. To use VectorJSON today, work with the raw LLM stream directly (raw fetch, WebSocket, SSE).
260
-
261
245
  ### Event-driven: React to fields as they stream in
262
246
 
263
247
  When an LLM streams a tool call, you usually care about specific fields at specific times. `createEventParser` lets you subscribe to paths and get notified the moment a value completes or a string grows:
@@ -350,7 +334,7 @@ for await (const partial of createParser({ source: chunks() })) {
350
334
 
351
335
  Validate and auto-infer types with Zod, Valibot, ArkType, or any lib with `.safeParse()`. Works on all three APIs:
352
336
 
353
- **Streaming parser with typed partial objects** — like Vercel AI SDK's `output`, but O(n) instead of O(n²):
337
+ **Streaming parser with typed partial objects:**
354
338
 
355
339
  ```ts
356
340
  import { z } from 'zod';
@@ -454,6 +438,24 @@ result.status; // "complete" | "complete_early" | "incomplete" | "invalid"
454
438
  result.value.users; // lazy Proxy — materializes on access
455
439
  ```
456
440
 
441
+ ### JSONL & JSON5
442
+
443
+ Both `createParser` and `createEventParser` accept `format: "jsonl" | "json5"`:
444
+
445
+ ```js
446
+ // JSONL — yields each value separately
447
+ for await (const value of createParser({ format: "jsonl", source: stream })) {
448
+ console.log(value); // { user: "Alice" }, { user: "Bob" }, ...
449
+ }
450
+
451
+ // JSON5 — comments, trailing commas, unquoted keys, single-quoted strings, hex, Infinity/NaN
452
+ const p = createParser({ format: "json5" });
453
+ p.feed(`{ name: 'Alice', tags: ['admin',], color: 0xFF0000, timeout: Infinity, }`);
454
+ p.getValue(); // { name: "Alice", tags: ["admin"], color: 16711680, timeout: Infinity }
455
+ ```
456
+
457
+ JSONL push-based: call `resetForNext()` after each value. JSON5 comments are stripped at the byte level during streaming.
458
+
457
459
  ## API Reference
458
460
 
459
461
  ### Direct exports (recommended)
@@ -503,6 +505,7 @@ createParser({ schema, source }); // options object
503
505
  interface CreateParserOptions<T = unknown> {
504
506
  schema?: ZodLike<T>; // only parse schema fields, validate on complete
505
507
  source?: ReadableStream<Uint8Array> | AsyncIterable<Uint8Array | string>;
508
+ format?: "json" | "jsonl" | "json5"; // default: "json"
506
509
  }
507
510
  ```
508
511
 
@@ -526,6 +529,7 @@ interface StreamingParser<T = unknown> {
526
529
  getRemaining(): Uint8Array | null;
527
530
  getRawBuffer(): ArrayBuffer | null; // transferable buffer for Worker postMessage
528
531
  getStatus(): FeedStatus;
532
+ resetForNext(): number; // JSONL: reset for next value, returns remaining byte count
529
533
  destroy(): void;
530
534
  [Symbol.asyncIterator](): AsyncIterableIterator<T | undefined>; // requires source
531
535
  }
@@ -549,7 +553,7 @@ Works with Zod, Valibot, ArkType — any library with `{ safeParse(v) → { succ
549
553
 
550
554
  ### `parsePartialJson(input, schema?): PartialJsonResult<DeepPartial<T>>`
551
555
 
552
- Compatible with Vercel AI SDK's `parsePartialJson` signature. Returns a plain JS object (not a Proxy). Pass an optional schema for type-safe validation.
556
+ One-shot partial JSON parse. Returns a plain JS object (not a Proxy). Pass an optional schema for type-safe validation.
553
557
 
554
558
  With a schema, returns `DeepPartial<T>` — all properties are optional because incomplete JSON will have missing fields. When `safeParse` succeeds, returns validated `data`. When `safeParse` fails on a repaired-parse (partial JSON), the raw parsed value is kept — the object is partial, that's expected.
555
559
 
@@ -581,7 +585,7 @@ createEventParser({ schema, source }); // schema + for-await
581
585
  {
582
586
  source?: ReadableStream<Uint8Array> | AsyncIterable<Uint8Array | string>;
583
587
  schema?: ZodLike<T>; // only parse schema fields (same as createParser)
584
- // format?: "json" | "jsonl"; // planned — JSONL support for both parsers
588
+ format?: "json" | "jsonl" | "json5"; // default: "json"
585
589
  }
586
590
  ```
587
591
 
@@ -654,7 +658,7 @@ interface DeltaEvent {
654
658
  | **Schema** | Pass Zod/Valibot, only schema fields are parsed | Same |
655
659
  | **Skip non-JSON** (think tags, code fences, prose) | — | Always |
656
660
  | **Field subscriptions** | — | `on()`, `onDelta()` |
657
- | **JSONL** | `format: "jsonl"` (planned) | `format: "jsonl"` (planned) |
661
+ | **JSONL** | `format: "jsonl"` | `format: "jsonl"` |
658
662
  | **Text callbacks** | — | `onText()` |
659
663
 
660
664
  **`createParser` parses one JSON value** and reports status — you check it and react:
@@ -744,7 +748,7 @@ ANTHROPIC_API_KEY=sk-ant-... bun examples/anthropic-tool-call.ts
744
748
  OPENAI_API_KEY=sk-... bun examples/openai-function-call.ts
745
749
  ```
746
750
 
747
- See also `examples/ai-usage.ts` for additional patterns (MCP stdio, Vercel AI SDK `streamObject`, NDJSON embeddings).
751
+ See also `examples/ai-usage.ts` for additional patterns (MCP stdio, NDJSON embeddings).
748
752
 
749
753
  ## Building from Source
750
754