@sembl/core 0.1.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 +21 -0
- package/README.md +103 -0
- package/dist/index.d.ts +742 -0
- package/dist/index.js +1059 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sembl 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,103 @@
|
|
|
1
|
+
# @sembl/core
|
|
2
|
+
|
|
3
|
+
Runtime for SEMBL — semantic coercion for TypeScript. Describe what a type
|
|
4
|
+
*means*, and turn unstructured input into a validated instance of it.
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
const draft = await sembl(listingHtml).partialCoerceTo(StayDetailsSchema);
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
This package holds the decorators, the runtime schema types, the coerce API,
|
|
11
|
+
validation, and tracing. Schemas are produced from your decorated classes by
|
|
12
|
+
[`@sembl/compiler`](https://github.com/nickrunner/sembl/tree/main/packages/compiler);
|
|
13
|
+
the LLM call is made by a provider package
|
|
14
|
+
([Anthropic](https://github.com/nickrunner/sembl/tree/main/packages/provider-anthropic),
|
|
15
|
+
[OpenAI](https://github.com/nickrunner/sembl/tree/main/packages/provider-openai)).
|
|
16
|
+
|
|
17
|
+
See the [project README](https://github.com/nickrunner/sembl#readme) for the
|
|
18
|
+
full walkthrough.
|
|
19
|
+
|
|
20
|
+
## Install
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
pnpm add @sembl/core
|
|
24
|
+
pnpm add -D @sembl/compiler
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Describing a type
|
|
28
|
+
|
|
29
|
+
`@Schema` says what a type is for, `@Describe` what each field means,
|
|
30
|
+
`@Constrain` bounds a value beyond its type, and `@ValuesFrom` says the legal
|
|
31
|
+
values come from somewhere resolved at runtime.
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { Schema, Describe, Constrain, ValuesFrom } from "@sembl/core";
|
|
35
|
+
|
|
36
|
+
@Schema("A short-term rental listing as a host would describe it.")
|
|
37
|
+
export class Listing {
|
|
38
|
+
@Describe("Display name for the listing.")
|
|
39
|
+
@Constrain({ maxLength: 40 })
|
|
40
|
+
name!: string;
|
|
41
|
+
|
|
42
|
+
@Describe("Amenities the property offers.")
|
|
43
|
+
@ValuesFrom("amenities")
|
|
44
|
+
@Constrain({ maxItems: 5 })
|
|
45
|
+
amenities!: string[];
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Coercing
|
|
50
|
+
|
|
51
|
+
`coerce` throws if a required field is missing; `partialCoerce` doesn't, and
|
|
52
|
+
returns `Partial<T>` with nulls stripped — the right one for pre-filling a form
|
|
53
|
+
a human will review. Both throw `CoerceError` on a type mismatch or a violated
|
|
54
|
+
constraint, with a `FieldValidationIssue[]` a form can render per field.
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { sembl, SemblConfig } from "@sembl/core";
|
|
58
|
+
|
|
59
|
+
SemblConfig.configure({
|
|
60
|
+
provider,
|
|
61
|
+
bundle,
|
|
62
|
+
// Called once per distinct source per coercion; you own any caching.
|
|
63
|
+
enumResolver: async (sourceId) => (await cms.taxonomy(sourceId)).map((d) => d.slug),
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const draft = await sembl(listingHtml).partialCoerceTo<Listing>(listingSchema);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
If a source backing a **required** field fails to resolve, coercion throws
|
|
70
|
+
`EnumResolutionError` rather than quietly widening the field to a free-form
|
|
71
|
+
string. A source backing only optional fields widens and records a trace event.
|
|
72
|
+
|
|
73
|
+
## Repair and provenance
|
|
74
|
+
|
|
75
|
+
`maxRepairAttempts` sends validation failures back to the model with its own
|
|
76
|
+
rejected output and the reasons. It only spends a call when validation actually
|
|
77
|
+
failed:
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
await coerce<Listing>(scrapedHtml, { provider, schema, maxRepairAttempts: 1 });
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
`partialCoerceWithProvenance` (and its strict sibling) additionally reports how
|
|
84
|
+
well the input supported each field, so a review UI can flag the guesses:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const { data, provenance } = await partialCoerceWithProvenance<Listing>(html, {
|
|
88
|
+
provider,
|
|
89
|
+
schema,
|
|
90
|
+
});
|
|
91
|
+
// provenance.name → { confidence: "high", evidence: "the Sea Cabin sleeps 6" }
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Provenance works by requesting a derived schema that wraps each field as
|
|
95
|
+
`{ value, confidence, evidence }`, then splitting the response apart and
|
|
96
|
+
validating the values against your original schema — no provider is involved.
|
|
97
|
+
Top-level fields only.
|
|
98
|
+
|
|
99
|
+
## Tracing
|
|
100
|
+
|
|
101
|
+
Pass `traceSinks` to see prompt construction, schema build, enum resolution,
|
|
102
|
+
the LLM call with token usage, and validation as nested spans. Implement
|
|
103
|
+
`TraceSink` (one `write(span)` method) to forward them anywhere.
|