llmbic 1.2.0 → 1.3.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/CHANGELOG.md +23 -7
- package/README.md +57 -28
- package/dist/extractor.d.ts +3 -1
- package/dist/extractor.d.ts.map +1 -1
- package/dist/extractor.js +6 -4
- package/dist/extractor.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/merge.d.ts +2 -2
- package/dist/merge.js +3 -3
- package/dist/prompt.d.ts +2 -2
- package/dist/prompt.js +3 -3
- package/dist/rules.d.ts +18 -5
- package/dist/rules.d.ts.map +1 -1
- package/dist/rules.js +17 -4
- package/dist/rules.js.map +1 -1
- package/dist/types/extractor.types.d.ts +18 -11
- package/dist/types/extractor.types.d.ts.map +1 -1
- package/dist/types/logger.types.d.ts +1 -1
- package/dist/types/merge.types.d.ts +3 -3
- package/dist/types/provider.types.d.ts +1 -1
- package/dist/types/rule.types.d.ts +18 -3
- package/dist/types/rule.types.d.ts.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,22 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [1.3.0] - 2026-04-18
|
|
9
|
+
|
|
10
|
+
Non-breaking. Rules can now read a caller-provided `context` object alongside `content`, so per-call metadata (locale, tenant configuration, feature flags) no longer has to be captured in rule closures at declaration time.
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- `ExtractionRule<TContext = unknown>` - optional generic type parameter describing the shape of a per-call context forwarded to `extract`. Defaults to `unknown`, so context-unaware rules and legacy code compile unchanged.
|
|
15
|
+
- `ExtractionRule.extract(content, context?)` - second optional argument, forwarded verbatim by `rule.apply` / `Extractor.extract` / `Extractor.extractSync`. Left `undefined` when the caller passes no context.
|
|
16
|
+
- `rule.create<TContext>(field, extract, options?)` - `create` is now generic over `TContext`, so typed contexts flow from the callback signature to the returned `ExtractionRule<TContext>`. `rule.regex` stays context-unaware and remains assignable to any `ExtractionRule<TContext>[]` via contextual parameter contravariance.
|
|
17
|
+
- `rule.apply<S, TContext>(content, rules, schema, logger?, context?)` - fifth optional argument passed through to every rule's `extract` callback.
|
|
18
|
+
- `ExtractorConfig<S, TContext = unknown>` and `Extractor<T, TContext = unknown>` - second optional generic parameter shared with the rules array. `Extractor.extract(content, context?)` and `Extractor.extractSync(content, context?)` forward `context` to `rule.apply`. `Extractor.merge` does not re-evaluate rules and accepts no `context`.
|
|
19
|
+
|
|
20
|
+
### Docs
|
|
21
|
+
|
|
22
|
+
- `CONTRIBUTING.md` - documents the 5-step release procedure (tests, bump, doc, tag, publish) to keep future releases aligned with the SemVer + npm publish lifecycle.
|
|
23
|
+
|
|
8
24
|
## [1.2.0] - 2026-04-16
|
|
9
25
|
|
|
10
26
|
Non-breaking. Production-readiness pass: per-field provenance, per-field merge policy, and pre/post LLM transformers. Token / cost tracking deliberately stays out of scope - `LlmProvider` keeps observability as a caller concern; wrap your `complete` for telemetry.
|
|
@@ -46,18 +62,18 @@ Non-breaking. Unblocks hybrid workflows that rely on nested schemas, agreement/c
|
|
|
46
62
|
- `ExtractorConfig<S>` gains optional `normalizers`, `validators`, `policy`, `logger`.
|
|
47
63
|
- `ExtractorLlmConfig` gains optional `mode`, `crossCheckHints`.
|
|
48
64
|
|
|
49
|
-
## [1.0.0]
|
|
65
|
+
## [1.0.0] - 2026-04-15
|
|
50
66
|
|
|
51
67
|
Initial public release.
|
|
52
68
|
|
|
53
69
|
### Added
|
|
54
70
|
|
|
55
|
-
- `createExtractor(config)`
|
|
56
|
-
- `rule` namespace
|
|
57
|
-
- `merge` namespace
|
|
58
|
-
- `prompt` namespace
|
|
59
|
-
- `validator` namespace
|
|
60
|
-
- `LlmProvider` contract
|
|
71
|
+
- `createExtractor(config)` - factory binding a Zod schema, deterministic rules and an optional LLM fallback into an extractor with `extract`, `extractSync`, `prompt`, `parse` and `merge` methods. Covers both one-shot async extraction and 4-step batch flows (extractSync -> prompt -> external LLM call -> parse -> merge).
|
|
72
|
+
- `rule` namespace - `rule.create(field, extractFn)`, `rule.regex(field, pattern, score, transform?)`, `rule.confidence(value, score)`, `rule.apply(content, rules, schema, logger?)`. Deterministic rules are pure synchronous functions returning typed matches with a confidence score in `[0, 1]`.
|
|
73
|
+
- `merge` namespace - `merge.apply(schema, rulesResult, llmResult, content, options?)` fuses rules output with LLM output, detects per-field conflicts, runs normalizers, re-validates against the Zod schema, and runs custom validators. `merge.defaultFieldPolicy` exposes the built-in fusion rules.
|
|
74
|
+
- `prompt` namespace - `prompt.build(schema, partial, options?)` emits an `LlmRequest` (`systemPrompt`, `userContent`, `responseSchema`, `knownValues`) restricted to fields missing from the deterministic pass. `prompt.parse(raw, missing, schema)` is a permissive parser that validates each field individually via Zod, drops invalid or unexpected keys, and never throws.
|
|
75
|
+
- `validator` namespace - `validator.of<T>()` returns `{ field, crossField }` factories bound to the data shape `T`, so predicates are fully typed from the field name.
|
|
76
|
+
- `LlmProvider` contract - single-method interface (`complete(request) -> { values }`) consumers implement to wire any backend (OpenAI, Anthropic, Ollama, custom HTTP, ...). No vendor SDK is pulled into the import graph.
|
|
61
77
|
- Per-field confidence scoring, conflict detection (`flag` / `prefer-rule` / `prefer-llm` strategies), and extraction metadata (`durationMs`, rule/LLM field counts).
|
|
62
78
|
- Full TypeScript `.d.ts` output with JSDoc on every public type, method and configuration field.
|
|
63
79
|
- Example wiring a local Ollama runtime under `examples/ollama.ts`.
|
package/README.md
CHANGED
|
@@ -5,13 +5,13 @@
|
|
|
5
5
|
[](./LICENSE)
|
|
6
6
|
[](https://nodejs.org)
|
|
7
7
|
|
|
8
|
-
Hybrid data extraction
|
|
8
|
+
Hybrid data extraction - deterministic rules + LLM fallback, with per-field confidence scoring.
|
|
9
9
|
|
|
10
10
|
The name folds **LLM** into [*lambic*](https://en.wikipedia.org/wiki/Lambic), the Belgian beer made by blending wild fermentation with a controlled process. Same idea here: LLMs are unpredictable, rules are rigid, and the mix produces something reliable.
|
|
11
11
|
|
|
12
12
|
## Why
|
|
13
13
|
|
|
14
|
-
Extracting structured data from unstructured text is a solved problem
|
|
14
|
+
Extracting structured data from unstructured text is a solved problem - until you need it to be *reliable*. Rules (regex, parsers) are deterministic but brittle. LLMs understand context but hallucinate. Neither is enough alone.
|
|
15
15
|
|
|
16
16
|
Llmbic combines both: deterministic rules extract what they can with full confidence, the LLM fills in the gaps, and a merge layer detects conflicts between the two. Every field carries a confidence score. You know exactly what's trustworthy and what needs review.
|
|
17
17
|
|
|
@@ -21,7 +21,7 @@ Llmbic combines both: deterministic rules extract what they can with full confid
|
|
|
21
21
|
npm install llmbic
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
-
Llmbic has a single dependency: [Zod](https://zod.dev). No vendor SDK is pulled in
|
|
24
|
+
Llmbic has a single dependency: [Zod](https://zod.dev). No vendor SDK is pulled in - you bring your own LLM provider via the 1-method `LlmProvider` interface (see "Writing a provider" below).
|
|
25
25
|
|
|
26
26
|
## Quick start
|
|
27
27
|
|
|
@@ -111,7 +111,7 @@ console.log(result.confidence);
|
|
|
111
111
|
// { total: 1.0, currency: 1.0, vendor: 0.7, date: 0.7 }
|
|
112
112
|
|
|
113
113
|
console.log(result.conflicts);
|
|
114
|
-
// []
|
|
114
|
+
// [] - no disagreement between rules and LLM
|
|
115
115
|
```
|
|
116
116
|
|
|
117
117
|
### Batch / async mode (for OpenAI Batch API, job queues, etc.)
|
|
@@ -119,19 +119,19 @@ console.log(result.conflicts);
|
|
|
119
119
|
When you manage the LLM call yourself (batching, polling, custom transport), use the 4-step API:
|
|
120
120
|
|
|
121
121
|
```typescript
|
|
122
|
-
// Step 1
|
|
122
|
+
// Step 1 - Deterministic extraction (sync, instant)
|
|
123
123
|
const partial = extractor.extractSync(markdown);
|
|
124
124
|
|
|
125
|
-
// Step 2
|
|
125
|
+
// Step 2 - Build the LLM request (you send it however you want)
|
|
126
126
|
const llmRequest = extractor.prompt(markdown, partial);
|
|
127
|
-
//
|
|
127
|
+
// -> { systemPrompt, userContent, responseSchema, knownValues }
|
|
128
128
|
|
|
129
129
|
// ... submit to OpenAI Batch API, poll later, get the response ...
|
|
130
130
|
|
|
131
|
-
// Step 3
|
|
131
|
+
// Step 3 - Parse the raw LLM response
|
|
132
132
|
const llmResult = extractor.parse(rawJsonResponse);
|
|
133
133
|
|
|
134
|
-
// Step 4
|
|
134
|
+
// Step 4 - Merge everything (fusion + conflict detection + validation)
|
|
135
135
|
const result = extractor.merge(partial, llmResult, markdown);
|
|
136
136
|
```
|
|
137
137
|
|
|
@@ -181,12 +181,12 @@ End-to-end runnable example (upload + poll + download + merge): [`examples/opena
|
|
|
181
181
|
|
|
182
182
|
### Per-field confidence scoring
|
|
183
183
|
|
|
184
|
-
Every field in the result carries a confidence score (0.0
|
|
184
|
+
Every field in the result carries a confidence score (0.0-1.0):
|
|
185
185
|
|
|
186
186
|
| Source | Confidence |
|
|
187
187
|
|--------|-----------|
|
|
188
188
|
| Deterministic rule, exact match | 1.0 |
|
|
189
|
-
| Deterministic rule, partial match | 0.7
|
|
189
|
+
| Deterministic rule, partial match | 0.7-0.9 (you decide) |
|
|
190
190
|
| LLM only | configurable default (0.7) |
|
|
191
191
|
| Rule + LLM agree | 1.0 |
|
|
192
192
|
| Rule + LLM disagree | 0.3 (flagged as conflict) |
|
|
@@ -223,7 +223,7 @@ result.conflicts;
|
|
|
223
223
|
// [{ field: 'total', ruleValue: 1250, ruleConfidence: 1.0, llmValue: 1520 }]
|
|
224
224
|
```
|
|
225
225
|
|
|
226
|
-
Three conflict strategies: `'flag'` (default
|
|
226
|
+
Three conflict strategies: `'flag'` (default - keep rule value, record conflict), `'prefer-rule'`, or `'prefer-llm'`.
|
|
227
227
|
|
|
228
228
|
In the default `'fill-gaps'` mode the LLM is only asked about fields the rules could not resolve, so conflicts are impossible. To actually trigger conflict detection, opt into cross-check (see below).
|
|
229
229
|
|
|
@@ -263,6 +263,35 @@ const extractor = createExtractor({
|
|
|
263
263
|
|
|
264
264
|
The merge step now sees two candidates per field and surfaces real disagreements through `result.conflicts`. `crossCheckHints: 'bias'` re-exposes the rule values as hints to save tokens, at the cost of confirmation bias (the LLM tends to agree with what it was shown).
|
|
265
265
|
|
|
266
|
+
### Per-call context
|
|
267
|
+
|
|
268
|
+
Rules can accept a second, caller-provided argument alongside `content` so they can read per-call metadata (locale, tenant configuration, feature flags) without having to capture it in a closure at declaration time:
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
import { createExtractor, rule } from 'llmbic';
|
|
272
|
+
import { z } from 'zod';
|
|
273
|
+
|
|
274
|
+
type Region = { region: 'us' | 'uk' };
|
|
275
|
+
|
|
276
|
+
const schema = z.object({ price: z.string() });
|
|
277
|
+
|
|
278
|
+
const priceRule = rule.create<Region>('price', (content, context) => {
|
|
279
|
+
const pattern = context?.region === 'uk' ? /£\s*(\d+)/ : /\$\s*(\d+)/;
|
|
280
|
+
const match = content.match(pattern);
|
|
281
|
+
return match ? rule.confidence(match[1]!, 1) : null;
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
const extractor = createExtractor<typeof schema, Region>({
|
|
285
|
+
schema,
|
|
286
|
+
rules: [priceRule],
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
await extractor.extract('Listed at £42', { region: 'uk' });
|
|
290
|
+
// -> { price: '42' }
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
The type parameter on `rule.create<TContext>` flows to the generic on `createExtractor<Schema, TContext>` and is enforced at every call site. `context` is forwarded verbatim to every rule's `extract` callback; rules that ignore the argument still compile and work. `extractSync(content, context?)` behaves the same way for batch workflows. `Extractor.merge` reuses the partial's values and does not re-evaluate rules, so it takes no `context`.
|
|
294
|
+
|
|
266
295
|
### Rich schemas
|
|
267
296
|
|
|
268
297
|
The JSON Schema handed to the LLM supports the Zod constructs that show up in real-world extraction targets:
|
|
@@ -339,7 +368,7 @@ Common patterns:
|
|
|
339
368
|
|
|
340
369
|
## Writing a provider
|
|
341
370
|
|
|
342
|
-
Llmbic does not ship vendor-specific adapters. The `LlmProvider` contract is a single method
|
|
371
|
+
Llmbic does not ship vendor-specific adapters. The `LlmProvider` contract is a single method - wiring to any backend (OpenAI, Anthropic, Ollama, vLLM, Gemini, custom HTTP, ...) is ~10 lines you write and own.
|
|
343
372
|
|
|
344
373
|
```typescript
|
|
345
374
|
import type { LlmProvider } from 'llmbic';
|
|
@@ -404,7 +433,7 @@ const provider: LlmProvider = {
|
|
|
404
433
|
};
|
|
405
434
|
```
|
|
406
435
|
|
|
407
|
-
**Ollama** (native `format`
|
|
436
|
+
**Ollama** (native `format` - JSON Schema, requires Ollama 0.5+):
|
|
408
437
|
|
|
409
438
|
```typescript
|
|
410
439
|
const client = new Ollama();
|
|
@@ -423,27 +452,27 @@ const provider: LlmProvider = {
|
|
|
423
452
|
};
|
|
424
453
|
```
|
|
425
454
|
|
|
426
|
-
Observability (token usage, latency, cost accounting) is out of scope
|
|
455
|
+
Observability (token usage, latency, cost accounting) is out of scope - wrap the `complete` call in whatever telemetry you already use.
|
|
427
456
|
|
|
428
457
|
## Design decisions
|
|
429
458
|
|
|
430
|
-
- **One dependency**
|
|
431
|
-
- **No retry**
|
|
432
|
-
- **No streaming**
|
|
433
|
-
- **No chunking**
|
|
434
|
-
- **Normalizers mutate**
|
|
435
|
-
- **Rules are sync**
|
|
459
|
+
- **One dependency** - Zod only. No vendor SDK ever enters the import graph; you bring your own LLM provider (see "Writing a provider").
|
|
460
|
+
- **No retry** - If the LLM returns invalid data, `parse()` does best-effort parsing (valid fields kept, invalid ignored). Retry is an orchestration concern.
|
|
461
|
+
- **No streaming** - Llmbic works with complete results. Streaming is a transport concern.
|
|
462
|
+
- **No chunking** - One content = one extraction. If your content is too long, split it before calling Llmbic.
|
|
463
|
+
- **Normalizers mutate** - For pragmatic reasons, normalizers receive and return the same object. The `merge()` function copies the data first, so the original is never modified.
|
|
464
|
+
- **Rules are sync** - Extraction rules are pure synchronous functions. If you need async lookups, do them before creating the rule.
|
|
436
465
|
|
|
437
466
|
## API reference
|
|
438
467
|
|
|
439
468
|
### `createExtractor(config)`
|
|
440
469
|
|
|
441
|
-
Creates an extractor instance. Config:
|
|
470
|
+
Creates an extractor instance. Signature: `createExtractor<S, TContext = unknown>(config)`. `TContext` is optional and describes the per-call context passed to `Extractor.extract(content, context?)`; see [Per-call context](#per-call-context). Config:
|
|
442
471
|
|
|
443
472
|
| Field | Type | Required | Description |
|
|
444
473
|
|-------|------|----------|-------------|
|
|
445
474
|
| `schema` | `ZodObject` | yes | Output schema (drives field enumeration and re-validation). |
|
|
446
|
-
| `rules` | `ExtractionRule[]` | yes | Deterministic extraction rules. |
|
|
475
|
+
| `rules` | `ExtractionRule<TContext>[]` | yes | Deterministic extraction rules. |
|
|
447
476
|
| `llm` | `ExtractorLlmConfig` | no | LLM fallback. Omit for rules-only mode. See below. |
|
|
448
477
|
| `normalizers` | `Normalizer<T>[]` | no | Post-merge transformations, run in declared order. |
|
|
449
478
|
| `validators` | `Validator<ExtractedData<T>>[]` | no | Invariants populating `result.validation`. |
|
|
@@ -466,10 +495,10 @@ Creates an extractor instance. Config:
|
|
|
466
495
|
|
|
467
496
|
| Member | Signature | Description |
|
|
468
497
|
|---|---|---|
|
|
469
|
-
| `rule.create` |
|
|
498
|
+
| `rule.create` | `<TContext = unknown>(field, extract, options?) => ExtractionRule<TContext>` | Declare a rule. `extract(content, context?)` returns a `RuleMatch` or `null`. `options.id` sets the stable identifier surfaced in `result.sources`. `TContext` is inferred from the callback when present. |
|
|
470
499
|
| `rule.regex` | `(field, pattern, score, transform?, options?) => ExtractionRule` | Regex-based rule. On match, capture group 1 (or the full match) is fed to `transform`. `options.id` sets the stable identifier surfaced in `result.sources`. |
|
|
471
500
|
| `rule.confidence` | `(value, score) => RuleMatch` | Wrap a value and a confidence score; sugar for custom `extract` callbacks. |
|
|
472
|
-
| `rule.apply` |
|
|
501
|
+
| `rule.apply` | `<S, TContext = unknown>(content, rules, schema, logger?, context?) => RulesResult` | Run every rule, pick the highest-confidence match per field, type-check against the schema. `context` is forwarded verbatim to each rule's `extract` callback. |
|
|
473
502
|
|
|
474
503
|
### `validator.of<T>()`
|
|
475
504
|
|
|
@@ -484,11 +513,11 @@ Binding `T` once lets TypeScript infer each field's type from the field name, so
|
|
|
484
513
|
|
|
485
514
|
| Method | Sync | Description |
|
|
486
515
|
|--------|------|-------------|
|
|
487
|
-
| `extract(content)` | async | Full pipeline: rules -> LLM (if configured) -> merge -> normalize -> validate. |
|
|
488
|
-
| `extractSync(content)` | sync | Rules only. Returns the partial result + `missing` fields. |
|
|
516
|
+
| `extract(content, context?)` | async | Full pipeline: rules -> LLM (if configured) -> merge -> normalize -> validate. `context` is forwarded verbatim to every rule's `extract` callback. |
|
|
517
|
+
| `extractSync(content, context?)` | sync | Rules only. Returns the partial result + `missing` fields. `context` is forwarded verbatim to every rule's `extract` callback. |
|
|
489
518
|
| `prompt(content, partial)` | sync | Builds the LLM request. Covers `partial.missing` in fill-gaps mode, every schema field in cross-check mode. |
|
|
490
519
|
| `parse(raw)` | sync | Parses a raw LLM JSON response, validating each field individually. Never throws. |
|
|
491
|
-
| `merge(partial, llmResult, content)` | sync | Merges rules + LLM, detects conflicts, normalizes, validates. |
|
|
520
|
+
| `merge(partial, llmResult, content)` | sync | Merges rules + LLM, detects conflicts, normalizes, validates. Does not re-evaluate rules, so takes no `context`. |
|
|
492
521
|
|
|
493
522
|
## License
|
|
494
523
|
|
package/dist/extractor.d.ts
CHANGED
|
@@ -15,11 +15,13 @@ import type { Extractor, ExtractorConfig } from './types/extractor.types.js';
|
|
|
15
15
|
* is parsed with {@link prompt.parse} and fused through {@link merge.apply}.
|
|
16
16
|
*
|
|
17
17
|
* @typeParam S - A Zod object schema describing the target data shape.
|
|
18
|
+
* @typeParam TContext - Shape of the optional per-call context forwarded to
|
|
19
|
+
* every rule's `extract` callback. Defaults to `unknown`.
|
|
18
20
|
* @param config - Schema, deterministic rules, and optional LLM fallback,
|
|
19
21
|
* plus `policy`, `normalizers`, `validators` and `logger` forwarded to
|
|
20
22
|
* every internal {@link merge.apply} call. The logger is also forwarded
|
|
21
23
|
* to {@link rule.apply} so schema-rejection warnings stay visible.
|
|
22
24
|
* @returns An {@link Extractor} bound to `config.schema`.
|
|
23
25
|
*/
|
|
24
|
-
export declare function createExtractor<S extends z.ZodObject<z.ZodRawShape
|
|
26
|
+
export declare function createExtractor<S extends z.ZodObject<z.ZodRawShape>, TContext = unknown>(config: ExtractorConfig<S, TContext>): Extractor<z.infer<S>, TContext>;
|
|
25
27
|
//# sourceMappingURL=extractor.d.ts.map
|
package/dist/extractor.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extractor.d.ts","sourceRoot":"","sources":["../src/extractor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAI7B,OAAO,KAAK,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAmD7E
|
|
1
|
+
{"version":3,"file":"extractor.d.ts","sourceRoot":"","sources":["../src/extractor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAI7B,OAAO,KAAK,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAmD7E;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,eAAe,CAC7B,CAAC,SAAS,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,EACpC,QAAQ,GAAG,OAAO,EAElB,MAAM,EAAE,eAAe,CAAC,CAAC,EAAE,QAAQ,CAAC,GACnC,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,CA4EjC"}
|
package/dist/extractor.js
CHANGED
|
@@ -55,6 +55,8 @@ function stampDuration(result, startedAt) {
|
|
|
55
55
|
* is parsed with {@link prompt.parse} and fused through {@link merge.apply}.
|
|
56
56
|
*
|
|
57
57
|
* @typeParam S - A Zod object schema describing the target data shape.
|
|
58
|
+
* @typeParam TContext - Shape of the optional per-call context forwarded to
|
|
59
|
+
* every rule's `extract` callback. Defaults to `unknown`.
|
|
58
60
|
* @param config - Schema, deterministic rules, and optional LLM fallback,
|
|
59
61
|
* plus `policy`, `normalizers`, `validators` and `logger` forwarded to
|
|
60
62
|
* every internal {@link merge.apply} call. The logger is also forwarded
|
|
@@ -79,9 +81,9 @@ export function createExtractor(config) {
|
|
|
79
81
|
logger: config.logger,
|
|
80
82
|
};
|
|
81
83
|
return {
|
|
82
|
-
async extract(content) {
|
|
84
|
+
async extract(content, context) {
|
|
83
85
|
const startedAt = performance.now();
|
|
84
|
-
const rulesResult = rule.apply(content, config.rules, config.schema, config.logger);
|
|
86
|
+
const rulesResult = rule.apply(content, config.rules, config.schema, config.logger, context);
|
|
85
87
|
const partial = merge.apply(config.schema, rulesResult, null, content, mergeOptions);
|
|
86
88
|
const shouldCallLlm = config.llm !== undefined &&
|
|
87
89
|
(buildOptions.mode === 'cross-check' || partial.missing.length > 0);
|
|
@@ -101,9 +103,9 @@ export function createExtractor(config) {
|
|
|
101
103
|
const final = merge.apply(config.schema, rulesResult, llmResult, content, mergeOptions);
|
|
102
104
|
return stampDuration(final, startedAt);
|
|
103
105
|
},
|
|
104
|
-
extractSync(content) {
|
|
106
|
+
extractSync(content, context) {
|
|
105
107
|
const startedAt = performance.now();
|
|
106
|
-
const rulesResult = rule.apply(content, config.rules, config.schema, config.logger);
|
|
108
|
+
const rulesResult = rule.apply(content, config.rules, config.schema, config.logger, context);
|
|
107
109
|
const partial = merge.apply(config.schema, rulesResult, null, content, mergeOptions);
|
|
108
110
|
return stampDuration(partial, startedAt);
|
|
109
111
|
},
|
package/dist/extract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,CAAC,CAAC;QAC1C,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/merge.d.ts
CHANGED
|
@@ -43,7 +43,7 @@ export declare const merge: {
|
|
|
43
43
|
* @param field - Name of the field being merged.
|
|
44
44
|
* @param ruleMatch - Value proposed by a deterministic rule, or `null` if none.
|
|
45
45
|
* @param llmValue - Value proposed by the LLM, or `null` if none. Cast to `T`
|
|
46
|
-
* without runtime type-check
|
|
46
|
+
* without runtime type-check - callers that expose `merge.field` via
|
|
47
47
|
* `merge.apply` rely on the final Zod re-validation to reject invalid LLM values.
|
|
48
48
|
* @param policy - Optional strategy and confidence overrides.
|
|
49
49
|
* @param logger - Optional logger notified of unexpected runtime situations
|
|
@@ -58,7 +58,7 @@ export declare const merge: {
|
|
|
58
58
|
* Passing `llmResult = null` runs in rules-only mode: every field keeps
|
|
59
59
|
* whatever the rules produced and `meta.llmCalled` is `false`.
|
|
60
60
|
*
|
|
61
|
-
* Orchestration only
|
|
61
|
+
* Orchestration only - the three phases (fusion, normalization, validation)
|
|
62
62
|
* each live in their own private helper above.
|
|
63
63
|
*
|
|
64
64
|
* Runtime fields of `meta` (`durationMs`, `tokensUsed`) are populated by
|
package/dist/merge.js
CHANGED
|
@@ -12,7 +12,7 @@ function fuseAllFields(schemaKeys, rulesResult, llmResult, policy, policyByField
|
|
|
12
12
|
let rulesMatched = 0;
|
|
13
13
|
for (const field of schemaKeys) {
|
|
14
14
|
const hasRuleValue = field in rulesResult.values;
|
|
15
|
-
// hasRuleValue implies confidence[field] is defined
|
|
15
|
+
// hasRuleValue implies confidence[field] is defined - rule.apply only writes
|
|
16
16
|
// to `confidence` when it also writes to `values`.
|
|
17
17
|
const ruleMatch = hasRuleValue
|
|
18
18
|
? {
|
|
@@ -167,7 +167,7 @@ export const merge = {
|
|
|
167
167
|
* @param field - Name of the field being merged.
|
|
168
168
|
* @param ruleMatch - Value proposed by a deterministic rule, or `null` if none.
|
|
169
169
|
* @param llmValue - Value proposed by the LLM, or `null` if none. Cast to `T`
|
|
170
|
-
* without runtime type-check
|
|
170
|
+
* without runtime type-check - callers that expose `merge.field` via
|
|
171
171
|
* `merge.apply` rely on the final Zod re-validation to reject invalid LLM values.
|
|
172
172
|
* @param policy - Optional strategy and confidence overrides.
|
|
173
173
|
* @param logger - Optional logger notified of unexpected runtime situations
|
|
@@ -239,7 +239,7 @@ export const merge = {
|
|
|
239
239
|
* Passing `llmResult = null` runs in rules-only mode: every field keeps
|
|
240
240
|
* whatever the rules produced and `meta.llmCalled` is `false`.
|
|
241
241
|
*
|
|
242
|
-
* Orchestration only
|
|
242
|
+
* Orchestration only - the three phases (fusion, normalization, validation)
|
|
243
243
|
* each live in their own private helper above.
|
|
244
244
|
*
|
|
245
245
|
* Runtime fields of `meta` (`durationMs`, `tokensUsed`) are populated by
|
package/dist/prompt.d.ts
CHANGED
|
@@ -39,7 +39,7 @@ export declare const prompt: {
|
|
|
39
39
|
/**
|
|
40
40
|
* Parse a raw LLM response permissively. Accepts either an already-decoded
|
|
41
41
|
* object or a JSON-encoded string. Each field listed in `missing` is
|
|
42
|
-
* validated individually against its Zod schema
|
|
42
|
+
* validated individually against its Zod schema - valid fields flow into
|
|
43
43
|
* `values`, invalid ones are dropped and surfaced as warnings. Keys outside
|
|
44
44
|
* `missing` are dropped as well, with a single aggregated warning so the
|
|
45
45
|
* caller can spot a prompt/provider mismatch.
|
|
@@ -50,7 +50,7 @@ export declare const prompt: {
|
|
|
50
50
|
* @param schema - Zod object schema whose fields back the validation.
|
|
51
51
|
* @param missing - Fields the LLM was expected to produce (typically
|
|
52
52
|
* {@link ExtractionResult.missing}).
|
|
53
|
-
* @param raw - The provider response
|
|
53
|
+
* @param raw - The provider response - object or JSON string.
|
|
54
54
|
*/
|
|
55
55
|
parse<S extends z.ZodObject<z.ZodRawShape>>(schema: S, missing: readonly (keyof z.infer<S>)[], raw: unknown): LlmResult;
|
|
56
56
|
};
|
package/dist/prompt.js
CHANGED
|
@@ -89,7 +89,7 @@ function buildResponseSchema(schema, missing) {
|
|
|
89
89
|
return { type: 'object', properties, required, additionalProperties: false };
|
|
90
90
|
}
|
|
91
91
|
/**
|
|
92
|
-
* Pick the non-null, non-missing entries of the partial result
|
|
92
|
+
* Pick the non-null, non-missing entries of the partial result - the values
|
|
93
93
|
* the deterministic pass has already resolved.
|
|
94
94
|
*/
|
|
95
95
|
function collectKnownValues(data, missing) {
|
|
@@ -233,7 +233,7 @@ export const prompt = {
|
|
|
233
233
|
/**
|
|
234
234
|
* Parse a raw LLM response permissively. Accepts either an already-decoded
|
|
235
235
|
* object or a JSON-encoded string. Each field listed in `missing` is
|
|
236
|
-
* validated individually against its Zod schema
|
|
236
|
+
* validated individually against its Zod schema - valid fields flow into
|
|
237
237
|
* `values`, invalid ones are dropped and surfaced as warnings. Keys outside
|
|
238
238
|
* `missing` are dropped as well, with a single aggregated warning so the
|
|
239
239
|
* caller can spot a prompt/provider mismatch.
|
|
@@ -244,7 +244,7 @@ export const prompt = {
|
|
|
244
244
|
* @param schema - Zod object schema whose fields back the validation.
|
|
245
245
|
* @param missing - Fields the LLM was expected to produce (typically
|
|
246
246
|
* {@link ExtractionResult.missing}).
|
|
247
|
-
* @param raw - The provider response
|
|
247
|
+
* @param raw - The provider response - object or JSON string.
|
|
248
248
|
*/
|
|
249
249
|
parse(schema, missing, raw) {
|
|
250
250
|
const missingKeys = missing;
|
package/dist/rules.d.ts
CHANGED
|
@@ -10,18 +10,23 @@ export declare const rule: {
|
|
|
10
10
|
* Declare a deterministic extraction rule targeting a single schema field.
|
|
11
11
|
*
|
|
12
12
|
* The `extract` callback receives the raw content and must return either a
|
|
13
|
-
* {@link RuleMatch} or `null` when the rule does not apply.
|
|
13
|
+
* {@link RuleMatch} or `null` when the rule does not apply. It may also
|
|
14
|
+
* accept an optional caller-defined `context` forwarded verbatim by
|
|
15
|
+
* {@link rule.apply} / {@link Extractor.extract}.
|
|
14
16
|
*
|
|
17
|
+
* @typeParam TContext - Shape of the optional context forwarded to
|
|
18
|
+
* `extract`. Defaults to `unknown`.
|
|
15
19
|
* @param field - Name of the schema field the rule writes to.
|
|
16
|
-
* @param extract - Callback that inspects the content and
|
|
20
|
+
* @param extract - Callback that inspects the content (and optional context)
|
|
21
|
+
* and proposes a value.
|
|
17
22
|
* @param options - Optional rule metadata. `id` is surfaced in
|
|
18
23
|
* `ExtractionResult.sources` when this rule produces the kept value;
|
|
19
24
|
* defaults to `${field}#${declarationIndex}`.
|
|
20
25
|
* @returns An {@link ExtractionRule} ready to be passed to {@link rule.apply}.
|
|
21
26
|
*/
|
|
22
|
-
create(field: string, extract: (content: string) => RuleMatch<unknown> | null, options?: {
|
|
27
|
+
create<TContext = unknown>(field: string, extract: (content: string, context?: TContext) => RuleMatch<unknown> | null, options?: {
|
|
23
28
|
id?: string;
|
|
24
|
-
}): ExtractionRule
|
|
29
|
+
}): ExtractionRule<TContext>;
|
|
25
30
|
/**
|
|
26
31
|
* Shortcut to build a regex-based {@link ExtractionRule}. On match, the
|
|
27
32
|
* value is taken from capture group 1 (or the full match if none), then
|
|
@@ -68,13 +73,21 @@ export declare const rule: {
|
|
|
68
73
|
* - Values failing the per-field Zod `safeParse` are discarded and the
|
|
69
74
|
* field falls back to `missing`. An optional logger receives a warning.
|
|
70
75
|
*
|
|
76
|
+
* The optional `context` is forwarded verbatim to every rule's `extract`
|
|
77
|
+
* callback. Rules that declare a narrower `ExtractionRule<TContext>` than
|
|
78
|
+
* the one passed here still compile thanks to contextual parameter
|
|
79
|
+
* contravariance; rules that ignore `context` keep working unchanged.
|
|
80
|
+
*
|
|
71
81
|
* @typeParam S - A Zod object schema.
|
|
82
|
+
* @typeParam TContext - Shape of the optional context forwarded to rules.
|
|
72
83
|
* @param content - Raw content to extract from (typically markdown or text).
|
|
73
84
|
* @param rules - Deterministic rules to evaluate.
|
|
74
85
|
* @param schema - Zod object schema describing the target data shape.
|
|
75
86
|
* @param logger - Optional logger notified when a value is rejected.
|
|
87
|
+
* @param context - Optional caller-defined value forwarded to every rule's
|
|
88
|
+
* `extract` callback. Left `undefined` when omitted.
|
|
76
89
|
* @returns The deterministic extraction result (values, confidence, missing).
|
|
77
90
|
*/
|
|
78
|
-
apply<S extends z.ZodObject<z.ZodRawShape
|
|
91
|
+
apply<S extends z.ZodObject<z.ZodRawShape>, TContext = unknown>(content: string, rules: ExtractionRule<TContext>[], schema: S, logger?: Logger, context?: TContext): RulesResult<z.infer<S>>;
|
|
79
92
|
};
|
|
80
93
|
//# sourceMappingURL=rules.d.ts.map
|
package/dist/rules.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rules.d.ts","sourceRoot":"","sources":["../src/rules.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,KAAK,EAAE,cAAc,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEpF;;;GAGG;AACH,eAAO,MAAM,IAAI;IACf
|
|
1
|
+
{"version":3,"file":"rules.d.ts","sourceRoot":"","sources":["../src/rules.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,KAAK,EAAE,cAAc,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEpF;;;GAGG;AACH,eAAO,MAAM,IAAI;IACf;;;;;;;;;;;;;;;;;OAiBG;WACI,QAAQ,mBACN,MAAM,WACJ,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,QAAQ,KAAK,SAAS,CAAC,OAAO,CAAC,GAAG,IAAI,YACjE;QAAE,EAAE,CAAC,EAAE,MAAM,CAAA;KAAE,GACxB,cAAc,CAAC,QAAQ,CAAC;IAI3B;;;;;;;;;;;OAWG;UACG,CAAC,kBACE,MAAM,WACJ,MAAM,mBACE,MAAM,cACX,CAAC,KAAK,EAAE,gBAAgB,KAAK,CAAC,YAChC;QAAE,EAAE,CAAC,EAAE,MAAM,CAAA;KAAE,GACxB,cAAc;IAYjB;;;;;;;;;;;;;;;;;OAiBG;eACQ,CAAC,SAAS,CAAC,SAAS,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;IAIpD;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;UACG,CAAC,SAAS,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,EAAE,QAAQ,qBACzC,MAAM,SACR,cAAc,CAAC,QAAQ,CAAC,EAAE,UACzB,CAAC,WACA,MAAM,YACL,QAAQ,GACjB,WAAW,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;CAwC3B,CAAC"}
|
package/dist/rules.js
CHANGED
|
@@ -7,10 +7,15 @@ export const rule = {
|
|
|
7
7
|
* Declare a deterministic extraction rule targeting a single schema field.
|
|
8
8
|
*
|
|
9
9
|
* The `extract` callback receives the raw content and must return either a
|
|
10
|
-
* {@link RuleMatch} or `null` when the rule does not apply.
|
|
10
|
+
* {@link RuleMatch} or `null` when the rule does not apply. It may also
|
|
11
|
+
* accept an optional caller-defined `context` forwarded verbatim by
|
|
12
|
+
* {@link rule.apply} / {@link Extractor.extract}.
|
|
11
13
|
*
|
|
14
|
+
* @typeParam TContext - Shape of the optional context forwarded to
|
|
15
|
+
* `extract`. Defaults to `unknown`.
|
|
12
16
|
* @param field - Name of the schema field the rule writes to.
|
|
13
|
-
* @param extract - Callback that inspects the content and
|
|
17
|
+
* @param extract - Callback that inspects the content (and optional context)
|
|
18
|
+
* and proposes a value.
|
|
14
19
|
* @param options - Optional rule metadata. `id` is surfaced in
|
|
15
20
|
* `ExtractionResult.sources` when this rule produces the kept value;
|
|
16
21
|
* defaults to `${field}#${declarationIndex}`.
|
|
@@ -75,14 +80,22 @@ export const rule = {
|
|
|
75
80
|
* - Values failing the per-field Zod `safeParse` are discarded and the
|
|
76
81
|
* field falls back to `missing`. An optional logger receives a warning.
|
|
77
82
|
*
|
|
83
|
+
* The optional `context` is forwarded verbatim to every rule's `extract`
|
|
84
|
+
* callback. Rules that declare a narrower `ExtractionRule<TContext>` than
|
|
85
|
+
* the one passed here still compile thanks to contextual parameter
|
|
86
|
+
* contravariance; rules that ignore `context` keep working unchanged.
|
|
87
|
+
*
|
|
78
88
|
* @typeParam S - A Zod object schema.
|
|
89
|
+
* @typeParam TContext - Shape of the optional context forwarded to rules.
|
|
79
90
|
* @param content - Raw content to extract from (typically markdown or text).
|
|
80
91
|
* @param rules - Deterministic rules to evaluate.
|
|
81
92
|
* @param schema - Zod object schema describing the target data shape.
|
|
82
93
|
* @param logger - Optional logger notified when a value is rejected.
|
|
94
|
+
* @param context - Optional caller-defined value forwarded to every rule's
|
|
95
|
+
* `extract` callback. Left `undefined` when omitted.
|
|
83
96
|
* @returns The deterministic extraction result (values, confidence, missing).
|
|
84
97
|
*/
|
|
85
|
-
apply(content, rules, schema, logger) {
|
|
98
|
+
apply(content, rules, schema, logger, context) {
|
|
86
99
|
const schemaKeys = Object.keys(schema.shape);
|
|
87
100
|
const values = {};
|
|
88
101
|
const confidenceMap = {};
|
|
@@ -93,7 +106,7 @@ export const rule = {
|
|
|
93
106
|
if (!schemaKeys.includes(field)) {
|
|
94
107
|
continue;
|
|
95
108
|
}
|
|
96
|
-
const match = candidate.extract(content);
|
|
109
|
+
const match = candidate.extract(content, context);
|
|
97
110
|
if (match === null) {
|
|
98
111
|
continue;
|
|
99
112
|
}
|
package/dist/rules.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rules.js","sourceRoot":"","sources":["../src/rules.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG;IAClB
|
|
1
|
+
{"version":3,"file":"r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AI,kBAAkB,KAAK,SAAS,IAAI,KAAK,CAAC,UAAU,IAAI,kBAAkB,EAAE,CAAC;gBAC/E,SAAS;YACX,CAAC;YACD,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,IAAwB,CAAC;YAChD,aAAa,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,UAAU,CAAC;YACxC,SAAS,CAAC,KAAK,CAAC,GAAG,SAAS,CAAC,EAAE,IAAI,GAAG,SAAS,CAAC,KAAK,IAAI,KAAK,EAAE,CAAC;QACnE,CAAC;QAED,MAAM,OAAO,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,IAAI,MAAM,CAAC,CAAC,CAAC;QAE7D,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,aAAa,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC;IACnE,CAAC;CACF,CAAC"}
|
|
@@ -51,12 +51,15 @@ export type ExtractorLlmConfig = {
|
|
|
51
51
|
* produce values for each field before any LLM fallback kicks in.
|
|
52
52
|
*
|
|
53
53
|
* @typeParam S - A Zod object schema describing the target data shape.
|
|
54
|
+
* @typeParam TContext - Shape of the optional per-call context forwarded to
|
|
55
|
+
* every rule's `extract` callback. Defaults to `unknown`, which keeps
|
|
56
|
+
* context-unaware rules assignable with zero boilerplate.
|
|
54
57
|
*/
|
|
55
|
-
export type ExtractorConfig<S extends z.ZodObject<z.ZodRawShape
|
|
58
|
+
export type ExtractorConfig<S extends z.ZodObject<z.ZodRawShape>, TContext = unknown> = {
|
|
56
59
|
/** Zod object schema the extractor targets. Drives field enumeration and re-validation. */
|
|
57
60
|
schema: S;
|
|
58
61
|
/** Deterministic rules evaluated against the raw content before any LLM fallback. */
|
|
59
|
-
rules: ExtractionRule[];
|
|
62
|
+
rules: ExtractionRule<TContext>[];
|
|
60
63
|
/** Optional LLM fallback invoked for fields the rules could not produce. */
|
|
61
64
|
llm?: ExtractorLlmConfig;
|
|
62
65
|
/** Post-merge transformations, forwarded to every `merge.apply` call. */
|
|
@@ -81,21 +84,25 @@ export type ExtractorConfig<S extends z.ZodObject<z.ZodRawShape>> = {
|
|
|
81
84
|
* this interface as the matching slice introduces them.
|
|
82
85
|
*
|
|
83
86
|
* @typeParam T - Shape of the target data object inferred from a Zod schema.
|
|
87
|
+
* @typeParam TContext - Shape of the optional per-call context forwarded to
|
|
88
|
+
* every rule's `extract` callback. Defaults to `unknown`.
|
|
84
89
|
*/
|
|
85
|
-
export type Extractor<T> = {
|
|
90
|
+
export type Extractor<T, TContext = unknown> = {
|
|
86
91
|
/**
|
|
87
92
|
* Run the full extraction pipeline against `content`: deterministic rules,
|
|
88
93
|
* optionally followed by an LLM fallback for missing fields, then the merge
|
|
89
|
-
* + validation step.
|
|
94
|
+
* + validation step. An optional `context` is forwarded verbatim to every
|
|
95
|
+
* rule's `extract` callback.
|
|
90
96
|
*/
|
|
91
|
-
extract(content: string): Promise<ExtractionResult<T>>;
|
|
97
|
+
extract(content: string, context?: TContext): Promise<ExtractionResult<T>>;
|
|
92
98
|
/**
|
|
93
99
|
* Run the deterministic rules and merge them against a `null` LLM result.
|
|
94
100
|
* Synchronous counterpart to {@link Extractor.extract} for batch workflows
|
|
95
101
|
* where the LLM call is managed by the caller (queues, scheduled jobs,
|
|
96
|
-
* external batch APIs).
|
|
102
|
+
* external batch APIs). An optional `context` is forwarded verbatim to
|
|
103
|
+
* every rule's `extract` callback.
|
|
97
104
|
*/
|
|
98
|
-
extractSync(content: string): ExtractionResult<T>;
|
|
105
|
+
extractSync(content: string, context?: TContext): ExtractionResult<T>;
|
|
99
106
|
/**
|
|
100
107
|
* Build the LLM request for `partial`. The target field set depends on the
|
|
101
108
|
* configured `llm.mode`: `'fill-gaps'` (default) covers only
|
|
@@ -112,10 +119,10 @@ export type Extractor<T> = {
|
|
|
112
119
|
*/
|
|
113
120
|
parse(raw: unknown): LlmResult;
|
|
114
121
|
/**
|
|
115
|
-
* Merge a previously-obtained `partial` with an LLM result
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
122
|
+
* Merge a previously-obtained `partial` with an LLM result to produce the
|
|
123
|
+
* final {@link ExtractionResult}. Reuses the per-field values, confidence
|
|
124
|
+
* and provenance already carried by `partial`: the deterministic rules are
|
|
125
|
+
* not re-evaluated, so no `context` parameter is accepted here.
|
|
119
126
|
*/
|
|
120
127
|
merge(partial: ExtractionResult<T>, llmResult: LlmResult, content: string): ExtractionResult<T>;
|
|
121
128
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extractor.types.d.ts","sourceRoot":"","sources":["../../src/types/extractor.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,KAAK,EACV,aAAa,EACb,gBAAgB,EAChB,gBAAgB,EAChB,SAAS,EACT,UAAU,EACX,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,KAAK,EAAE,eAAe,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEtF;;;;GAIG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC/B,+DAA+D;IAC/D,QAAQ,EAAE,WAAW,CAAC;IACtB,4GAA4G;IAC5G,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;;;OAKG;IACH,IAAI,CAAC,EAAE,eAAe,CAAC;IACvB;;;OAGG;IACH,eAAe,CAAC,EAAE,eAAe,CAAC;IAClC;;;;;;;OAOG;IACH,gBAAgB,CAAC,EAAE,CACjB,OAAO,EAAE,UAAU,EACnB,OAAO,EAAE,M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,CAAC,CAAC,CAAC;IACtE;;;;;;OAMG;IACH,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC;IAClE;;;;;OAKG;IACH,KAAK,CAAC,GAAG,EAAE,OAAO,GAAG,SAAS,CAAC;IAC/B;;;;;OAKG;IACH,KAAK,CACH,OAAO,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAC5B,SAAS,EAAE,SAAS,EACpB,OAAO,EAAE,MAAM,GACd,gBAAgB,CAAC,CAAC,CAAC,CAAC;CACxB,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Minimal logger interface consumed by llmbic internals. Any logger that
|
|
3
|
-
* exposes a `warn` method (and optionally `info`) can be plugged in
|
|
3
|
+
* exposes a `warn` method (and optionally `info`) can be plugged in -
|
|
4
4
|
* pino, winston, `console`, or a test double.
|
|
5
5
|
*/
|
|
6
6
|
export type Logger = {
|
|
@@ -48,10 +48,10 @@ export type MergeApplyOptions<T> = {
|
|
|
48
48
|
/**
|
|
49
49
|
* Strategy applied when the rule and the LLM disagree on a field value.
|
|
50
50
|
*
|
|
51
|
-
* - `'flag'`
|
|
51
|
+
* - `'flag'` - keep the rule value, lower its confidence, and record a
|
|
52
52
|
* {@link Conflict} so the caller can review the disagreement.
|
|
53
|
-
* - `'prefer-rule'`
|
|
54
|
-
* - `'prefer-llm'`
|
|
53
|
+
* - `'prefer-rule'` - silently keep the rule value and its confidence.
|
|
54
|
+
* - `'prefer-llm'` - silently keep the LLM value and the default LLM
|
|
55
55
|
* confidence.
|
|
56
56
|
*/
|
|
57
57
|
export type ConflictStrategy = 'flag' | 'prefer-rule' | 'prefer-llm';
|
|
@@ -8,7 +8,7 @@ export type LlmProvider = {
|
|
|
8
8
|
/**
|
|
9
9
|
* Send `request` to the underlying model and return the structured values
|
|
10
10
|
* it produced. Observability concerns (token counters, latency, cost) are
|
|
11
|
-
* the caller's responsibility
|
|
11
|
+
* the caller's responsibility - they live outside the llmbic contract so
|
|
12
12
|
* the library stays free of vendor-specific metering.
|
|
13
13
|
*
|
|
14
14
|
* @param request - Prompt, user content, and JSON Schema built by {@link prompt.build}.
|
|
@@ -13,8 +13,18 @@ export type RuleMatch<T> = {
|
|
|
13
13
|
/**
|
|
14
14
|
* A deterministic rule that tries to extract a single schema field from raw
|
|
15
15
|
* content. `extract` returns `null` when the rule does not apply.
|
|
16
|
+
*
|
|
17
|
+
* Rules can optionally accept a `context` argument: an opaque, caller-defined
|
|
18
|
+
* value forwarded verbatim by `rule.apply` / `Extractor.extract`. Typical use
|
|
19
|
+
* is to expose per-call metadata (locale, tenant-specific configuration,
|
|
20
|
+
* feature flags) rules need to decide whether they apply. `TContext`
|
|
21
|
+
* defaults to `unknown` so context-unaware rules stay assignable to arrays
|
|
22
|
+
* typed with any context.
|
|
23
|
+
*
|
|
24
|
+
* @typeParam TContext - Shape of the optional per-call context forwarded to
|
|
25
|
+
* `extract`. Defaults to `unknown`.
|
|
16
26
|
*/
|
|
17
|
-
export type ExtractionRule = {
|
|
27
|
+
export type ExtractionRule<TContext = unknown> = {
|
|
18
28
|
/**
|
|
19
29
|
* Stable identifier surfaced in `ExtractionResult.sources` when this rule
|
|
20
30
|
* produces the kept value. Optional: when omitted, `rule.apply` assigns
|
|
@@ -24,8 +34,13 @@ export type ExtractionRule = {
|
|
|
24
34
|
id?: string;
|
|
25
35
|
/** Name of the schema field this rule targets. */
|
|
26
36
|
field: string;
|
|
27
|
-
/**
|
|
28
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Inspects `content` - and optionally a caller-provided `context` -
|
|
39
|
+
* and returns a match, or `null` if nothing was found. `context` is
|
|
40
|
+
* forwarded verbatim by `rule.apply` / `Extractor.extract` and left
|
|
41
|
+
* `undefined` when the caller passes no context.
|
|
42
|
+
*/
|
|
43
|
+
extract: (content: string, context?: TContext) => RuleMatch<unknown> | null;
|
|
29
44
|
};
|
|
30
45
|
/**
|
|
31
46
|
* The output of the rules pass. Contains the values that deterministic rules
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rule.types.d.ts","sourceRoot":"","sources":["../../src/types/rule.types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI;IACzB,gDAAgD;IAChD,KAAK,EAAE,CAAC,CAAC;IACT,4EAA4E;IAC5E,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF
|
|
1
|
+
{"version":3,"file":"rule.types.d.ts","sourceRoot":"","sources":["../../src/types/rule.types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI;IACzB,gDAAgD;IAChD,KAAK,EAAE,CAAC,CAAC;IACT,4EAA4E;IAC5E,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,cAAc,CAAC,QAAQ,GAAG,OAAO,IAAI;IAC/C;;;;;OAKG;IACH,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,kDAAkD;IAClD,KAAK,EAAE,MAAM,CAAC;IACd;;;;;OAKG;IACH,OAAO,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,QAAQ,KAAK,SAAS,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC;CAC7E,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI;IAC3B,mEAAmE;IACnE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IACnB,yDAAyD;IACzD,UAAU,EAAE,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC;IAC7C;;;;;;;OAOG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC;IAC7C,8DAA8D;IAC9D,OAAO,EAAE,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC;CACtB,CAAC"}
|