llmbic 1.0.0 → 1.2.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 +41 -0
- package/README.md +177 -29
- package/dist/extractor.d.ts +13 -7
- package/dist/extractor.d.ts.map +1 -1
- package/dist/extractor.js +50 -22
- package/dist/extractor.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/merge.d.ts.map +1 -1
- package/dist/merge.js +50 -4
- package/dist/merge.js.map +1 -1
- package/dist/prompt.d.ts +20 -13
- package/dist/prompt.d.ts.map +1 -1
- package/dist/prompt.js +97 -39
- package/dist/prompt.js.map +1 -1
- package/dist/rules.d.ts +9 -2
- package/dist/rules.d.ts.map +1 -1
- package/dist/rules.js +19 -15
- package/dist/rules.js.map +1 -1
- package/dist/types/extractor.types.d.ts +54 -4
- package/dist/types/extractor.types.d.ts.map +1 -1
- package/dist/types/merge.types.d.ts +42 -0
- package/dist/types/merge.types.d.ts.map +1 -1
- package/dist/types/prompt.types.d.ts +29 -0
- package/dist/types/prompt.types.d.ts.map +1 -1
- package/dist/types/rule.types.d.ts +16 -0
- package/dist/types/rule.types.d.ts.map +1 -1
- package/package.json +4 -1
package/dist/merge.js
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
* value, fuse it with the LLM candidate via {@link merge.field}, and collect
|
|
4
4
|
* per-field outcomes. Invoked once at the top of {@link merge.apply}.
|
|
5
5
|
*/
|
|
6
|
-
function fuseAllFields(schemaKeys, rulesResult, llmResult, policy, logger) {
|
|
6
|
+
function fuseAllFields(schemaKeys, rulesResult, llmResult, policy, policyByField, logger) {
|
|
7
7
|
const data = {};
|
|
8
8
|
const confidence = {};
|
|
9
|
+
const sources = {};
|
|
9
10
|
const conflicts = [];
|
|
10
11
|
const missing = [];
|
|
11
12
|
let rulesMatched = 0;
|
|
@@ -23,9 +24,13 @@ function fuseAllFields(schemaKeys, rulesResult, llmResult, policy, logger) {
|
|
|
23
24
|
rulesMatched += 1;
|
|
24
25
|
}
|
|
25
26
|
const llmValue = llmResult?.values[field] ?? null;
|
|
26
|
-
const
|
|
27
|
+
const fieldOverride = policyByField?.[field];
|
|
28
|
+
const resolvedPolicy = fieldOverride === undefined ? policy : { ...policy, ...fieldOverride };
|
|
29
|
+
const ruleId = rulesResult.sourceIds?.[field];
|
|
30
|
+
const fused = merge.field(field, ruleMatch, llmValue, resolvedPolicy, logger);
|
|
27
31
|
data[field] = fused.value;
|
|
28
32
|
confidence[field] = fused.confidence;
|
|
33
|
+
sources[field] = deriveSource(fused, ruleMatch, llmValue, resolvedPolicy, ruleId);
|
|
29
34
|
if (fused.conflict !== undefined) {
|
|
30
35
|
conflicts.push(fused.conflict);
|
|
31
36
|
}
|
|
@@ -33,7 +38,47 @@ function fuseAllFields(schemaKeys, rulesResult, llmResult, policy, logger) {
|
|
|
33
38
|
missing.push(field);
|
|
34
39
|
}
|
|
35
40
|
}
|
|
36
|
-
return { data, confidence, conflicts, missing, rulesMatched };
|
|
41
|
+
return { data, confidence, sources, conflicts, missing, rulesMatched };
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Classify the origin of a fused value into a {@link FieldSource}. Mirrors
|
|
45
|
+
* the decision tree of {@link merge.field} without re-running the strategy:
|
|
46
|
+
*
|
|
47
|
+
* - rule alone -> `'rule'`
|
|
48
|
+
* - LLM alone -> `'llm'`
|
|
49
|
+
* - both null -> `null`
|
|
50
|
+
* - both present, conflict recorded -> `'flag'` (only the `'flag'` strategy
|
|
51
|
+
* produces a conflict)
|
|
52
|
+
* - both present, no conflict, kept value differs from the rule -> `'llm'`
|
|
53
|
+
* (only `'prefer-llm'` reaches this case)
|
|
54
|
+
* - both present, no conflict, kept value matches the rule -> `'agreement'`
|
|
55
|
+
* when the policy's `compare` returns true, else `'rule'` (`'prefer-rule'`
|
|
56
|
+
* silent path)
|
|
57
|
+
*
|
|
58
|
+
* `ruleId` is `''` when the rule provided no declared id and `rule.apply`
|
|
59
|
+
* was bypassed by the caller.
|
|
60
|
+
*/
|
|
61
|
+
function deriveSource(fused, ruleMatch, llmValue, policy, ruleId) {
|
|
62
|
+
if (fused.value === null) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
const id = ruleId ?? '';
|
|
66
|
+
if (ruleMatch === null) {
|
|
67
|
+
return { kind: 'llm' };
|
|
68
|
+
}
|
|
69
|
+
if (llmValue === null || llmValue === undefined) {
|
|
70
|
+
return { kind: 'rule', ruleId: id };
|
|
71
|
+
}
|
|
72
|
+
if (fused.conflict !== undefined) {
|
|
73
|
+
return { kind: 'flag', ruleId: id };
|
|
74
|
+
}
|
|
75
|
+
if (fused.value !== ruleMatch.value) {
|
|
76
|
+
return { kind: 'llm' };
|
|
77
|
+
}
|
|
78
|
+
const compare = policy?.compare ?? merge.defaultFieldPolicy.compare;
|
|
79
|
+
return compare(ruleMatch.value, llmValue)
|
|
80
|
+
? { kind: 'agreement', ruleId: id }
|
|
81
|
+
: { kind: 'rule', ruleId: id };
|
|
37
82
|
}
|
|
38
83
|
/**
|
|
39
84
|
* Apply every configured {@link Normalizer} to the merged data in declared
|
|
@@ -209,13 +254,14 @@ export const merge = {
|
|
|
209
254
|
*/
|
|
210
255
|
apply(schema, rulesResult, llmResult, content, options) {
|
|
211
256
|
const schemaKeys = Object.keys(schema.shape);
|
|
212
|
-
const fusion = fuseAllFields(schemaKeys, rulesResult, llmResult, options?.policy, options?.logger);
|
|
257
|
+
const fusion = fuseAllFields(schemaKeys, rulesResult, llmResult, options?.policy, options?.policyByField, options?.logger);
|
|
213
258
|
const normalized = runNormalizers(fusion.data, options?.normalizers, content);
|
|
214
259
|
const violations = collectViolations(schema, normalized, fusion.missing, options?.validators);
|
|
215
260
|
const valid = !violations.some((v) => v.severity === 'error');
|
|
216
261
|
return {
|
|
217
262
|
data: normalized,
|
|
218
263
|
confidence: fusion.confidence,
|
|
264
|
+
sources: fusion.sources,
|
|
219
265
|
conflicts: fusion.conflicts,
|
|
220
266
|
missing: fusion.missing,
|
|
221
267
|
validation: { valid, violations },
|
package/dist/merge.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merge.js","sourceRoot":"","sources":["../src/merge.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"merge.js","sourceRoot":"","sources":["../src/merge.ts"],"names":[],"mappings":"AAyBA;;;;GAIG;AACH,SAAS,aAAa,CACpB,UAAuB,EACvB,WAA2B,EAC3B,SAA2B,EAC3B,MAA6C,EAC7C,aAAyE,EACzE,MAA0B;IAE1B,MAAM,IAAI,GAAG,EAAsB,CAAC;IACpC,MAAM,UAAU,GAAG,EAAuC,CAAC;IAC3D,MAAM,OAAO,GAAG,EAA4C,CAAC;IAC7D,MAAM,SAAS,GAAe,EAAE,CAAC;IACjC,MAAM,OAAO,GAAgB,EAAE,CAAC;IAChC,IAAI,YAAY,GAAG,CAAC,CAAC;IAErB,KAAK,MAAM,KAAK,IAAI,UAAU,EAAE,CAAC;QAC/B,MAAM,YAAY,GAAG,KAAK,IAAI,WAAW,CAAC,MAAM,CAAC;QACjD,6EAA6E;QAC7E,mDAAmD;QACnD,MAAM,SAAS,GAA8B,YAAY;YACvD,CAAC,CAAC;gBACE,KAAK,EAAE,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC;gBAChC,UAAU,EAAE,WAAW,CAAC,UAAU,CAAC,KAAK,CAAW;aACpD;YACH,CAAC,CAAC,IAAI,CAAC;QACT,IAAI,YAAY,EAAE,CAAC;YACjB,YAAY,IAAI,CAAC,CAAC;QACpB,CAAC;QAED,MAAM,QAAQ,GAAG,SAAS,EAAE,MAAM,CAAC,KAAe,CAAC,IAAI,IAAI,CAAC;QAE5D,MAAM,aAAa,GAAG,aAAa,EAAE,CAAC,KAAK,CAAC,CAAC;QAC7C,MAAM,cAAc,GAClB,aAAa,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE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AI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC;QAE9D,OAAO;YACL,IAAI,EAAE,UAAU;YAChB,UAAU,EAAE,MAAM,CAAC,UAAU;YAC7B,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,SAAS,EAAE,MAAM,CAAC,SAAS;YAC3B,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,UAAU,EAAE,EAAE,KAAK,EAAE,UAAU,EAAE;YACjC,IAAI,EAAE;gBACJ,YAAY,EAAE,MAAM,CAAC,YAAY;gBACjC,SAAS,EAAE,SAAS,KAAK,IAAI;gBAC7B,UAAU,EAAE,CAAC;aACd;SACF,CAAC;IACJ,CAAC;CACF,CAAC"}
|
package/dist/prompt.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { z } from 'zod';
|
|
2
2
|
import type { ExtractionResult, LlmResult } from './types/merge.types.js';
|
|
3
|
-
import type { LlmRequest } from './types/prompt.types.js';
|
|
3
|
+
import type { LlmRequest, PromptBuildOptions } from './types/prompt.types.js';
|
|
4
4
|
/**
|
|
5
5
|
* Prompt-building primitives that turn a partial extraction result into an
|
|
6
6
|
* {@link LlmRequest} targeted at the fields the deterministic pass could not
|
|
@@ -8,27 +8,34 @@ import type { LlmRequest } from './types/prompt.types.js';
|
|
|
8
8
|
*/
|
|
9
9
|
export declare const prompt: {
|
|
10
10
|
/**
|
|
11
|
-
* Build an LLM request
|
|
12
|
-
* is a JSON Schema covering only those fields, and values already produced
|
|
13
|
-
* by the deterministic pass are surfaced both as `knownValues` and as a
|
|
14
|
-
* hint block prepended to `userContent`.
|
|
11
|
+
* Build an LLM request targeting a subset of the schema's fields.
|
|
15
12
|
*
|
|
16
|
-
*
|
|
13
|
+
* - In `'fill-gaps'` mode (default) the response schema covers only
|
|
14
|
+
* `partial.missing`, and rule values flow back to the LLM as hints both
|
|
15
|
+
* through `knownValues` and a prepended "Already extracted" block in
|
|
16
|
+
* `userContent`.
|
|
17
|
+
* - In `'cross-check'` mode the response schema covers every schema field,
|
|
18
|
+
* so {@link merge.apply} can surface agreements or disagreements with
|
|
19
|
+
* the rule pass. `crossCheckHints: 'unbiased'` (default) drops the hint
|
|
20
|
+
* block and empties `knownValues` so the LLM re-extracts from scratch;
|
|
21
|
+
* `'bias'` keeps the hints to save tokens at the cost of confirmation
|
|
22
|
+
* bias.
|
|
23
|
+
*
|
|
24
|
+
* Orchestration only: the four phases (response-schema build, known-values
|
|
17
25
|
* collection, user-content formatting, request assembly) each live in their
|
|
18
26
|
* own private helper above.
|
|
19
27
|
*
|
|
20
28
|
* @typeParam S - A Zod object schema describing the full target shape.
|
|
21
29
|
* @param schema - Zod object schema that drives the field selection.
|
|
22
30
|
* @param partial - Output of {@link merge.apply} (or any equivalent partial)
|
|
23
|
-
*
|
|
31
|
+
* `data` is always read; `missing` drives the fill-gaps schema and the
|
|
32
|
+
* hint block.
|
|
24
33
|
* @param content - Original text the request will refer to.
|
|
25
|
-
* @param options - Optional
|
|
26
|
-
* @throws When a
|
|
27
|
-
*
|
|
34
|
+
* @param options - Optional overrides: `systemPrompt`, `mode`, `crossCheckHints`.
|
|
35
|
+
* @throws When a target field uses an unsupported Zod kind; the error
|
|
36
|
+
* message names the offending field.
|
|
28
37
|
*/
|
|
29
|
-
build<S extends z.ZodObject<z.ZodRawShape>>(schema: S, partial: Pick<ExtractionResult<z.infer<S>>, "data" | "missing">, content: string, options?:
|
|
30
|
-
systemPrompt?: string;
|
|
31
|
-
}): LlmRequest;
|
|
38
|
+
build<S extends z.ZodObject<z.ZodRawShape>>(schema: S, partial: Pick<ExtractionResult<z.infer<S>>, "data" | "missing">, content: string, options?: PromptBuildOptions): LlmRequest;
|
|
32
39
|
/**
|
|
33
40
|
* Parse a raw LLM response permissively. Accepts either an already-decoded
|
|
34
41
|
* object or a JSON-encoded string. Each field listed in `missing` is
|
package/dist/prompt.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt.d.ts","sourceRoot":"","sources":["../src/prompt.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,KAAK,EAEV,gBAAgB,EAChB,SAAS,EACV,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"prompt.d.ts","sourceRoot":"","sources":["../src/prompt.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,KAAK,EAEV,gBAAgB,EAChB,SAAS,EACV,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAuN9E;;;;GAIG;AACH,eAAO,MAAM,MAAM;IACjB;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;UACG,CAAC,SAAS,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,UAChC,CAAC,WACA,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC,WACtD,MAAM,YACL,kBAAkB,GAC3B,UAAU;IAsBb;;;;;;;;;;;;;;;OAeG;UACG,CAAC,SAAS,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,UAChC,CAAC,WACA,SAAS,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,OACjC,OAAO,GACX,SAAS;CAiBb,CAAC"}
|
package/dist/prompt.js
CHANGED
|
@@ -1,41 +1,80 @@
|
|
|
1
1
|
const DEFAULT_SYSTEM_PROMPT = 'Extract the listed fields from the content as a JSON object.';
|
|
2
2
|
/**
|
|
3
|
-
* Convert a
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* their schema.
|
|
3
|
+
* Convert a `z.nullable(inner)` into JSON Schema by recursing into `inner`
|
|
4
|
+
* and widening its `type` to `[innerType, 'null']`. Refuses nested nullables
|
|
5
|
+
* whose inner already carries a tuple `type`.
|
|
7
6
|
*/
|
|
8
|
-
function
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
if (
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
if (kind === 'number') {
|
|
15
|
-
return { type: 'number' };
|
|
16
|
-
}
|
|
17
|
-
if (kind === 'boolean') {
|
|
18
|
-
return { type: 'boolean' };
|
|
19
|
-
}
|
|
20
|
-
if (kind === 'enum') {
|
|
21
|
-
const entries = def.entries;
|
|
22
|
-
return { type: 'string', enum: Object.values(entries) };
|
|
7
|
+
function nullableToJsonSchema(def, field) {
|
|
8
|
+
const inner = zodFieldToJsonSchema(def.innerType, field);
|
|
9
|
+
const innerType = inner.type;
|
|
10
|
+
if (typeof innerType !== 'string') {
|
|
11
|
+
throw new Error(`Unsupported nested nullable on field "${field}"`);
|
|
23
12
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
13
|
+
return { ...inner, type: [innerType, 'null'] };
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Convert a `z.object(shape)` into JSON Schema by recursing over every
|
|
17
|
+
* property. Children wrapped in `z.optional(...)` are kept in `properties`
|
|
18
|
+
* but excluded from the object-level `required` list.
|
|
19
|
+
*/
|
|
20
|
+
function objectToJsonSchema(def, field) {
|
|
21
|
+
const shape = def.shape;
|
|
22
|
+
const properties = {};
|
|
23
|
+
const required = [];
|
|
24
|
+
for (const [key, child] of Object.entries(shape)) {
|
|
25
|
+
properties[key] = zodFieldToJsonSchema(child, `${field}.${key}`);
|
|
26
|
+
if (child.def.type !== 'optional') {
|
|
27
|
+
required.push(key);
|
|
28
28
|
}
|
|
29
|
-
return { ...inner, type: [inner.type, 'null'] };
|
|
30
29
|
}
|
|
31
|
-
|
|
30
|
+
return { type: 'object', properties, required, additionalProperties: false };
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Dispatch the conversion by Zod kind. Primitives short-circuit, wrappers
|
|
34
|
+
* (`optional`, `default`, `nullable`, `array`, `object`) recurse, unsupported
|
|
35
|
+
* kinds throw with the offending `field` named so the caller can restructure
|
|
36
|
+
* their schema.
|
|
37
|
+
*/
|
|
38
|
+
function zodKindToJsonSchema(def, kind, field) {
|
|
39
|
+
switch (kind) {
|
|
40
|
+
case 'string':
|
|
41
|
+
case 'number':
|
|
42
|
+
case 'boolean':
|
|
43
|
+
return { type: kind };
|
|
44
|
+
case 'enum':
|
|
45
|
+
return { type: 'string', enum: Object.values(def.entries) };
|
|
46
|
+
case 'nullable':
|
|
47
|
+
return nullableToJsonSchema(def, field);
|
|
48
|
+
case 'optional':
|
|
49
|
+
case 'default':
|
|
50
|
+
return zodFieldToJsonSchema(def.innerType, field);
|
|
51
|
+
case 'array':
|
|
52
|
+
return { type: 'array', items: zodFieldToJsonSchema(def.element, field) };
|
|
53
|
+
case 'object':
|
|
54
|
+
return objectToJsonSchema(def, field);
|
|
55
|
+
default:
|
|
56
|
+
throw new Error(`Unsupported Zod type "${kind}" on field "${field}"`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Convert a single Zod field schema to JSON Schema. Wraps the kind-level
|
|
61
|
+
* dispatch with a `description` pass so `.describe()` / `.meta({ description })`
|
|
62
|
+
* registered at this recursion level flows through to the output; providers'
|
|
63
|
+
* structured-output features consume it natively.
|
|
64
|
+
*/
|
|
65
|
+
function zodFieldToJsonSchema(zodType, field) {
|
|
66
|
+
const schema = zodKindToJsonSchema(zodType.def, zodType.def.type, field);
|
|
67
|
+
const description = zodType.description;
|
|
68
|
+
return description ? { ...schema, description } : schema;
|
|
32
69
|
}
|
|
33
70
|
/**
|
|
34
71
|
* Build the JSON Schema handed to the LLM, restricted to the fields the
|
|
35
|
-
* deterministic pass could not produce.
|
|
72
|
+
* deterministic pass could not produce. Optional top-level fields are kept
|
|
73
|
+
* in `properties` but excluded from `required`.
|
|
36
74
|
*/
|
|
37
75
|
function buildResponseSchema(schema, missing) {
|
|
38
76
|
const properties = {};
|
|
77
|
+
const required = [];
|
|
39
78
|
const shape = schema.shape;
|
|
40
79
|
for (const field of missing) {
|
|
41
80
|
const zodField = shape[field];
|
|
@@ -43,8 +82,11 @@ function buildResponseSchema(schema, missing) {
|
|
|
43
82
|
continue;
|
|
44
83
|
}
|
|
45
84
|
properties[field] = zodFieldToJsonSchema(zodField, field);
|
|
85
|
+
if (zodField.def.type !== 'optional') {
|
|
86
|
+
required.push(field);
|
|
87
|
+
}
|
|
46
88
|
}
|
|
47
|
-
return { type: 'object', properties, required:
|
|
89
|
+
return { type: 'object', properties, required, additionalProperties: false };
|
|
48
90
|
}
|
|
49
91
|
/**
|
|
50
92
|
* Pick the non-null, non-missing entries of the partial result — the values
|
|
@@ -142,28 +184,44 @@ function collectUnexpectedKeys(object, missing) {
|
|
|
142
184
|
*/
|
|
143
185
|
export const prompt = {
|
|
144
186
|
/**
|
|
145
|
-
* Build an LLM request
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
187
|
+
* Build an LLM request targeting a subset of the schema's fields.
|
|
188
|
+
*
|
|
189
|
+
* - In `'fill-gaps'` mode (default) the response schema covers only
|
|
190
|
+
* `partial.missing`, and rule values flow back to the LLM as hints both
|
|
191
|
+
* through `knownValues` and a prepended "Already extracted" block in
|
|
192
|
+
* `userContent`.
|
|
193
|
+
* - In `'cross-check'` mode the response schema covers every schema field,
|
|
194
|
+
* so {@link merge.apply} can surface agreements or disagreements with
|
|
195
|
+
* the rule pass. `crossCheckHints: 'unbiased'` (default) drops the hint
|
|
196
|
+
* block and empties `knownValues` so the LLM re-extracts from scratch;
|
|
197
|
+
* `'bias'` keeps the hints to save tokens at the cost of confirmation
|
|
198
|
+
* bias.
|
|
149
199
|
*
|
|
150
|
-
* Orchestration only
|
|
200
|
+
* Orchestration only: the four phases (response-schema build, known-values
|
|
151
201
|
* collection, user-content formatting, request assembly) each live in their
|
|
152
202
|
* own private helper above.
|
|
153
203
|
*
|
|
154
204
|
* @typeParam S - A Zod object schema describing the full target shape.
|
|
155
205
|
* @param schema - Zod object schema that drives the field selection.
|
|
156
206
|
* @param partial - Output of {@link merge.apply} (or any equivalent partial)
|
|
157
|
-
*
|
|
207
|
+
* `data` is always read; `missing` drives the fill-gaps schema and the
|
|
208
|
+
* hint block.
|
|
158
209
|
* @param content - Original text the request will refer to.
|
|
159
|
-
* @param options - Optional
|
|
160
|
-
* @throws When a
|
|
161
|
-
*
|
|
210
|
+
* @param options - Optional overrides: `systemPrompt`, `mode`, `crossCheckHints`.
|
|
211
|
+
* @throws When a target field uses an unsupported Zod kind; the error
|
|
212
|
+
* message names the offending field.
|
|
162
213
|
*/
|
|
163
214
|
build(schema, partial, content, options) {
|
|
164
|
-
const
|
|
165
|
-
const
|
|
166
|
-
const
|
|
215
|
+
const mode = options?.mode ?? 'fill-gaps';
|
|
216
|
+
const crossCheckHints = options?.crossCheckHints ?? 'unbiased';
|
|
217
|
+
const targetFields = mode === 'cross-check'
|
|
218
|
+
? Object.keys(schema.shape)
|
|
219
|
+
: partial.missing;
|
|
220
|
+
const responseSchema = buildResponseSchema(schema, targetFields);
|
|
221
|
+
const exposeHints = mode === 'fill-gaps' || crossCheckHints === 'bias';
|
|
222
|
+
const knownValues = exposeHints
|
|
223
|
+
? collectKnownValues(partial.data, partial.missing)
|
|
224
|
+
: {};
|
|
167
225
|
const userContent = formatUserContent(content, knownValues);
|
|
168
226
|
return {
|
|
169
227
|
systemPrompt: options?.systemPrompt ?? DEFAULT_SYSTEM_PROMPT,
|
package/dist/prompt.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt.js","sourceRoot":"","sources":["../src/prompt.ts"],"names":[],"mappings":"AAQA,MAAM,qBAAqB,GACzB,8DAA8D,CAA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AAC,CAAC;QACvE,CAAC;QACD,OAAO,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC;IACjE,CAAC;CACF,CAAC"}
|
package/dist/rules.d.ts
CHANGED
|
@@ -14,9 +14,14 @@ export declare const rule: {
|
|
|
14
14
|
*
|
|
15
15
|
* @param field - Name of the schema field the rule writes to.
|
|
16
16
|
* @param extract - Callback that inspects the content and proposes a value.
|
|
17
|
+
* @param options - Optional rule metadata. `id` is surfaced in
|
|
18
|
+
* `ExtractionResult.sources` when this rule produces the kept value;
|
|
19
|
+
* defaults to `${field}#${declarationIndex}`.
|
|
17
20
|
* @returns An {@link ExtractionRule} ready to be passed to {@link rule.apply}.
|
|
18
21
|
*/
|
|
19
|
-
create(field: string, extract: (content: string) => RuleMatch<unknown> | null
|
|
22
|
+
create(field: string, extract: (content: string) => RuleMatch<unknown> | null, options?: {
|
|
23
|
+
id?: string;
|
|
24
|
+
}): ExtractionRule;
|
|
20
25
|
/**
|
|
21
26
|
* Shortcut to build a regex-based {@link ExtractionRule}. On match, the
|
|
22
27
|
* value is taken from capture group 1 (or the full match if none), then
|
|
@@ -29,7 +34,9 @@ export declare const rule: {
|
|
|
29
34
|
* @param transform - Optional mapper from the raw `RegExpMatchArray` to a value.
|
|
30
35
|
* @returns An {@link ExtractionRule} ready to be passed to {@link rule.apply}.
|
|
31
36
|
*/
|
|
32
|
-
regex<T = string>(field: string, pattern: RegExp, confidenceScore: number, transform?: (match: RegExpMatchArray) => T
|
|
37
|
+
regex<T = string>(field: string, pattern: RegExp, confidenceScore: number, transform?: (match: RegExpMatchArray) => T, options?: {
|
|
38
|
+
id?: string;
|
|
39
|
+
}): ExtractionRule;
|
|
33
40
|
/**
|
|
34
41
|
* Build a {@link RuleMatch} from a value and a confidence score. Syntactic
|
|
35
42
|
* sugar used inside custom rule callbacks to avoid writing the object literal
|
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;;;;;;;;;;;;OAYG;kBAEM,MAAM,WACJ,CAAC,OAAO,EAAE,MAAM,KAAK,SAAS,CAAC,OAAO,CAAC,GAAG,IAAI,YAC7C;QAAE,EAAE,CAAC,EAAE,MAAM,CAAA;KAAE,GACxB,cAAc;IAIjB;;;;;;;;;;;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;;;;;;;;;;;;;;;;;;OAkBG;UACG,CAAC,SAAS,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,WAC/B,MAAM,SACR,cAAc,EAAE,UACf,CAAC,WACA,MAAM,GACd,WAAW,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;CAwC3B,CAAC"}
|
package/dist/rules.js
CHANGED
|
@@ -11,10 +11,13 @@ export const rule = {
|
|
|
11
11
|
*
|
|
12
12
|
* @param field - Name of the schema field the rule writes to.
|
|
13
13
|
* @param extract - Callback that inspects the content and proposes a value.
|
|
14
|
+
* @param options - Optional rule metadata. `id` is surfaced in
|
|
15
|
+
* `ExtractionResult.sources` when this rule produces the kept value;
|
|
16
|
+
* defaults to `${field}#${declarationIndex}`.
|
|
14
17
|
* @returns An {@link ExtractionRule} ready to be passed to {@link rule.apply}.
|
|
15
18
|
*/
|
|
16
|
-
create(field, extract) {
|
|
17
|
-
return { field, extract };
|
|
19
|
+
create(field, extract, options) {
|
|
20
|
+
return options?.id !== undefined ? { id: options.id, field, extract } : { field, extract };
|
|
18
21
|
},
|
|
19
22
|
/**
|
|
20
23
|
* Shortcut to build a regex-based {@link ExtractionRule}. On match, the
|
|
@@ -28,18 +31,16 @@ export const rule = {
|
|
|
28
31
|
* @param transform - Optional mapper from the raw `RegExpMatchArray` to a value.
|
|
29
32
|
* @returns An {@link ExtractionRule} ready to be passed to {@link rule.apply}.
|
|
30
33
|
*/
|
|
31
|
-
regex(field, pattern, confidenceScore, transform) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const value = transform ? transform(match) : (match[1] ?? match[0]);
|
|
40
|
-
return { value, confidence: confidenceScore };
|
|
41
|
-
},
|
|
34
|
+
regex(field, pattern, confidenceScore, transform, options) {
|
|
35
|
+
const extract = (content) => {
|
|
36
|
+
const match = content.match(pattern);
|
|
37
|
+
if (!match) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
const value = transform ? transform(match) : (match[1] ?? match[0]);
|
|
41
|
+
return { value, confidence: confidenceScore };
|
|
42
42
|
};
|
|
43
|
+
return options?.id !== undefined ? { id: options.id, field, extract } : { field, extract };
|
|
43
44
|
},
|
|
44
45
|
/**
|
|
45
46
|
* Build a {@link RuleMatch} from a value and a confidence score. Syntactic
|
|
@@ -85,7 +86,9 @@ export const rule = {
|
|
|
85
86
|
const schemaKeys = Object.keys(schema.shape);
|
|
86
87
|
const values = {};
|
|
87
88
|
const confidenceMap = {};
|
|
88
|
-
|
|
89
|
+
const sourceIds = {};
|
|
90
|
+
for (let index = 0; index < rules.length; index += 1) {
|
|
91
|
+
const candidate = rules[index];
|
|
89
92
|
const field = candidate.field;
|
|
90
93
|
if (!schemaKeys.includes(field)) {
|
|
91
94
|
continue;
|
|
@@ -110,9 +113,10 @@ export const rule = {
|
|
|
110
113
|
}
|
|
111
114
|
values[field] = parsed.data;
|
|
112
115
|
confidenceMap[field] = match.confidence;
|
|
116
|
+
sourceIds[field] = candidate.id ?? `${candidate.field}#${index}`;
|
|
113
117
|
}
|
|
114
118
|
const missing = schemaKeys.filter((key) => !(key in values));
|
|
115
|
-
return { values, confidence: confidenceMap, missing };
|
|
119
|
+
return { values, confidence: confidenceMap, sourceIds, missing };
|
|
116
120
|
},
|
|
117
121
|
};
|
|
118
122
|
//# sourceMappingURL=rules.js.map
|
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":"rules.js","sourceRoot":"","sources":["../src/rules.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG;IAClB;;;;;;;;;;;;OAYG;IACH,MAAM,CACJ,KAAa,EACb,OAAuD,EACvD,OAAyB;QAEzB,OAAO,OAAO,EAAE,EAAE,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,OAAO,CAAC,EAAE,E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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"}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { z } from 'zod';
|
|
2
2
|
import type { ExtractionRule } from './rule.types.js';
|
|
3
|
-
import type { ExtractionResult, LlmResult } from './merge.types.js';
|
|
3
|
+
import type { ExtractedData, ExtractionResult, FieldMergePolicy, LlmResult, Normalizer } from './merge.types.js';
|
|
4
4
|
import type { LlmProvider } from './provider.types.js';
|
|
5
|
-
import type {
|
|
5
|
+
import type { Logger } from './logger.types.js';
|
|
6
|
+
import type { Validator } from './validate.types.js';
|
|
7
|
+
import type { CrossCheckHints, LlmRequest, PromptBuildMode } from './prompt.types.js';
|
|
6
8
|
/**
|
|
7
9
|
* LLM-fallback section of {@link ExtractorConfig}. When present, the
|
|
8
10
|
* extractor hands the fields the deterministic rules could not produce to
|
|
@@ -13,6 +15,35 @@ export type ExtractorLlmConfig = {
|
|
|
13
15
|
provider: LlmProvider;
|
|
14
16
|
/** Optional override for {@link LlmRequest.systemPrompt}; defaults to the {@link prompt.build} built-in. */
|
|
15
17
|
systemPrompt?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Field-selection strategy passed to {@link prompt.build}. In
|
|
20
|
+
* `'cross-check'` mode the extractor always calls the LLM (even when the
|
|
21
|
+
* rules resolved every field) so the merge step can surface agreements or
|
|
22
|
+
* conflicts. Defaults to `'fill-gaps'`.
|
|
23
|
+
*/
|
|
24
|
+
mode?: PromptBuildMode;
|
|
25
|
+
/**
|
|
26
|
+
* Hint-exposure policy for cross-check mode. Defaults to `'unbiased'`.
|
|
27
|
+
* Ignored when `mode !== 'cross-check'`.
|
|
28
|
+
*/
|
|
29
|
+
crossCheckHints?: CrossCheckHints;
|
|
30
|
+
/**
|
|
31
|
+
* Hook called with the fully-built {@link LlmRequest} just before
|
|
32
|
+
* `provider.complete`. Return the request to send. Useful for PII
|
|
33
|
+
* redaction (replace emails / phones / IDs in `userContent`), locale
|
|
34
|
+
* tagging (prepend `Language: ...` to `systemPrompt`), or any caller-side
|
|
35
|
+
* pre-processing. The original `content` is forwarded so the hook can
|
|
36
|
+
* cross-reference it. May be asynchronous; errors propagate to `extract`.
|
|
37
|
+
*/
|
|
38
|
+
transformRequest?: (request: LlmRequest, content: string) => LlmRequest | Promise<LlmRequest>;
|
|
39
|
+
/**
|
|
40
|
+
* Hook called with the parsed {@link LlmResult} just after
|
|
41
|
+
* `provider.complete`. Return the result the merge step should use. Useful
|
|
42
|
+
* for restoring PII-redacted values, applying caller-side post-processing,
|
|
43
|
+
* or stripping unsafe content. Receives the (possibly transformed) request
|
|
44
|
+
* for context. May be asynchronous; errors propagate to `extract`.
|
|
45
|
+
*/
|
|
46
|
+
transformResponse?: (result: LlmResult, request: LlmRequest) => LlmResult | Promise<LlmResult>;
|
|
16
47
|
};
|
|
17
48
|
/**
|
|
18
49
|
* Configuration accepted by {@link createExtractor}. A schema describes the
|
|
@@ -28,6 +59,22 @@ export type ExtractorConfig<S extends z.ZodObject<z.ZodRawShape>> = {
|
|
|
28
59
|
rules: ExtractionRule[];
|
|
29
60
|
/** Optional LLM fallback invoked for fields the rules could not produce. */
|
|
30
61
|
llm?: ExtractorLlmConfig;
|
|
62
|
+
/** Post-merge transformations, forwarded to every `merge.apply` call. */
|
|
63
|
+
normalizers?: Normalizer<z.infer<S>>[];
|
|
64
|
+
/** Invariants checked on the normalized data; populate `result.validation`. */
|
|
65
|
+
validators?: Validator<ExtractedData<z.infer<S>>>[];
|
|
66
|
+
/** Overrides for the per-field merge policy (conflict strategy, confidences, compare). */
|
|
67
|
+
policy?: Partial<FieldMergePolicy>;
|
|
68
|
+
/**
|
|
69
|
+
* Per-field policy overrides applied on top of `policy`. Precedence:
|
|
70
|
+
* library defaults < `policy` < `policyByField[field]`. Forwarded to every
|
|
71
|
+
* internal `merge.apply` call.
|
|
72
|
+
*/
|
|
73
|
+
policyByField?: {
|
|
74
|
+
[K in keyof z.infer<S>]?: Partial<FieldMergePolicy>;
|
|
75
|
+
};
|
|
76
|
+
/** Logger propagated through the merge pipeline for warnings and fallbacks. */
|
|
77
|
+
logger?: Logger;
|
|
31
78
|
};
|
|
32
79
|
/**
|
|
33
80
|
* Public surface returned by {@link createExtractor}. Methods are added to
|
|
@@ -50,8 +97,11 @@ export type Extractor<T> = {
|
|
|
50
97
|
*/
|
|
51
98
|
extractSync(content: string): ExtractionResult<T>;
|
|
52
99
|
/**
|
|
53
|
-
* Build the LLM request for
|
|
54
|
-
*
|
|
100
|
+
* Build the LLM request for `partial`. The target field set depends on the
|
|
101
|
+
* configured `llm.mode`: `'fill-gaps'` (default) covers only
|
|
102
|
+
* `partial.missing`; `'cross-check'` covers every schema field. Delegates
|
|
103
|
+
* to {@link prompt.build} with the bound schema and the configured
|
|
104
|
+
* `systemPrompt` / `crossCheckHints`.
|
|
55
105
|
*/
|
|
56
106
|
prompt(content: string, partial: ExtractionResult<T>): LlmRequest;
|
|
57
107
|
/**
|
|
@@ -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,
|
|
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,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