@orkestrel/contract 0.0.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/LICENSE +21 -0
- package/README.md +46 -0
- package/dist/src/core/combinators.d.ts +384 -0
- package/dist/src/core/compilers.d.ts +119 -0
- package/dist/src/core/constants.d.ts +20 -0
- package/dist/src/core/helpers.d.ts +112 -0
- package/dist/src/core/index.d.ts +8 -0
- package/dist/src/core/index.js +2026 -0
- package/dist/src/core/index.js.map +1 -0
- package/dist/src/core/parsers.d.ts +230 -0
- package/dist/src/core/shapers.d.ts +178 -0
- package/dist/src/core/types.d.ts +372 -0
- package/dist/src/core/validators.d.ts +250 -0
- package/package.json +69 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { FieldPath, JSONSchema, RandomFunction, Result } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Invoke a callback and capture its outcome as a {@link Result}, never letting
|
|
4
|
+
* a throw escape.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* The single sanctioned never-throw boundary for the guards (AGENTS §14). The
|
|
8
|
+
* `whereOf`, `lazyOf`, and `transformOf` combinators invoke caller-supplied
|
|
9
|
+
* callbacks *inside* a guard body, yet a guard must NEVER throw — it returns a
|
|
10
|
+
* `boolean`. This converts a throwing callback into a `Failure` so the
|
|
11
|
+
* surrounding guard can treat it as a non-match instead of propagating the
|
|
12
|
+
* exception, written once and shared rather than copy-pasted as ad-hoc
|
|
13
|
+
* `try`/`catch`.
|
|
14
|
+
*
|
|
15
|
+
* @param callback - The callback to invoke with no arguments
|
|
16
|
+
* @returns A `Success` carrying the return value, or a `Failure` carrying the
|
|
17
|
+
* thrown reason normalised to an `Error`
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const outcome = attempt(() => predicate(value))
|
|
22
|
+
* return outcome.success && outcome.value
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare function attempt<T>(callback: () => T): Result<T>;
|
|
26
|
+
/**
|
|
27
|
+
* Resolve a (possibly nested) field value from a record by a key or key path.
|
|
28
|
+
*
|
|
29
|
+
* @remarks
|
|
30
|
+
* A single `string` is ONE key (never split on `.`, so dotted keys are safe); a
|
|
31
|
+
* string array descends left-to-right through nested objects. Intermediates may
|
|
32
|
+
* be any object — records, class instances, or arrays indexed by string. Returns
|
|
33
|
+
* `undefined` the moment a segment is missing or lands on a non-object, so the
|
|
34
|
+
* lookup is total — even against a hostile getter or Proxy trap that throws on
|
|
35
|
+
* read, contained via {@link attempt} so the throw never escapes.
|
|
36
|
+
*
|
|
37
|
+
* @param record - The source record
|
|
38
|
+
* @param path - A property key, or a key path descending into nested objects
|
|
39
|
+
* @returns The resolved value, or `undefined`
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* resolveField({ user: { name: 'Ada' } }, ['user', 'name']) // 'Ada'
|
|
44
|
+
* resolveField({ 'a.b': 1 }, 'a.b') // 1 (one key)
|
|
45
|
+
* resolveField({ a: 1 }, ['a', 'b']) // undefined
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare function resolveField(record: Readonly<Record<string, unknown>>, path: FieldPath): unknown;
|
|
49
|
+
/**
|
|
50
|
+
* Build a deterministic pseudo-random source seeded from a single number.
|
|
51
|
+
*
|
|
52
|
+
* @remarks
|
|
53
|
+
* A mulberry32 generator — the same seed always yields the same sequence, so
|
|
54
|
+
* generated seed data is reproducible across runs. Used as the default random
|
|
55
|
+
* source for {@link compileGenerator}, seeded from the wall clock so casual
|
|
56
|
+
* callers still get varied output without passing a source themselves.
|
|
57
|
+
*
|
|
58
|
+
* @param seed - The seed for the sequence
|
|
59
|
+
* @returns A {@link RandomFunction} returning values in `[0, 1)`
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* const random = seededRandom(42)
|
|
64
|
+
* random() // always the same first value for seed 42
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export declare function seededRandom(seed: number): RandomFunction;
|
|
68
|
+
/**
|
|
69
|
+
* Count the enumerable own-symbol keys on a value.
|
|
70
|
+
*
|
|
71
|
+
* @remarks
|
|
72
|
+
* String keys are ignored — only `Object.getOwnPropertySymbols` entries whose
|
|
73
|
+
* descriptor is `enumerable` are counted. Backs the object-emptiness guards
|
|
74
|
+
* (`isEmptyObject` / `isNonEmptyObject`) so a record keyed only by an
|
|
75
|
+
* enumerable symbol is not mistaken for empty.
|
|
76
|
+
*
|
|
77
|
+
* @param value - The object to inspect
|
|
78
|
+
* @returns The number of enumerable own-symbol keys
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* const flag = Symbol('flag')
|
|
83
|
+
* enumerableSymbolCount(Object.defineProperty({}, flag, { value: 1, enumerable: true })) // 1
|
|
84
|
+
* enumerableSymbolCount({}) // 0
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare function enumerableSymbolCount(value: object): number;
|
|
88
|
+
/**
|
|
89
|
+
* Narrow a compiled {@link JSONSchema} down to the open `Readonly<Record<string, unknown>>` shape
|
|
90
|
+
* tool definitions advertise as `parameters` — through the {@link isRecord} boundary guard, never
|
|
91
|
+
* an assertion (AGENTS §14).
|
|
92
|
+
*
|
|
93
|
+
* @remarks
|
|
94
|
+
* A `JSONSchema` is the closed contract-compiler fragment (it has no index signature), whereas a
|
|
95
|
+
* tool advertises its `parameters` as an open record. The two are structurally compatible but not
|
|
96
|
+
* assignable, so the schema crosses that boundary through `isRecord` — a compiled contract schema
|
|
97
|
+
* is always a record, so the guard passes; the `undefined` fallback only satisfies the type's
|
|
98
|
+
* optionality. This is the single sanctioned narrowing from a compiled contract schema to the open
|
|
99
|
+
* tool-parameters record, so the crossing lives once rather than being copy-pasted per call site.
|
|
100
|
+
*
|
|
101
|
+
* @param schema - The compiled JSON Schema (a contract's `schema`)
|
|
102
|
+
* @returns The schema as the open tool-parameters record, or `undefined` when it is not a record
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* import { createContract, schemaToParameters } from '@src/core'
|
|
107
|
+
*
|
|
108
|
+
* const contract = createContract(shape)
|
|
109
|
+
* const parameters = schemaToParameters(contract.schema) // the open record a tool advertises
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export declare function schemaToParameters(schema: JSONSchema): Readonly<Record<string, unknown>> | undefined;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type * from './types.js';
|
|
2
|
+
export * from './constants.js';
|
|
3
|
+
export * from './helpers.js';
|
|
4
|
+
export * from './combinators.js';
|
|
5
|
+
export * from './compilers.js';
|
|
6
|
+
export * from './parsers.js';
|
|
7
|
+
export * from './shapers.js';
|
|
8
|
+
export * from './validators.js';
|